Skip to content

Commit dbc8f8c

Browse files
author
Roland Kakonyi
committed
Cache test data to be sent until WebSocket connection gets established
1 parent bd82093 commit dbc8f8c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/Reporter.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,36 @@ export default class Reporter {
44
constructor() {
55
// Set the Reporter type to realtime.
66
this.type = 'realtime';
7+
this.cachedTestData = [];
78
}
89

910
// Internal: Creates a websocket connection to the cavy-cli server.
1011
onStart() {
1112
const url = 'ws://127.0.0.1:8082/';
1213
this.ws = new WebSocket(url);
14+
this.ws.onopen = () => {
15+
while (testData = this.cachedTestData.shift()) {
16+
this.sendData(testData);
17+
}
18+
};
1319
}
1420

1521
// Internal: Send a single test result to cavy-cli over the websocket connection.
1622
send(result) {
17-
if (this.websocketReady()) {
18-
testData = { event: 'singleResult', data: result };
23+
const testData = { event: 'singleResult', data: result };
24+
if (this.websocketConnecting()) {
25+
this.cachedTestData.push(testData);
26+
} else if (this.websocketReady()) {
1927
this.sendData(testData);
2028
}
2129
}
2230

2331
// Internal: Send report to cavy-cli over the websocket connection.
2432
onFinish(report) {
25-
if (this.websocketReady()) {
26-
testData = { event: 'testingComplete', data: report };
33+
const testData = { event: 'testingComplete', data: report };
34+
if (this.websocketConnecting()) {
35+
this.cachedTestData.push(testData);
36+
} else if (this.websocketReady()) {
2737
this.sendData(testData);
2838
} else {
2939
// If cavy-cli is not running, let people know in a friendly way
@@ -35,6 +45,12 @@ export default class Reporter {
3545
}
3646
}
3747

48+
// Private: Determines whether data can not be sent over the websocket yet.
49+
websocketConnecting() {
50+
// WebSocket.readyState 0 means the web socket connection is CONNECTING.
51+
return this.ws.readyState == 0;
52+
}
53+
3854
// Private: Determines whether data can be sent over the websocket.
3955
websocketReady() {
4056
// WebSocket.readyState 1 means the web socket connection is OPEN.
@@ -49,9 +65,7 @@ export default class Reporter {
4965
console.log('Cavy test report successfully sent to cavy-cli');
5066
}
5167
} catch (e) {
52-
console.group('Error sending test data');
53-
console.warn(e.message);
54-
console.groupEnd();
68+
console.warn(`Error sending test data ${e.message} payload: ${JSON.stringify(testData)}`);
5569
}
5670
}
5771
}

0 commit comments

Comments
 (0)