Skip to content

Commit

Permalink
Add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
d-matsui committed Jan 14, 2021
1 parent b55ae57 commit 2de41a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/peer/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class Socket extends EventEmitter {
try {
serverInfo = await this._getSignalingServer();
} catch (err) {
this._connectToNewServer(++numAttempts);
// Call with await to ensure that this method attempts up to the limit before giving up in a test case.
await this._connectToNewServer(++numAttempts);
return;
}

Expand Down
27 changes: 27 additions & 0 deletions tests/peer/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,31 @@ describe('Socket', () => {
});
});
});

describe('_connectToNewServer', () => {
const errorMessage = 'Could not connect to server.';
let connectToNewServerSpy;
let emitStub;
let getSignalingServerStub;

beforeEach(() => {
connectToNewServerSpy = sinon.spy(socket, '_connectToNewServer');
emitStub = sinon.stub(socket, 'emit');
getSignalingServerStub = sinon.stub(socket, '_getSignalingServer');
getSignalingServerStub.returns(Promise.reject(new Error('error')));
});

afterEach(() => {
connectToNewServerSpy.restore();
emitStub.restore();
getSignalingServerStub.restore();
});

it('should attempt up to 11 times before giving up and emitting an error on the socket', async () => {
await socket._connectToNewServer();

assert.equal(connectToNewServerSpy.callCount, 11);
assert.deepEqual(emitStub.args[0], ['error', errorMessage]);
});
});
});

0 comments on commit 2de41a1

Please sign in to comment.