Skip to content

Commit 6c71cca

Browse files
fix: treat 0 packet loss as successful even if execaError (#351)
1 parent 9beb065 commit 6c71cca

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib/status-manager.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ export class StatusManager {
183183
} catch (reason) {
184184
if (isExecaError(reason) && reason?.stdout?.toString()?.length) {
185185
const parsed = parse(reason.stdout.toString());
186-
unSuccessfulResults.push({ target, result: parsed });
186+
const isSuccessful = parsed.stats?.loss === 0;
187+
188+
if (isSuccessful) {
189+
successfulResults.push({ target, result: parsed });
190+
} else {
191+
unSuccessfulResults.push({ target, result: parsed });
192+
}
187193
} else {
188194
rejectedResults.push({ target, reason: reason as ExecaError });
189195
}

test/unit/lib/status-manager.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ describe('StatusManager', () => {
137137
expect(socket.emit.args[2]).to.deep.equal([ 'probe:isIPv6Supported:update', true ]);
138138
});
139139

140+
it('should treat rejected execa error with 0% packet loss as successful (happens if command times out)', async () => {
141+
const statusManager = new StatusManager(socket, pingCmd);
142+
pingCmd.rejects({ stdout: pingSuccess, stderr: 'Command timed out', exitCode: 143 });
143+
await statusManager.start();
144+
expect(pingCmd.callCount).to.equal(6);
145+
expect(statusManager.getStatus()).to.equal('ready');
146+
expect(socket.emit.callCount).to.equal(3);
147+
expect(socket.emit.args[0]).to.deep.equal([ 'probe:status:update', 'ready' ]);
148+
expect(socket.emit.args[1]).to.deep.equal([ 'probe:isIPv4Supported:update', true ]);
149+
expect(socket.emit.args[2]).to.deep.equal([ 'probe:isIPv6Supported:update', true ]);
150+
});
151+
152+
it('should treat rejected execa error with packet loss as failed', async () => {
153+
const statusManager = new StatusManager(socket, pingCmd);
154+
pingCmd.rejects({ stdout: pingPacketLoss, stderr: 'Command timed out', exitCode: 143 });
155+
await statusManager.start();
156+
expect(pingCmd.callCount).to.equal(6);
157+
expect(statusManager.getStatus()).to.equal('ping-test-failed');
158+
expect(socket.emit.callCount).to.equal(3);
159+
expect(socket.emit.args[0]).to.deep.equal([ 'probe:status:update', 'ping-test-failed' ]);
160+
expect(socket.emit.args[1]).to.deep.equal([ 'probe:isIPv4Supported:update', false ]);
161+
expect(socket.emit.args[2]).to.deep.equal([ 'probe:isIPv6Supported:update', false ]);
162+
});
163+
140164
it('should run check in a fixed intervals and do emit with a status every time', async () => {
141165
const statusManager = new StatusManager(socket, pingCmd);
142166
expect(pingCmd.callCount).to.equal(0);

0 commit comments

Comments
 (0)