Skip to content

Commit 744ece9

Browse files
authored
Delay summary timings output until finished (#431)
This fixes the problem that summary timings are printed before grouped output (Fixes #316).
1 parent c36f059 commit 744ece9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/flow-control/log-timings.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ it('returns same commands', () => {
5757

5858
it("does not log timings and doesn't throw if no logger is provided", () => {
5959
controller = new LogTimings({});
60-
controller.handle(commands);
60+
const { onFinish } = controller.handle(commands);
6161

6262
commands[0].timer.next({ startDate: startDate0 });
6363
commands[1].timer.next({ startDate: startDate1 });
6464
commands[1].timer.next({ startDate: startDate1, endDate: endDate1 });
6565
commands[0].timer.next({ startDate: startDate0, endDate: endDate0 });
6666

67+
onFinish?.();
68+
6769
expect(logger.logCommandEvent).toHaveBeenCalledTimes(0);
6870
});
6971

@@ -101,20 +103,26 @@ it('logs the timings at the start and end (ie complete or error) event of each c
101103
});
102104

103105
it('does not log timings summary if there was an error', () => {
104-
controller.handle(commands);
106+
const { onFinish } = controller.handle(commands);
105107

106108
commands[0].close.next(command0ExitInfo);
107109
commands[1].error.next(undefined);
108110

111+
onFinish?.();
112+
109113
expect(logger.logTable).toHaveBeenCalledTimes(0);
110114
});
111115

112-
it('logs the sorted timings summary when all processes close successfully', () => {
113-
controller.handle(commands);
116+
it('logs the sorted timings summary when all processes close successfully after onFinish is called', () => {
117+
const { onFinish } = controller.handle(commands);
114118

115119
commands[0].close.next(command0ExitInfo);
116120
commands[1].close.next(command1ExitInfo);
117121

122+
expect(logger.logGlobalEvent).toHaveBeenCalledTimes(0);
123+
124+
onFinish?.();
125+
118126
expect(logger.logGlobalEvent).toHaveBeenCalledTimes(1);
119127
expect(logger.logGlobalEvent).toHaveBeenCalledWith('Timings:');
120128
expect(logger.logTable).toHaveBeenCalledTimes(1);

src/flow-control/log-timings.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import formatDate from 'date-fns/format';
33
import _ from 'lodash';
44
import * as Rx from 'rxjs';
5-
import { bufferCount, take } from 'rxjs/operators';
5+
import { bufferCount, combineLatestWith, take } from 'rxjs/operators';
66

77
import { CloseEvent, Command } from '../command';
88
import * as defaults from '../defaults';
@@ -97,11 +97,13 @@ export class LogTimings implements FlowController {
9797

9898
// overall summary timings
9999
const closeStreams = commands.map((command) => command.close);
100+
const finished = new Rx.Subject<void>();
100101
const allProcessesClosed = Rx.merge(...closeStreams).pipe(
101102
bufferCount(closeStreams.length),
102-
take(1)
103+
take(1),
104+
combineLatestWith(finished)
103105
);
104-
allProcessesClosed.subscribe((exitInfos) => this.printExitInfoTimingTable(exitInfos));
105-
return { commands };
106+
allProcessesClosed.subscribe(([exitInfos]) => this.printExitInfoTimingTable(exitInfos));
107+
return { commands, onFinish: () => finished.next() };
106108
}
107109
}

0 commit comments

Comments
 (0)