Skip to content

Commit

Permalink
Add connectionAttemptFailed event.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwippermann committed Jan 21, 2024
1 parent 82b4907 commit 54f2f93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/tcp-connection-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ class TcpConnectionEndpoint extends EventEmitter {

const callback = function(err, result, transition) {
if (err) {
_this.emit('connectionAttemptFailed', {
ip: socket.remoteAddress,
family: socket.remoteFamily,
port: socket.remotePort,
error: err,
});

write('-ERROR: ' + JSON.stringify(err.toString()) + '\r\n');
} else {
write(result.toString() + '\r\n');
Expand Down
20 changes: 20 additions & 0 deletions test/specs/tcp-connection-endpoint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ describe('TcpConnectionEndpoint', () => {

it('should work correctly with a rejecting viaTag verifier', async () => {
await runTestableEndpointAndSocket(async ({ ep, socket, read }) => {
const onConnectionAttemptFailed = jest.fn();

ep.on('connectionAttemptFailed', onConnectionAttemptFailed);

const endPromise = new Promise(resolve => {
socket.once('end', () => {
resolve();
Expand All @@ -365,6 +369,22 @@ describe('TcpConnectionEndpoint', () => {

socket.end();

expect(onConnectionAttemptFailed.mock.calls).toHaveLength(1);
expect(onConnectionAttemptFailed.mock.calls [0]).toHaveLength(1);

const ev1 = onConnectionAttemptFailed.mock.calls [0] [0];

expectOwnPropertyNamesToEqual(ev1, [
'ip',
'port',
'family',
'error',
]);
expect(typeof ev1.ip).toBe('string');
expect(typeof ev1.port).toBe('number');
expect(ev1.family).toBe('IPv6');
expect(ev1.error.message).toBe('Unknown viaTag: unknownViaTag');

await endPromise;
});
});
Expand Down

0 comments on commit 54f2f93

Please sign in to comment.