-
Notifications
You must be signed in to change notification settings - Fork 886
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: allow sync flag to be passed to ThreadStream for synchronous… (
#2063) * feature: allow sync flag to be passed to ThreadStream for synchronous logging * tests: add unit tests for sync true to ThreadStream * tests: fix unit test * tests: fix unit test
- Loading branch information
1 parent
f220965
commit 5bc0a92
Showing
3 changed files
with
74 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict' | ||
|
||
const pino = require('../..') | ||
const { join } = require('node:path') | ||
const { readFileSync } = require('node:fs') | ||
const { test } = require('tap') | ||
const { file } = require('../helper') | ||
|
||
test('thread-stream sync true should log synchronously', async (t) => { | ||
const outputPath = file() | ||
|
||
function getOutputLogLines () { | ||
return (readFileSync(outputPath)).toString().trim().split('\n').map(JSON.parse) | ||
} | ||
|
||
const transport = pino.transport({ | ||
target: join(__dirname, '..', 'fixtures', 'to-file-transport.js'), | ||
options: { destination: outputPath, flush: true }, | ||
sync: true | ||
}) | ||
const instance = pino(transport) | ||
|
||
var value = { message: 'sync' } | ||
instance.info(value) | ||
instance.info(value) | ||
instance.info(value) | ||
instance.info(value) | ||
instance.info(value) | ||
instance.info(value) | ||
let interrupt = false | ||
let flushData | ||
let loopCounter = 0 | ||
|
||
// Start a synchronous loop | ||
while (!interrupt && loopCounter < (process.env.MAX_TEST_LOOP_ITERATION || 20000)) { | ||
try { | ||
loopCounter++ | ||
const data = getOutputLogLines() | ||
flushData = data | ||
if (data) { | ||
interrupt = true | ||
break | ||
} | ||
} catch (error) { | ||
// File may not exist yet | ||
// Wait till MAX_TEST_LOOP_ITERATION iterations | ||
} | ||
} | ||
|
||
if (!interrupt) { | ||
throw new Error('Sync loop did not get interrupt') | ||
} | ||
|
||
t.equal(flushData.length, 6) | ||
}) |