Skip to content

Commit

Permalink
Merge PR #11 from 'nodech/noqueue-on-destroy'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Oct 31, 2023
2 parents 7006e0a + d9e0a95 commit 57a5fe0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
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

0 comments on commit 57a5fe0

Please sign in to comment.