Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mongodb-log-writer/src/mongo-log-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('MongoLogManager', function () {
});

afterEach(async function () {
await fs.rmdir(directory, { recursive: true });
await fs.rm(directory, { recursive: true });
sinon.restore();
});

Expand Down
21 changes: 21 additions & 0 deletions packages/mongodb-log-writer/src/mongo-log-writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,25 @@ describe('MongoLogWriter', function () {
new Set(['TypeError'])
);
});

it('flushes pending writes on the MongoLogWriter itself', async function() {
const chunks: string[] = [];
const target = new stream.Writable({
write(chunk, encoding, callback) {
chunks.push(chunk.toString());
// Simulate a 'slow' consumer (i.e. one that does actual I/O,
// as opposed to one that does not do asynchronous work outside
// of microtask queues (promises, nextTick)
setImmediate(callback);
}
});
const w = new MongoLogWriter('id', null, target);
for (let i = 0; i < 5; i++) {
w.info('component', mongoLogId(0), 'ctx', 'msg', { i });
}
await w.flush();
expect(chunks.map(c => c ? JSON.parse(c).attr.i : c)).to.deep.equal([
0, 1, 2, 3, 4, ''
]);
});
});
10 changes: 8 additions & 2 deletions packages/mongodb-log-writer/src/mongo-log-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { EJSON } from 'bson';
import { Writable } from 'stream';
import { inspect } from 'util';

const kFlushDummy = Symbol('kFlushDummy');

type PlainWritable = Pick<Writable, 'write' | 'end'>;

/**
Expand Down Expand Up @@ -112,10 +114,14 @@ export class MongoLogWriter extends Writable {
}

_write(
info: MongoLogEntry,
info: MongoLogEntry | typeof kFlushDummy,
encoding: unknown,
callback: (err?: Error | null | undefined) => void
): void {
if (info === kFlushDummy) {
this._target.write('', callback);
return;
}
const validationError = validateLogEntry(info);
if (validationError) {
callback(validationError);
Expand Down Expand Up @@ -183,7 +189,7 @@ export class MongoLogWriter extends Writable {

/** Wait until all pending data has been written to the underlying stream. */
async flush(): Promise<void> {
await new Promise((resolve) => this._target.write('', resolve));
await new Promise((resolve) => this.write(kFlushDummy, resolve));
}

/**
Expand Down