Skip to content

Commit

Permalink
Merge pull request #2254 from huridocs/2229-socket-update-error
Browse files Browse the repository at this point in the history
Fix error when updating session sockets on multiple clients concurrently
  • Loading branch information
daneryl authored Mar 20, 2019
2 parents 1749f9d + eb5e4aa commit 0fb4c56
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/api/socketio/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default (server, app) => {
});

app.use((req, res, next) => {
req.io.getCurrentSessionSockets = () => {
req.getCurrentSessionSockets = () => {
const sessionSockets = {
sockets: [],
emit(...args) {
Expand Down
17 changes: 14 additions & 3 deletions app/api/socketio/specs/middleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,29 @@ describe('socketio middleware', () => {
});

it('should find and return an array of sockets belonging to the current cookie', () => {
let result = req.io.getCurrentSessionSockets();
let result = req.getCurrentSessionSockets();
expect(result.sockets[0]).toBe(socket1);
expect(result.sockets[1]).toBe(socket3);

req.session.id = 'sessionId2';
executeMiddleware(req, res, next);
result = req.io.getCurrentSessionSockets();
result = req.getCurrentSessionSockets();
expect(result.sockets[0]).toBe(socket2);
});

it('should isolate sockets for each requests when multiple requests are issued', () => {
const req1 = { ...req };
const req2 = { ...req, session: { id: 'sessionId2' } };
executeMiddleware(req1, res, next);
executeMiddleware(req2, res, next);
const req1Result = req1.getCurrentSessionSockets();
const req2Result = req2.getCurrentSessionSockets();
expect(req1Result.sockets).toEqual([socket1, socket3]);
expect(req2Result.sockets).toEqual([socket2]);
});

it('should include in the result an "emit" function that emits to all the found sockets the sent message', () => {
const result = req.io.getCurrentSessionSockets();
const result = req.getCurrentSessionSockets();
const data = { data: 'data' };

result.emit('Message', data);
Expand Down
6 changes: 3 additions & 3 deletions app/api/upload/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default (app) => {

const file = req.files[0].destination + req.files[0].filename;

const sessionSockets = req.io.getCurrentSessionSockets();
const sessionSockets = req.getCurrentSessionSockets();
sessionSockets.emit('conversionStart', req.body.document);
debugLog.debug(`Starting conversion of: ${req.files[0].originalname}`);
return Promise.all([
Expand Down Expand Up @@ -106,7 +106,7 @@ export default (app) => {
.then(() => {
debugLog.debug('Saving documents');
return entities.saveMultiple(docs.map(doc => ({ ...doc, file: { ...doc.file, timestamp: Date.now() } }))).then(() => {
const sessionSockets = req.io.getCurrentSessionSockets();
const sessionSockets = req.getCurrentSessionSockets();
sessionSockets.emit('documentProcessed', req.body.document);
});
});
Expand All @@ -120,7 +120,7 @@ export default (app) => {
entities.saveMultiple(docs.map(doc => ({ ...doc, processed: false })));
});

const sessionSockets = req.io.getCurrentSessionSockets();
const sessionSockets = req.getCurrentSessionSockets();
sessionSockets.emit('conversionFailed', req.body.document);
});

Expand Down
11 changes: 9 additions & 2 deletions app/api/upload/specs/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe('upload routes', () => {
spyOn(search, 'delete').and.returnValue(Promise.resolve());
spyOn(entities, 'indexEntities').and.returnValue(Promise.resolve());
iosocket = jasmine.createSpyObj('socket', ['emit']);
const io = { getCurrentSessionSockets: () => ({ sockets: [iosocket], emit: iosocket.emit }) };
routes = instrumentRoutes(uploadRoutes);
file = {
fieldname: 'file',
Expand All @@ -82,7 +81,15 @@ describe('upload routes', () => {
path: `${__dirname}/uploads/f2082bf51b6ef839690485d7153e847a.pdf`,
size: 171411271
};
req = { language: 'es', user: 'admin', headers: {}, body: { document: 'sharedId1' }, files: [file], io };
req = {
language: 'es',
user: 'admin',
headers: {},
body: { document: 'sharedId1' },
files: [file],
io: {},
getCurrentSessionSockets: () => ({ sockets: [iosocket], emit: iosocket.emit })
};

db.clearAllAndLoad(fixtures).then(done).catch(catchErrors(done));
spyOn(errorLog, 'error'); //just to avoid annoying console output
Expand Down

0 comments on commit 0fb4c56

Please sign in to comment.