Skip to content

Commit ef7add2

Browse files
committed
add tests for 3 main cases
1 parent 93718bc commit ef7add2

File tree

2 files changed

+88
-22
lines changed

2 files changed

+88
-22
lines changed

packages/server/src/service.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ export function useService(
6060
}
6161
}
6262

63-
const waiting = options.wellnessCheck.timeout
64-
? timebox(options.wellnessCheck.timeout, untilWell)
65-
: untilWell();
66-
const checked = yield* waiting;
67-
if (options.wellnessCheck.timeout && checked && checked.timeout) {
63+
if (options.wellnessCheck.timeout) {
64+
const checked = yield* timebox(
65+
options.wellnessCheck.timeout,
66+
untilWell
67+
);
68+
if (checked && checked.timeout) {
6869
throw new Error("service wellness check timed out");
70+
}
71+
} else {
72+
yield* untilWell();
6973
}
7074
}
7175

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { it } from "node:test";
1+
import { describe, it } from "node:test";
22
import assert from "node:assert";
33
import { useService } from "../src/service.ts";
44
import { each, Err, Ok, run } from "effection";
@@ -16,24 +16,86 @@ it("test service", async () => {
1616
assert(assertionCount > 0);
1717
});
1818

19-
it("test with wellness", async () => {
20-
let assertionCount = 0;
21-
await run(function* () {
22-
yield* useService("test-service", nodeScriptWorks, {
23-
wellnessCheck: {
24-
timeout: 200,
25-
*operation(stdio) {
26-
for (let line of yield* each<string>(stdio)) {
27-
if (line.includes("test service started")) {
28-
return Ok<void>(void 0);
19+
describe("useService with wellness check", () => {
20+
it("test with short wellness timeout", async () => {
21+
let assertionCount = 0;
22+
let errored = false;
23+
await run(function* () {
24+
yield* useService("test-service", nodeScriptWorks, {
25+
wellnessCheck: {
26+
timeout: 2,
27+
*operation(stdio) {
28+
for (let line of yield* each<string>(stdio)) {
29+
console.log("wellness check got line:", { line });
30+
if (line.includes("test service started")) {
31+
return Ok<void>(void 0);
32+
}
33+
yield* each.next();
2934
}
30-
yield* each.next();
31-
}
32-
return Err(new Error("got sick of waiting"));
35+
return Err(new Error("got sick of waiting"));
36+
},
3337
},
34-
},
38+
});
39+
assertionCount++;
40+
}).catch((error) => {
41+
assert.equal(error.message, "service wellness check timed out");
42+
errored = true;
43+
assertionCount++;
3544
});
36-
assertionCount++;
45+
assert(assertionCount > 0);
46+
assert(errored);
47+
});
48+
49+
it("test with long wellness timeout", async () => {
50+
let assertionCount = 0;
51+
await run(function* () {
52+
yield* useService("test-service", nodeScriptWorks, {
53+
wellnessCheck: {
54+
timeout: 2000,
55+
*operation(stdio) {
56+
for (let line of yield* each<string>(stdio)) {
57+
console.log("wellness check got line:", { line });
58+
if (line.includes("test service started")) {
59+
return Ok<void>(void 0);
60+
}
61+
yield* each.next();
62+
}
63+
return Err(new Error("got sick of waiting"));
64+
},
65+
},
66+
});
67+
assertionCount++;
68+
});
69+
assert(assertionCount > 0);
70+
});
71+
72+
it("test with mid-frequency wellness timeout", async () => {
73+
let assertionCount = 0;
74+
let errored = false;
75+
await run(function* () {
76+
yield* useService("test-service", nodeScriptWorks, {
77+
wellnessCheck: {
78+
timeout: 200,
79+
frequency: 100,
80+
*operation(stdio) {
81+
for (let line of yield* each<string>(stdio)) {
82+
console.log("wellness check got line:", { line });
83+
if (line.includes("test service started")) {
84+
return Ok<void>(void 0);
85+
}
86+
yield* each.next();
87+
}
88+
return Err(new Error("got sick of waiting"));
89+
},
90+
},
91+
});
92+
assertionCount++;
93+
}).catch((error) => {
94+
assert.equal(error.message, "service wellness check timed out");
95+
errored = true;
96+
assertionCount++;
97+
});
98+
assert(assertionCount > 0);
99+
assert(errored);
37100
});
38-
assert(assertionCount > 0);
39101
});

0 commit comments

Comments
 (0)