Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket: don't queue events on destroyed sockets. #11

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@ class Socket extends EventEmitter {
enforce(args.length > 0, 'event', 'string');
enforce(typeof args[0] === 'string', 'event', 'string');

if (this.destroyed)
return Promise.reject(new Error('Socket destroyed.'));

const id = this.sequence;

this.sequence += 1;
Expand Down
71 changes: 69 additions & 2 deletions test/socket-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ describe('Socket', () => {
assert.bufferEqual(barData, 'baz', 'ascii');
});

it('should close', (cb) => {
it('should not queue on destroyed socket', async () => {
socket.destroy();
await assert.rejects(socket.call('foo'), {
message: 'Socket destroyed.'
});
});

it('should close', (cb) => {
server.close(cb);
});
});
Expand All @@ -160,6 +166,8 @@ describe('Socket', () => {

let socket = null;
let barData = null;
let clientFailedCallDone = false;
let serverCallError = null;

it('should setup server', (cb) => {
io.attach(server);
Expand Down Expand Up @@ -190,6 +198,22 @@ describe('Socket', () => {
io.leave(socket, name);
io.to(name, 'test', 'testing again');
});

socket.bind('trigger client hook and event', async (data) => {
await socket.call('client hook', data);
await socket.fire('client event', data);
});

socket.hook('socket call back with delay', async (n) => {
await timeout(n);
try {
await socket.call('call response');
} catch (e) {
serverCallError = e;
} finally {
clientFailedCallDone = true;
}
});
});

server.listen(8000, cb);
Expand Down Expand Up @@ -255,8 +279,51 @@ describe('Socket', () => {
assert.deepStrictEqual(json, obj);
});

it('should close', (cb) => {
it('should receive hook and event', async () => {
const hook = new Promise((resolve) => {
const hook = async (data) => {
resolve(data);
socket.unhook('client hook', hook);
return null;
};

socket.hook('client hook', hook);
});

const event = new Promise((resolve) => {
const event = async (data) => {
resolve(data);
socket.unbind('client event', event);
};

socket.bind('client event', event);
});

socket.fire('trigger client hook and event', 'hello');
assert.strictEqual(await hook, 'hello');
assert.strictEqual(await event, 'hello');
});

it('should not queue hook on destroyed socket (server)', async () => {
let callError = null;

socket
.call('socket call back with delay', 200)
.catch((err) => {
callError = err;
});

await timeout(100);
socket.destroy();
await timeout(150);

assert.strictEqual(callError.message, 'Job timed out.');
assert.strictEqual(clientFailedCallDone, true);
assert(serverCallError);
assert.strictEqual(serverCallError.message, 'Socket destroyed.');
});

it('should close', (cb) => {
server.close(cb);
});
});
Expand Down