Skip to content

Commit

Permalink
Cache test data to be sent until WebSocket connection gets establised
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Kakonyi committed Nov 16, 2023
1 parent bd82093 commit c4f4d01
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ export default class Reporter {
constructor() {
// Set the Reporter type to realtime.
this.type = 'realtime';
this.cachedTestData = [];
}

// Internal: Creates a websocket connection to the cavy-cli server.
onStart() {
const url = 'ws://127.0.0.1:8082/';
this.ws = new WebSocket(url);
this.ws = new WebSocket(url, undefined, {handshakeTimeout: 1000});
this.ws.onopen = () => {
while (testData = this.cachedTestData.shift()) {
this.sendData(testData);
}
};
}

// Internal: Send a single test result to cavy-cli over the websocket connection.
send(result) {
if (this.websocketReady()) {
testData = { event: 'singleResult', data: result };
const testData = { event: 'singleResult', data: result };
if (this.websocketConnecting()) {
this.cachedTestData.push(testData);
} else if (this.websocketReady()) {
this.sendData(testData);
}
}

// Internal: Send report to cavy-cli over the websocket connection.
onFinish(report) {
if (this.websocketReady()) {
testData = { event: 'testingComplete', data: report };
const testData = { event: 'testingComplete', data: report };
if (this.websocketConnecting()) {
this.cachedTestData.push(testData);
} else if (this.websocketReady()) {
this.sendData(testData);
} else {
// If cavy-cli is not running, let people know in a friendly way
Expand All @@ -35,6 +45,12 @@ export default class Reporter {
}
}

// Private: Determines whether data can not be sent over the websocket yet.
websocketConnecting() {
// WebSocket.readyState 0 means the web socket connection is CONNECTING.
return this.ws.readyState == 0;
}

// Private: Determines whether data can be sent over the websocket.
websocketReady() {
// WebSocket.readyState 1 means the web socket connection is OPEN.
Expand All @@ -49,9 +65,7 @@ export default class Reporter {
console.log('Cavy test report successfully sent to cavy-cli');
}
} catch (e) {
console.group('Error sending test data');
console.warn(e.message);
console.groupEnd();
console.warn(`Error sending test data ${e.message} payload: ${JSON.stringify(testData)}`);
}
}
}

0 comments on commit c4f4d01

Please sign in to comment.