Skip to content

Commit aef619a

Browse files
Add tests for Sleeps
Signed-off-by: Xavier Geerinck <[email protected]>
1 parent f332bd6 commit aef619a

File tree

9 files changed

+412
-55
lines changed

9 files changed

+412
-55
lines changed

package-lock.json

Lines changed: 349 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"types": "http/index.d.ts",
66
"scripts": {
77
"test": "npm run test:unit:all && npm run test:e2e:all",
8-
"test:load": "jest --runInBand",
8+
"test:load": "jest --runInBand --detectOpenHandles",
99
"test:load:http": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components npm run test:load 'test/load'",
10-
"test:e2e": "jest --runInBand",
10+
"test:e2e": "jest --runInBand --detectOpenHandles",
1111
"test:e2e:all": "npm run test:e2e:grpc:main && npm run test:e2e:http:main && npm run test:e2e:http:actors",
1212
"test:e2e:grpc:main": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol grpc --app-port 50001 --dapr-grpc-port 50000 --components-path ./test/components npm run test:e2e 'test/e2e/main.grpc.test.ts'",
1313
"test:e2e:http:main": "TEST_SECRET_1=secret_val_1 TEST_SECRET_2=secret_val_2 dapr run --app-id test-suite --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./test/components npm run test:e2e 'test/e2e/main.http.test.ts'",
@@ -28,6 +28,7 @@
2828
"@js-temporal/polyfill": "^0.3.0",
2929
"body-parser": "^1.19.0",
3030
"google-protobuf": "^3.18.0",
31+
"http-terminator": "^3.0.4",
3132
"node-fetch": "^2.6.1",
3233
"restana": "^4.9.1",
3334
"uuid": "^8.3.2"

src/implementation/Server/GRPCServer/GRPCServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class GRPCServer implements IServer {
1717
server: IServerType;
1818
serverImpl: IServerImplType;
1919
serverCredentials: grpc.ServerCredentials;
20-
serverStartupDelay = 1000; // @todo: use health api https://docs.dapr.io/reference/api/health_api/
20+
serverStartupDelay = 1000;
2121
client: DaprClient;
2222

2323
constructor(client: DaprClient) {

src/implementation/Server/HTTPServer/HTTPServer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import HTTPServerImpl from "./HTTPServerImpl";
44
import IServer from "../../../interfaces/Server/IServer";
55
import * as NodeJSUtils from "../../../utils/NodeJS.util";
66
import { DaprClient } from "../../..";
7+
import { createHttpTerminator } from 'http-terminator';
78

89
// eslint-disable-next-line
910
export interface IServerImplType extends HTTPServerImpl { }
@@ -17,7 +18,7 @@ export default class HTTPServer implements IServer {
1718
server: IServerType;
1819
serverAddress: string;
1920
serverImpl: IServerImplType;
20-
serverStartupDelay = 1000; // @todo: use health api https://docs.dapr.io/reference/api/health_api/
21+
serverStartupDelay = 1000;
2122
client: DaprClient;
2223

2324
constructor(client: DaprClient) {
@@ -124,7 +125,10 @@ export default class HTTPServer implements IServer {
124125
}
125126

126127
async stop(): Promise<void> {
127-
await this.server.close();
128+
const s = this.server.getServer();
129+
const httpTerminator = createHttpTerminator({ server: this.server.getServer() });
130+
await httpTerminator.terminate();
131+
// await this.server.close();
128132
this.isInitialized = false;
129133
}
130134
}

src/implementation/Server/HTTPServer/actor.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ export default class HTTPServerActor implements IServerActor {
3939
* This will create the routes that get invoked by the Dapr Sidecar
4040
*/
4141
async init(): Promise<void> {
42-
// Change the server startupDelay to 3s since we want the placement tables to get updated correctly
43-
// @todo: is there a better way to detect this?
44-
this.server.serverStartupDelay = 4000;
45-
console.log("[Actors] Initializing, changing startup delay to 4s to allow for placement tables to propagate");
42+
console.log("[Actors] Initializing");
4643

4744
// Probes the application for a response to state that the app is healthy and running
4845
// https://docs.dapr.io/reference/api/actors_api/#health-check

src/utils/NodeJS.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export async function sleep(ms = 1000): Promise<void> {
2-
await (new Promise(resolve => setTimeout(resolve, ms)));
2+
return new Promise(resolve => setTimeout(resolve, ms));
33
}
44

55
export async function flushPromises() {

test/e2e/actors.http.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import DemoActorSayImpl from '../actor/DemoActorSayImpl';
88
import DemoActorTimerImpl from '../actor/DemoActorTimerImpl';
99
import ActorId from '../../src/actors/ActorId';
1010
import ActorProxyBuilder from '../../src/actors/client/ActorProxyBuilder';
11+
import * as NodeJSUtil from '../../src/utils/NodeJS.util';
1112

1213
const serverHost = "127.0.0.1";
1314
const serverPort = "50001";
@@ -48,10 +49,26 @@ describe('http/actors', () => {
4849
}, 30 * 1000);
4950

5051
afterAll(async () => {
51-
// await server.stop();
52+
await server.stop(); // if we hang here, it means connections are open that were not closed. Debug why
5253
// await client.stop();
5354
});
5455

56+
describe('general', () => {
57+
it('should allow us to use promises as some testing libraries have issues within the Node Ecosystem', async () => {
58+
const mock = jest.fn(() => {
59+
return new Promise((resolve) => setTimeout(resolve, 500));
60+
});
61+
62+
const handler = jest.fn();
63+
64+
await mock().then(handler);
65+
66+
await (new Promise(resolve => setTimeout(resolve, 2000)));
67+
68+
expect(handler).toHaveBeenCalled();
69+
});
70+
});
71+
5572
describe('actorProxy', () => {
5673
it('should be able to create an actor object through the proxy', async () => {
5774
const builder = new ActorProxyBuilder<DemoActorCounterImpl>(DemoActorCounterImpl, client);
@@ -163,7 +180,7 @@ describe('http/actors', () => {
163180
expect(res0).toEqual(0);
164181

165182
// Now we wait for dueTime (2s)
166-
await (new Promise(resolve => setTimeout(resolve, 2000)));
183+
await NodeJSUtil.sleep(2000);
167184

168185
// After that the reminder callback will be called
169186
// In our case, the callback increments the count attribute

test/e2e/main.http.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CommunicationProtocolEnum, DaprClient, DaprServer, HttpMethod } from '../../src';
2+
import DemoActorCounterImpl from '../actor/DemoActorCounterImpl';
23

34
const serverHost = '127.0.0.1';
45
const serverPort = '50001';
@@ -37,6 +38,22 @@ describe('http/main', () => {
3738
await client.stop();
3839
});
3940

41+
describe('general', () => {
42+
it('should allow us to use promises as some testing libraries have issues within the Node Ecosystem', async () => {
43+
const mock = jest.fn(() => {
44+
return new Promise((resolve) => setTimeout(resolve, 500));
45+
});
46+
47+
const handler = jest.fn();
48+
49+
await mock().then(handler);
50+
51+
await (new Promise(resolve => setTimeout(resolve, 2000)));
52+
53+
expect(handler).toHaveBeenCalled();
54+
});
55+
});
56+
4057
describe('metadata', () => {
4158
it('should be able to get the metadata of the Dapr sidecar', async () => {
4259
const res = await client.metadata.get();

test/unit/main/general.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
describe('General', () => {
2+
it('should allow us to use promises as some testing libraries have issues within the Node Ecosystem', async () => {
3+
const mock = jest.fn(() => {
4+
return new Promise((resolve) => setTimeout(resolve, 500));
5+
});
6+
7+
const handler = jest.fn();
8+
9+
await mock().then(handler);
10+
11+
await (new Promise(resolve => setTimeout(resolve, 2000)));
12+
13+
expect(handler).toHaveBeenCalled();
14+
});
15+
});

0 commit comments

Comments
 (0)