Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Commit

Permalink
Added/Updated some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ckyycc committed Mar 2, 2019
1 parent 263f480 commit 2592d01
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
56 changes: 51 additions & 5 deletions test/Pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe('Pool', function () {
describe('#addResourceToAvailable', function () {
let resource;
beforeEach(() => {
resource = new Resource();
resource = new Resource(undefined);
});
it('should add the resource to available resource list', function () {
pool.addResourceToAvailable(resource).catch(() => '');
Expand Down Expand Up @@ -342,7 +342,7 @@ describe('Pool', function () {
describe('#replacePlaceHolderWithConnectionFromAll', function () {
let resource;
beforeEach(() => {
resource = new Resource();
resource = new Resource(undefined);
});
it('should replace the placeholder with the provided resource from the pool', function () {
const placeHolder = Symbol('PlaceHolder4Creation');
Expand All @@ -354,14 +354,22 @@ describe('Pool', function () {
});
});

it('#should not call _notifyAllOperators once', function () {
it('#should call _notifyAllOperators once', function () {
const placeHolder = Symbol('PlaceHolder4Creation');
pool['_allResources'].push(placeHolder);
spy = sinon.spy(pool, '_notifyAllOperators');
pool.replacePlaceHolderWithConnectionFromAll(placeHolder, resource).catch(() => '');
sinon.assert.calledOnce(spy);
});

it('#should not call _notifyAllOperators if resource is a Symbol as well', function () {
const placeHolder = Symbol('PlaceHolder4Creation');
pool['_allResources'].push(placeHolder);
spy = sinon.spy(pool, '_notifyAllOperators');
pool.replacePlaceHolderWithConnectionFromAll(placeHolder, Symbol('PlaceHolder4Creation')).catch(() => '');
sinon.assert.notCalled(spy);
});

it('#should return rejected promise if the placeHolder cannot be matched', function () {
const placeHolder = Symbol('PlaceHolder4Creation');
return pool.replacePlaceHolderWithConnectionFromAll(placeHolder, resource).catch(() => {
Expand Down Expand Up @@ -462,6 +470,27 @@ describe('Pool', function () {
should(pool.isPoolInitialized()).equals(pool['_initializeFlag']);
});
});

// describe('#_checkConnectionIdleTimeout', function () {
// const time = 500;
// let clock;
// before(function() {
// clock = sinon.useFakeTimers();
// });
// after(function() {
// clock.restore();
// });
// it('#shoud emit error if _notifyAllOperators failed', function () {
// const errorMsg = 'Error_NotifyAllOperators';
// stub = Stub.getStubForObjectWithRejectedPromise(pool, '_notifyAllOperators', errorMsg);
// spy = sinon.spy(Utils, 'emitMessage');
// pool['_checkConnectionIdleTimeout'](time);
// clock.tick(500);
// sinon.assert.calledOnce(spy);
// // sinon.assert.calledWith(spy, EventType.ERROR);
// });
// });

describe('#_notifyAllOperators', function () {
let stubList;
beforeEach(() => {
Expand All @@ -478,14 +507,31 @@ describe('Pool', function () {
should(str).equals(Stub.resolvedMessage);
});
});
it('#should reject if no taskFound.', function () {
// `Something wrong, can not find any worker for task ${task && task.taskType ? task.taskType : 'None'}`

it('#should reject if no taskFound (task is undefined).', function () {
const rejectError = 'it was not supposed to succeed.';
pool['_operators'].forEach((operator) => {
stubList.push(Stub.getStubForOperatorWithObject(operator, 'work', null));
});
return pool['_notifyAllOperators']()
.then(() => Promise.reject(rejectError))
.catch((err) => should.notStrictEqual(err, rejectError));
.catch((err) => {
should(err.message).equals('Something wrong, can not find any worker for task None');
});
});

it('#should reject if no taskFound (task is not undefined).', function () {
const rejectError = 'it was not supposed to succeed.';
pool['_operators'].forEach((operator) => {
stubList.push(Stub.getStubForOperatorWithObject(operator, 'work', null));
});
const Task = {taskType: 'JUST_FOR_TEST'};
return pool['_notifyAllOperators'](Task)
.then(() => Promise.reject(rejectError))
.catch((err) => {
should(err.message).equals('Something wrong, can not find any worker for task JUST_FOR_TEST');
});
});
});
});
10 changes: 10 additions & 0 deletions test/PoolOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ describe('PoolOperator', function () {
});

describe('#_checkIdleTimeout', function () {
it('#should not touch the resource if it is not timeout', function () {
const resource1 = new Resource({});
resource1['_lastIdleTime'] = Date.now();
operator.pool['_availableResources'].push(resource1);
stub1 = Stub.getStubForObjectWithResolvedPromise(operator, 'destroy');
stub2 = Stub.getStubForObjectWithResolvedPromise(operator, '_ensureMinPoolResources');
return operator['_checkIdleTimeout']().then(() => {
sinon.assert.notCalled(stub1);
});
});
it('#should try to destroy all (3) the resources from the pool (total is 5) if idle timeout.', function () {
const timeout = operator.pool.options.idleTimeout + 300000;

Expand Down
8 changes: 8 additions & 0 deletions test/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ describe('Request', function () {
request['_setTimeout']('AA');
sinon.assert.calledOnce(spy);
});
it('#should not do anything if delay is not number and state is not in pending', function () {
const request = new Request(1);
request.promise.catch(() => '');
request['_state'] = RequestState.REJECTED;
spy = sinon.spy(request, 'reject');
request['_setTimeout']('AA');
sinon.assert.notCalled(spy);
});
it('#should call reject is delay is less than 0', function () {
const request = new Request(1);
request.promise.catch(() => '');
Expand Down

0 comments on commit 2592d01

Please sign in to comment.