forked from hyperledger-cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): files needs to migrated from Tape to Jest
Primary Changes --------------- 1. Updated logger-provider.ts and logger.ts for LoggerProvider configuration and added a way for optional custom writable stream 2. Using the custom writable stream, fixed logger.test.ts after removing the skip on the test and UUID marker now appears in the custom stream output 3. Migrated test file from tape to jest 4. Removed file path of logger.test.ts in testPathIgnorePatterns to run jest test Fixes: Signed-off-by: ruzell22 <[email protected]>
- Loading branch information
Showing
4 changed files
with
48 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 26 additions & 58 deletions
84
packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,34 @@ | ||
import test, { Test } from "tape"; | ||
Check failure on line 1 in packages/cactus-common/src/test/typescript/unit/logging/logger.test.ts GitHub Actions / yarn_lint
|
||
import { v4 as uuidv4 } from "uuid"; | ||
import { LoggerProvider } from "../../../../main/typescript/public-api"; | ||
import { Writable } from "stream"; | ||
|
||
describe("Logger Tests", () => { | ||
it("Logger#debug/error writes to stdout/stderr", async () => { | ||
const outputData: string[] = []; | ||
const customStream = new Writable({ | ||
write(chunk, encoding, callback) { | ||
outputData.push(chunk.toString()); | ||
callback(); | ||
}, | ||
}); | ||
|
||
// FIXME(2020-11-12) this does not work because for some reason the stdout | ||
// stream does not emit 'data' events with anything even though it should. | ||
// Suspecting that the test runner library does some internal magic with | ||
// piping the stream somewhere else or similar foul play at hand. | ||
// Until we can fix this, marked the test to be skipped. | ||
test.skip("Logger#debug/error writes to stdout/stderr", async (t: Test) => { | ||
const log = LoggerProvider.getOrCreate({ | ||
level: "TRACE", | ||
label: "logger-test", | ||
}); | ||
|
||
// generate random UUID v4 to guarantee we don't mistake something else as the marker | ||
const marker = uuidv4(); | ||
interface DataHandler { | ||
(data: Buffer): void; | ||
} | ||
// wait for the marker to appear on stdout OR crash with timeout if it never comes | ||
let aggregateStdOut = ""; | ||
let stdOutDataHandler: undefined | DataHandler; | ||
let didNotThrow: boolean; | ||
|
||
try { | ||
// hook up to the stdout data stream and wrap it in a promise that can be awaited | ||
// for when the marker does appear on stdout (which would be a passing test) | ||
// or when it times out (which would mean the test is failing). | ||
// Certain issues could happen here if the stream is chunking data and then you never | ||
// actually get the complete marker string at once but instead different parts of it | ||
// but I did not consider this because the uuid is only a few dozen bytes when stored as a hex string | ||
// so I'm pretty confident it wouldn't get chunked (probably not impossible either though so | ||
// if you are paranoid about that happening (which would make the test flaky) then you can | ||
// bake in some stream data aggregation instead where you collect and continually append | ||
// the incoming data chunks and test for marker presence in the aggregate variable not the chunk | ||
// that is provided in the 'data' event handler callback. | ||
await new Promise<void>((resolve, reject) => { | ||
const timeoutMsg = "Timed out waiting for marker to appear on stdout"; | ||
const timerId = setTimeout(() => reject(new Error(timeoutMsg)), 5000); | ||
|
||
const stdOutDataHandler: DataHandler = (data: Buffer) => { | ||
const msg = data.toString("utf-8"); | ||
aggregateStdOut = aggregateStdOut.concat(msg); | ||
if (msg.includes(marker)) { | ||
clearInterval(timerId); | ||
resolve(); | ||
} | ||
}; | ||
|
||
process.stdout.on("data", stdOutDataHandler); | ||
|
||
// send the log now that we have hooked into the stream waiting for the marker to appear | ||
log.info(marker); | ||
const log = LoggerProvider.getOrCreate({ | ||
level: "TRACE", | ||
label: "logger-test", | ||
stream: customStream, | ||
}); | ||
didNotThrow = true; | ||
} catch (ex) { | ||
didNotThrow = false; | ||
} | ||
|
||
process.stdout.off("data", stdOutDataHandler as DataHandler); | ||
t.comment(`Aggregate std out messages: ${aggregateStdOut}`); | ||
t.true(didNotThrow, "Marker appeared on stdout on time OK"); | ||
// generate random UUID v4 to guarantee we don't mistake something else as the marker | ||
const marker = uuidv4(); | ||
log.info(marker); | ||
|
||
t.end(); | ||
// Delay verification to ensure all writes are flushed | ||
await new Promise((resolve) => setImmediate(resolve)); | ||
|
||
// Verify marker in output | ||
const capturedOutput = outputData.join(""); | ||
expect(capturedOutput).toContain(marker); | ||
log.info(`Marker (${marker}) appeared in custom stream output`); | ||
}); | ||
}); |