Skip to content

Commit 47a3389

Browse files
committed
Update cross-region tests, await socket close on close()
1 parent 15b7a01 commit 47a3389

File tree

3 files changed

+167
-212
lines changed

3 files changed

+167
-212
lines changed

src/boilingdata/boilingdata.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class BoilingData {
132132
private creds?: BDCredentials;
133133
private socketInstance: ISocketInstance;
134134
private logger: Console;
135+
private closedPromise?: Promise<void>;
135136

136137
constructor(public props: IBoilingData) {
137138
this.logger = createLogger({ name: "boilingdata", logLevel: this.props.logLevel ?? "info" });
@@ -173,43 +174,47 @@ export class BoilingData {
173174

174175
public async close(): Promise<void> {
175176
this.socketInstance.socket?.close(1000);
177+
if (this.closedPromise) await this.closedPromise;
176178
}
177179

178180
public async connect(): Promise<void> {
179181
return new Promise((resolve, reject) => {
180182
const sock = this.socketInstance;
181183
const cbs = this.props.globalCallbacks;
182-
getBoilingDataCredentials(
183-
this.props.username,
184-
this.props.password,
185-
this.region,
186-
this.props.endpointUrl,
187-
this.props.mfa,
188-
this.props.authcontext,
189-
this.logger,
190-
)
191-
.then(creds => {
192-
this.creds = creds;
193-
sock.socket = new WebSocket(this.creds.signedWebsocketUrl);
194-
sock.socket!.onclose = () => {
195-
if (cbs?.onSocketClose) cbs.onSocketClose();
196-
};
197-
sock.socket!.onopen = () => {
198-
if (cbs?.onSocketOpen) cbs.onSocketOpen();
199-
resolve();
200-
};
201-
sock.socket!.onerror = (err: any) => {
202-
this.logger.error(err);
184+
this.closedPromise = new Promise<void>(closeResolve => {
185+
getBoilingDataCredentials(
186+
this.props.username,
187+
this.props.password,
188+
this.region,
189+
this.props.endpointUrl,
190+
this.props.mfa,
191+
this.props.authcontext,
192+
this.logger,
193+
)
194+
.then(creds => {
195+
this.creds = creds;
196+
sock.socket = new WebSocket(this.creds.signedWebsocketUrl);
197+
sock.socket!.onclose = () => {
198+
if (cbs?.onSocketClose) cbs.onSocketClose();
199+
closeResolve();
200+
};
201+
sock.socket!.onopen = () => {
202+
if (cbs?.onSocketOpen) cbs.onSocketOpen();
203+
resolve();
204+
};
205+
sock.socket!.onerror = (err: any) => {
206+
this.logger.error(err);
207+
reject(err);
208+
};
209+
sock.socket!.onmessage = (msg: MessageEvent) => {
210+
return this.handleSocketMessage(msg);
211+
};
212+
})
213+
.catch(err => {
214+
console.error(err);
203215
reject(err);
204-
};
205-
sock.socket!.onmessage = (msg: MessageEvent) => {
206-
return this.handleSocketMessage(msg);
207-
};
208-
})
209-
.catch(err => {
210-
console.error(err);
211-
reject(err);
212-
});
216+
});
217+
});
213218
});
214219
}
215220

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`BoilingData in all North-America and Europe AWS Regions runs query succesfully in other regions too 1`] = `
4+
Array [
5+
Array [
6+
Object {
7+
"birthdate": "3/8/1971",
8+
"cc": "6759521864920116",
9+
"comments": "1E+02",
10+
"country": "Indonesia",
11+
"email": "[email protected]",
12+
"first_name": "Amanda",
13+
"gender": "Female",
14+
"id": 1,
15+
"ip_address": "1.197.201.2",
16+
"last_name": "Jordan",
17+
"registration_dttm": "2016-02-03 07:55:29+00",
18+
"salary": 49756.53,
19+
"title": "Internal Auditor",
20+
},
21+
],
22+
]
23+
`;
24+
25+
exports[`BoilingData in all North-America and Europe AWS Regions runs query succesfully in other regions too 2`] = `
26+
Array [
27+
Array [
28+
Object {
29+
"birthdate": "3/8/1971",
30+
"cc": "6759521864920116",
31+
"comments": "1E+02",
32+
"country": "Indonesia",
33+
"email": "[email protected]",
34+
"first_name": "Amanda",
35+
"gender": "Female",
36+
"id": 1,
37+
"ip_address": "1.197.201.2",
38+
"last_name": "Jordan",
39+
"registration_dttm": "2016-02-03 07:55:29+00",
40+
"salary": 49756.53,
41+
"title": "Internal Auditor",
42+
},
43+
],
44+
]
45+
`;
46+
47+
exports[`BoilingData in all North-America and Europe AWS Regions runs query succesfully in other regions too 3`] = `
48+
Array [
49+
Array [
50+
Object {
51+
"birthdate": "3/8/1971",
52+
"cc": "6759521864920116",
53+
"comments": "1E+02",
54+
"country": "Indonesia",
55+
"email": "[email protected]",
56+
"first_name": "Amanda",
57+
"gender": "Female",
58+
"id": 1,
59+
"ip_address": "1.197.201.2",
60+
"last_name": "Jordan",
61+
"registration_dttm": "2016-02-03 07:55:29+00",
62+
"salary": 49756.53,
63+
"title": "Internal Auditor",
64+
},
65+
],
66+
]
67+
`;

0 commit comments

Comments
 (0)