Skip to content

Commit 11a2b8e

Browse files
CommanderStormautofix-ci[bot]Copilot
authored
fix(ci): backoff all external TCP checks (#6613)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b1355c6 commit 11a2b8e

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

test/backend-test/monitors/test-tcp.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ const { UP, PENDING } = require("../../../src/util");
55
const net = require("net");
66

77
describe("TCP Monitor", () => {
8+
/**
9+
* Retries a test function with exponential backoff for external service reliability
10+
* @param {Function} testFn - Async function to retry
11+
* @param {object} heartbeat - Heartbeat object to reset between attempts
12+
* @param {number} maxAttempts - Maximum number of retry attempts (default: 5)
13+
* @returns {Promise<void>}
14+
*/
15+
async function retryExternalService(testFn, heartbeat, maxAttempts = 5) {
16+
let lastError;
17+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
18+
try {
19+
await testFn();
20+
return; // Success, exit retry loop
21+
} catch (error) {
22+
lastError = error;
23+
// Reset heartbeat for next attempt
24+
heartbeat.msg = "";
25+
heartbeat.status = PENDING;
26+
// Wait a bit before retrying with exponential backoff
27+
if (attempt < maxAttempts) {
28+
await new Promise(resolve => setTimeout(resolve, 500 * 2 ** (attempt - 1)));
29+
}
30+
}
31+
}
32+
// If all retries failed, throw the last error
33+
throw lastError;
34+
}
835
/**
936
* Creates a TCP server on a specified port
1037
* @param {number} port - The port number to listen on
@@ -115,8 +142,9 @@ describe("TCP Monitor", () => {
115142
status: PENDING,
116143
};
117144

118-
await tcpMonitor.check(monitor, heartbeat, {});
119-
145+
await retryExternalService(async () => {
146+
await tcpMonitor.check(monitor, heartbeat, {});
147+
}, heartbeat);
120148
assert.strictEqual(heartbeat.status, UP);
121149
});
122150

@@ -138,8 +166,9 @@ describe("TCP Monitor", () => {
138166
status: PENDING,
139167
};
140168

141-
await tcpMonitor.check(monitor, heartbeat, {});
142-
169+
await retryExternalService(async () => {
170+
await tcpMonitor.check(monitor, heartbeat, {});
171+
}, heartbeat);
143172
assert.strictEqual(heartbeat.status, UP);
144173
});
145174

@@ -186,23 +215,9 @@ describe("TCP Monitor", () => {
186215
status: PENDING,
187216
};
188217

189-
// Retry up to 5 times for external service reliability
190-
let lastError;
191-
for (let attempt = 1; attempt <= 5; attempt++) {
192-
try {
193-
await tcpMonitor.check(monitor, heartbeat, {});
194-
assert.strictEqual(heartbeat.status, UP);
195-
return; // Success, exit test
196-
} catch (error) {
197-
lastError = error;
198-
// Reset heartbeat for next attempt
199-
heartbeat.msg = "";
200-
heartbeat.status = PENDING;
201-
// Wait a bit before retrying
202-
await new Promise(resolve => setTimeout(resolve, 500 * 2 ** (attempt - 1)));
203-
}
204-
}
205-
// If all retries failed, throw the last error
206-
throw lastError;
218+
await retryExternalService(async () => {
219+
await tcpMonitor.check(monitor, heartbeat, {});
220+
}, heartbeat);
221+
assert.strictEqual(heartbeat.status, UP);
207222
});
208223
});

0 commit comments

Comments
 (0)