|
1 | 1 | const EventEmitter = require('events'); |
| 2 | +const process = require('process'); |
2 | 3 | const Command = require('./command'); |
3 | 4 |
|
4 | 5 | const createProcess = () => { |
@@ -54,6 +55,72 @@ describe('#start()', () => { |
54 | 55 | process.emit('error', 'foo'); |
55 | 56 | }); |
56 | 57 |
|
| 58 | + it('shares start and close timing events to the timing stream', done => { |
| 59 | + const process = createProcess(); |
| 60 | + const command = new Command({ spawn: () => process }); |
| 61 | + |
| 62 | + const startDate = new Date(); |
| 63 | + const endDate = new Date(startDate.getTime() + 1000); |
| 64 | + jest.spyOn(Date, 'now') |
| 65 | + .mockReturnValueOnce(startDate.getTime()) |
| 66 | + .mockReturnValueOnce(endDate.getTime()); |
| 67 | + |
| 68 | + let callCount = 0; |
| 69 | + command.timer.subscribe(({startDate: actualStartDate, endDate: actualEndDate}) => { |
| 70 | + switch (callCount) { |
| 71 | + case 0: |
| 72 | + expect(actualStartDate).toStrictEqual(startDate); |
| 73 | + expect(actualEndDate).toBeUndefined(); |
| 74 | + break; |
| 75 | + case 1: |
| 76 | + expect(actualStartDate).toStrictEqual(startDate); |
| 77 | + expect(actualEndDate).toStrictEqual(endDate); |
| 78 | + done(); |
| 79 | + break; |
| 80 | + default: |
| 81 | + throw new Error('Unexpected call count'); |
| 82 | + } |
| 83 | + callCount++; |
| 84 | + }); |
| 85 | + |
| 86 | + command.start(); |
| 87 | + process.emit('close', 0, null); |
| 88 | + |
| 89 | + }); |
| 90 | + |
| 91 | + it('shares start and error timing events to the timing stream', done => { |
| 92 | + const process = createProcess(); |
| 93 | + const command = new Command({ spawn: () => process }); |
| 94 | + |
| 95 | + const startDate = new Date(); |
| 96 | + const endDate = new Date(startDate.getTime() + 1000); |
| 97 | + jest.spyOn(Date, 'now') |
| 98 | + .mockReturnValueOnce(startDate.getTime()) |
| 99 | + .mockReturnValueOnce(endDate.getTime()); |
| 100 | + |
| 101 | + let callCount = 0; |
| 102 | + command.timer.subscribe(({startDate: actualStartDate, endDate: actualEndDate}) => { |
| 103 | + switch (callCount) { |
| 104 | + case 0: |
| 105 | + expect(actualStartDate).toStrictEqual(startDate); |
| 106 | + expect(actualEndDate).toBeUndefined(); |
| 107 | + break; |
| 108 | + case 1: |
| 109 | + expect(actualStartDate).toStrictEqual(startDate); |
| 110 | + expect(actualEndDate).toStrictEqual(endDate); |
| 111 | + done(); |
| 112 | + break; |
| 113 | + default: |
| 114 | + throw new Error('Unexpected call count'); |
| 115 | + } |
| 116 | + callCount++; |
| 117 | + }); |
| 118 | + |
| 119 | + command.start(); |
| 120 | + process.emit('error', 0, null); |
| 121 | + |
| 122 | + }); |
| 123 | + |
57 | 124 | it('shares closes to the close stream with exit code', done => { |
58 | 125 | const process = createProcess(); |
59 | 126 | const command = new Command({ spawn: () => process }); |
@@ -83,6 +150,31 @@ describe('#start()', () => { |
83 | 150 | process.emit('close', null, 'SIGKILL'); |
84 | 151 | }); |
85 | 152 |
|
| 153 | + it('shares closes to the close stream with timing information', done => { |
| 154 | + const process1 = createProcess(); |
| 155 | + const command = new Command({ spawn: () => process1 }); |
| 156 | + |
| 157 | + const startDate = new Date(); |
| 158 | + const endDate = new Date(startDate.getTime() + 1000); |
| 159 | + jest.spyOn(Date, 'now') |
| 160 | + .mockReturnValueOnce(startDate.getTime()) |
| 161 | + .mockReturnValueOnce(endDate.getTime()); |
| 162 | + |
| 163 | + jest.spyOn(process, 'hrtime') |
| 164 | + .mockReturnValueOnce([0, 0]) |
| 165 | + .mockReturnValueOnce([1, 1e8]); |
| 166 | + |
| 167 | + command.close.subscribe(data => { |
| 168 | + expect(data.timings.startDate).toStrictEqual(startDate); |
| 169 | + expect(data.timings.endDate).toStrictEqual(endDate); |
| 170 | + expect(data.timings.durationSeconds).toBe(1.1); |
| 171 | + done(); |
| 172 | + }); |
| 173 | + |
| 174 | + command.start(); |
| 175 | + process1.emit('close', null, 'SIGKILL'); |
| 176 | + }); |
| 177 | + |
86 | 178 | it('shares closes to the close stream with command info and index', done => { |
87 | 179 | const process = createProcess(); |
88 | 180 | const commandInfo = { |
@@ -170,7 +262,7 @@ describe('#kill()', () => { |
170 | 262 |
|
171 | 263 | it('marks the command as killed', done => { |
172 | 264 | command.start(); |
173 | | - |
| 265 | + |
174 | 266 | command.close.subscribe(data => { |
175 | 267 | expect(data.exitCode).toBe(1); |
176 | 268 | expect(data.killed).toBe(true); |
|
0 commit comments