Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

fix(NA): Non forced close without connection terminate and also some connection's flow improvements #197

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ export class SubscriptionClient {
public close(isForced = true) {
if (this.client !== null) {
this.forceClose = isForced;
this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_TERMINATE, null);

if (this.forceClose) {
this.sendMessage(undefined, MessageTypes.GQL_CONNECTION_TERMINATE, null);
}

this.client.close();
this.client = null;
}
Expand Down
46 changes: 45 additions & 1 deletion src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ new SubscriptionServer(onConnectErrorOptions, { server: httpServerWithOnConnectE
const httpServerWithDelay = createServer(notFoundRequestListener);
httpServerWithDelay.listen(DELAYED_TEST_PORT);
new SubscriptionServer(Object.assign({}, options, {
onSubscribe: (msg: OperationMessagePayload | any, params: SubscriptionOptions) => {
onSubscribe: (msg: OperationMessagePayload | any, params: SubscriptionOptions): any => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(Object.assign({}, params, { context: msg['context'] }));
Expand Down Expand Up @@ -1144,13 +1144,18 @@ describe('Client', function () {
reconnectionAttempts: 1,
});
const tryReconnectSpy = sinon.spy(subscriptionsClient, 'tryReconnect');
let receivedConnecitonTerminate = false;
wsServer.on('connection', (connection: any) => {
connection.on('message', (message: any) => {
const parsedMessage = JSON.parse(message);
// mock server
if (parsedMessage.type === MessageTypes.GQL_CONNECTION_INIT) {
connection.send(JSON.stringify({ type: MessageTypes.GQL_CONNECTION_ACK, payload: {} }));
}

if (parsedMessage.type === MessageTypes.GQL_CONNECTION_TERMINATE) {
receivedConnecitonTerminate = true;
}
});
});

Expand All @@ -1164,11 +1169,50 @@ describe('Client', function () {
};

setTimeout(() => {
expect(receivedConnecitonTerminate).to.be.equal(true);
expect(tryReconnectSpy.callCount).to.be.equal(0);
expect(subscriptionsClient.status).to.be.equal(WebSocket.CLOSED);
done();
}, 500);
});

it('should close the connection without sent connection terminate and reconnect', function (done) {
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`, {
reconnect: true,
reconnectionAttempts: 1,
});
const tryReconnectSpy = sinon.spy(subscriptionsClient, 'tryReconnect');
let receivedConnecitonTerminate = false;
wsServer.on('connection', (connection: any) => {
connection.on('message', (message: any) => {
const parsedMessage = JSON.parse(message);
// mock server
if (parsedMessage.type === MessageTypes.GQL_CONNECTION_INIT) {
connection.send(JSON.stringify({ type: MessageTypes.GQL_CONNECTION_ACK, payload: {} }));
}

if (parsedMessage.type === MessageTypes.GQL_CONNECTION_TERMINATE) {
receivedConnecitonTerminate = true;
}
});
});

const originalOnMessage = subscriptionsClient.client.onmessage;
subscriptionsClient.client.onmessage = (dataReceived: any) => {
let receivedDataParsed = JSON.parse(dataReceived.data);
if (receivedDataParsed.type === MessageTypes.GQL_CONNECTION_ACK) {
originalOnMessage(dataReceived);
subscriptionsClient.close(false);
}
};

setTimeout(() => {
expect(tryReconnectSpy.callCount).to.be.equal(1);
expect(subscriptionsClient.status).to.be.equal(WebSocket.OPEN);
expect(receivedConnecitonTerminate).to.be.equal(false);
done();
}, 500);
});
});

describe('Server', function () {
Expand Down