Skip to content

Commit

Permalink
Merge pull request #1373 from RoundingWell/websocket-close
Browse files Browse the repository at this point in the history
Restart closed websocket if still subscribed
  • Loading branch information
paulfalgout authored Jan 3, 2025
2 parents b23d218 + 116503f commit 8bcb08f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/js/entities-service/entities/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Backbone from 'backbone';
import { contains, extend, keys, reduce, size } from 'underscore';
import { contains, extend, keys, reduce, size, union } from 'underscore';
import Radio from 'backbone.radio';
import Store from 'backbone.store';
import dayjs from 'dayjs';
Expand Down Expand Up @@ -250,7 +250,7 @@ const _Model = BaseModel.extend({
return !!size(programAction.get('allowed_uploads'));
},
addFile(resource) {
const newFilesRelationship = [...this.get('_files'), { id: resource.id, type: 'files' }];
const newFilesRelationship = union(this.get('_files'), [{ id: resource.id, type: 'files' }]);

this.set({ _files: newFilesRelationship });
},
Expand Down
47 changes: 39 additions & 8 deletions src/js/services/ws.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ let service;
const clientKey = 'clientKey';

Cypress.Commands.add('startService', () => {
const startSpy = cy.spy().as('startService');

service.on('start', startSpy);

// Return a promise that resolves when the service starts
return new Cypress.Promise(resolve => {
service.on('start', resolve);
service.once('start', resolve);
service.start();
});
});
Expand Down Expand Up @@ -80,26 +85,52 @@ context('WS Service', function() {
specify('Restarting a closed socket', function() {
const channel = Radio.channel('ws');

const closedTest = { name: 'SendTest', data: 'CLOSED' };
const closedTest = { id: 'foo', type: 'bar' };
const addTest = { id: 'foo2', type: 'bar2' };

const notification = {
state: {},
data: {
name: 'Subscribe',
data: {
clientKey: 'clientKey',
resources: [closedTest],
},
},
};

cy
.startService()
.then(() => {
cy.stub(service, 'start').as('start');

return new Cypress.Promise(resolve => {
service.ws.addEventListener('close', () => {
channel.request('send', closedTest);
channel.request('subscribe', closedTest);
resolve();
});
service.ws.close();
});
});

cy
.get('@start')
.should('have.been.calledOnce')
.and('have.been.calledWith', { state: {}, data: closedTest });
.get('@startService')
.should('be.calledTwice')
.then(spy => {
const secondCall = spy.getCall(1);
expect(secondCall.args[0]).to.deep.equal(notification);
})
.then(() => {
channel.request('add', addTest);
service.ws.close();
});

cy
.get('@startService')
.should('be.calledThrice')
.then(spy => {
const thirdCall = spy.getCall(2);
notification.data.data.resources.push(addTest);
expect(thirdCall.args[0]).to.deep.equal(notification);
});
});

specify('Message handling', function() {
Expand Down
1 change: 1 addition & 0 deletions src/js/services/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export default App.extend({

onClose() {
this.stopHeartbeat();
if (this.resources.length) this._subscribe();
},

onMessage(event) {
Expand Down

0 comments on commit 8bcb08f

Please sign in to comment.