Skip to content

Commit 9c0494c

Browse files
committed
A few hours and some caffeine later... We got it!
1 parent 6cba661 commit 9c0494c

File tree

1 file changed

+119
-128
lines changed

1 file changed

+119
-128
lines changed

test/e2e/jobs/jobs.test.ts

Lines changed: 119 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -18,143 +18,134 @@ import express from "express";
1818
jest.setTimeout(120 * 1000);
1919

2020
describe("Jobs End to End", () => {
21-
22-
let network: StartedNetwork | null = null;
23-
let daprScheduler: StartedTestContainer | null = null;
24-
25-
beforeAll(async () => {
26-
27-
network = await new Network().start();
28-
29-
daprScheduler = await (new GenericContainer("ghcr.io/dapr/dapr")
30-
.withName("dapr-js-sdk-test-scheduler")
31-
.withNetwork(network)
32-
.withNetworkAliases("scheduler")
33-
.withExposedPorts(8083, 8080)
34-
.withCommand([
35-
"./scheduler",
36-
// note: Don't think this is necessary, buuuuut????
37-
"--listen-address", "0.0.0.0",
38-
"--port", "8083",
39-
"--log-level", "debug",
40-
"--healthz-listen-address", "0.0.0.0",
41-
"--healthz-port", "8080",
42-
// note: This feels redundant, but here as yet another thing I've tried.
43-
// "--mode", "standalone",
44-
])
45-
.withTmpFs({
46-
"/data": "rw",
47-
})
48-
// note: Because dapr containers don't have `sh` or `bash` inside, this is kind of the best health check.
49-
.withWaitStrategy(Wait.forLogMessage("api is ready").withStartupTimeout(10000))
50-
// note: Still having some issues with this.
51-
// .withWaitStrategy(Wait.forHttp("/healthz", 8080)
52-
// .forStatusCodeMatching((statusCode) => statusCode >= 200 && statusCode <= 399))
53-
// .withStartupTimeout(120_000)
54-
.start());
21+
let network: StartedNetwork | null = null;
22+
let daprScheduler: StartedTestContainer | null = null;
23+
let daprDaemon: StartedTestContainer | null = null;
24+
let expressServer: any | null = null;
25+
let callback: (() => unknown) | null = null;
26+
27+
beforeAll(async () => {
28+
network = await new Network().start();
29+
});
30+
31+
afterAll(async () => {
32+
await network?.stop();
33+
});
34+
35+
beforeEach(async () => {
36+
37+
if (! network) throw new Error("Network not ready!");
38+
39+
callback = jest.fn(async () => { console.debug("Callback called!"); });
40+
41+
const expressApp = express()
42+
expressApp.post("/job/test", (req, res) => {
43+
44+
if (! callback) throw new Error("Callback not ready!");
45+
46+
callback();
47+
48+
res.send("👍");
5549
});
56-
57-
afterAll(async () => {
58-
await daprScheduler?.stop();
59-
await network?.stop();
50+
expressServer = expressApp.listen(8070, "0.0.0.0");
51+
52+
await TestContainers.exposeHostPorts(8070);
53+
54+
daprScheduler = await new GenericContainer("ghcr.io/dapr/dapr")
55+
.withName("dapr-js-sdk-test-scheduler")
56+
.withNetwork(network)
57+
.withNetworkAliases("scheduler")
58+
.withExposedPorts(8083, 8080)
59+
.withCommand([
60+
"./scheduler",
61+
"--listen-address",
62+
"0.0.0.0",
63+
"--port",
64+
"8083",
65+
"--log-level",
66+
"debug",
67+
"--healthz-listen-address",
68+
"0.0.0.0",
69+
"--healthz-port",
70+
"8080",
71+
])
72+
.withTmpFs({
73+
"/data": "rw",
74+
})
75+
.withWaitStrategy(Wait.forLogMessage("api is ready").withStartupTimeout(10000))
76+
.start()
77+
;
78+
79+
daprDaemon = await new GenericContainer("ghcr.io/dapr/dapr")
80+
.withName("dapr-js-sdk-test-daemon")
81+
.withNetwork(network)
82+
.withNetworkAliases("daprd")
83+
.withExposedPorts(8081, 8082)
84+
.withCommand([
85+
"./daprd",
86+
"--app-id", "dapr-js-sdk-testing",
87+
"--app-channel-address", "host.testcontainers.internal",
88+
// "--app-channel-address", "host.containers.internal",
89+
"--app-protocol", "http",
90+
"--app-port", "8070",
91+
"--dapr-grpc-port", "8081",
92+
"--dapr-http-port", "8082",
93+
"--scheduler-host-address", `scheduler:8083`,
94+
"--placement-host-address", "",
95+
"--enable-metrics", "false",
96+
"--log-level", "debug",
97+
"--enable-api-logging",
98+
])
99+
.withWaitStrategy(Wait.forLogMessage("dapr initialized. Status: Running.").withStartupTimeout(10000))
100+
// .withWaitStrategy(Wait.forLogMessage("HTTP server is running on port").withStartupTimeout(10000))
101+
// .withWaitStrategy(Wait.forHttp("/v1.0/healthz/outbound", 8082).forStatusCodeMatching((statusCode) => statusCode >= 200 && statusCode <= 399))
102+
// .withStartupTimeout(120_000)
103+
.start()
104+
;
105+
});
106+
107+
afterEach(async () => {
108+
await daprDaemon?.stop();
109+
await daprScheduler?.stop();
110+
await expressServer?.close();
111+
});
112+
113+
it("Registers and receives a one second job five times.", async () => {
114+
115+
const times = 5;
116+
117+
const client = new DaprClient({
118+
daprHost: "localhost",
119+
daprPort: getPort(daprDaemon, 8082),
120+
communicationProtocol: CommunicationProtocolEnum.HTTP,
60121
});
61122

62-
it("Registers and receives a one second job five times.", async () => {
63-
64-
const callback = jest.fn(async () => { console.info("Callback called!"); });
65-
66-
const expressApp = express();
67-
expressApp.post("/job/test", callback);
68-
const expressServer = expressApp.listen(8070);
69-
70-
console.log("Waiting for listen...");
71-
await (new Promise(resolve => setTimeout(resolve, 10000)));
72-
console.log("Done waiting for listen.");
73-
74-
console.info("Exposing 8070.");
75-
await TestContainers.exposeHostPorts(8070);
76-
console.info("8070 exposed.");
123+
await client?.jobs.schedule(
124+
"test",
125+
{ value: "test" },
126+
"* * * * * *"
127+
);
77128

78-
const daprContainer = new GenericContainer("ghcr.io/dapr/dapr")
79-
.withName("dapr-js-sdk-test-daemon")
80-
.withNetwork(network!)
81-
.withNetworkAliases("daprd")
82-
.withExposedPorts(8081, 8082)
83-
.withCommand([
84-
"./daprd",
85-
"--app-id", "dapr-js-sdk-testing",
86-
"--app-channel-address", "host.testcontainers.internal",
87-
// "--app-channel-address", "host.containers.internal",
88-
"--app-protocol", "http",
89-
"--app-port", "8070",
90-
"--dapr-grpc-port", "8081",
91-
"--dapr-http-port", "8082",
92-
"--scheduler-host-address", `scheduler:8083`,
93-
"--placement-host-address", "",
94-
"--enable-metrics", "false",
95-
"--log-level", "debug",
96-
"--enable-api-logging",
97-
])
98-
// .withWaitStrategy(Wait.forLogMessage("dapr initialized. Status: Running.").withStartupTimeout(10000))
99-
.withWaitStrategy(Wait.forLogMessage("HTTP server is running on port").withStartupTimeout(10000))
100-
// .withWaitStrategy(Wait.forHttp("/v1.0/healthz/outbound", 8082)
101-
// .forStatusCodeMatching((statusCode) => statusCode >= 200 && statusCode <= 399))
102-
// .withStartupTimeout(120_000)
103-
;
129+
const job = await client?.jobs.get("test");
130+
131+
await (new Promise(resolve => setTimeout(resolve, times * 1000)));
104132

105-
const daprd = await daprContainer.start();
106-
console.info("daprd started.");
107-
const client = new DaprClient({
108-
daprHost: getIp(daprd),
109-
daprPort: getPort(daprd, 8082),
110-
communicationProtocol: CommunicationProtocolEnum.HTTP,
111-
});
112-
console.info("client created.");
113-
await client?.jobs.schedule(
114-
"test",
115-
{ value: "test" },
116-
"* * * * * *"
117-
);
118-
console.info("job scheduled.");
119-
const job = await client?.jobs.get("test");
120-
121-
await client.start();
122-
console.info("client started.");
123-
124-
console.log("Waiting...");
125-
await (new Promise(resolve => setTimeout(resolve, 10000)));
126-
console.log("Done waiting.");
127-
128-
await client?.jobs.delete("test");
129-
expressServer.close();
130-
131-
expect(job).toMatchObject({
132-
"data": {
133-
"value": {
134-
"value": "test",
135-
},
133+
expect(job).toMatchObject({
134+
"data": {
135+
"value": {
136+
"value": "test",
136137
},
137-
"name": "test",
138-
"schedule": "* * * * * *",
139-
});
140-
expect(callback).toHaveBeenCalledTimes(5);
141-
expect(callback).toHaveBeenCalledWith({
142-
value: "test",
143-
});
138+
},
139+
"name": "test",
140+
"schedule": "* * * * * *",
144141
});
145-
146-
function getIp(container: StartedTestContainer | null | undefined): string {
147-
148-
if (! network) throw new Error("Network is null or undefined?");
149-
if (! container) throw new Error("Container is null or undefined?");
150-
151-
return container.getIpAddress(network.getName());
152-
}
142+
143+
expect(callback).toHaveBeenCalledTimes(times);
144+
});
153145

154146
function getPort(container: StartedTestContainer | null | undefined, port: number): string {
155-
156-
if (! container) throw new Error("Container is null or undefined?");
147+
if (!container) throw new Error("Container is null or undefined?");
157148

158149
return container.getMappedPort(port).toString();
159150
}
160-
});
151+
});

0 commit comments

Comments
 (0)