Skip to content

Commit

Permalink
Fix some tests about due to wrong usage of fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
srknzl committed Apr 13, 2022
1 parent a5b1a90 commit b02003f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions test/unit/proxy/cpsubsystem/CPSessionManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ describe('CPSessionManagerTest', function () {
});

it('isValid: should consider current acquire count', function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
const state = new SessionState(Long.fromNumber(42), null, 1000);

// session should be expired now
sandbox.useFakeTimers(2000);
clock.tick(2000);
expect(state.isValid()).to.be.false;

state.acquire(1);
expect(state.isValid()).to.be.true;
});

it('isValid: should consider current time', function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
const state = new SessionState(Long.fromNumber(42), null, 1000);

expect(state.isValid()).to.be.true;

// session should be expired now
sandbox.useFakeTimers(2000);
clock.tick(2000);
expect(state.isValid()).to.be.false;
});
});
Expand Down
8 changes: 4 additions & 4 deletions test/unit/proxy/cpsubsystem/FencedLockProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,27 @@ describe('FencedLockProxyTest', function () {
});

it('tryLock: should return undefined on expired session error and no timeout', async function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
prepareAcquireSession(1);
stubRequestTryLock(2, new SessionExpiredError());

const promise = proxy.tryLock();
// advance time as if requests were real
sandbox.useFakeTimers(100);
clock.tick(100);
const fence = await promise;

expect(fence).to.be.undefined;
expect(cpSessionManagerStub.invalidateSession.calledOnce).to.be.true;
});

it('tryLock: should keep trying on expired session error and specified timeout', async function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
prepareAcquireSession(1);
stubRequestTryLock(2, new SessionExpiredError());

const promise = proxy.tryLock(1000);
// advance time as if requests were real
sandbox.useFakeTimers(100);
clock.tick(100);
const fence = await promise;

expect(fence.toNumber()).to.be.equal(2);
Expand Down
8 changes: 4 additions & 4 deletions test/unit/proxy/cpsubsystem/SessionAwareSemaphoreProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,27 +172,27 @@ describe('SessionAwareSemaphoreProxyTest', function () {
});

it('tryAcquire: should return false on expired session error and no timeout', async function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
prepareAcquireSession(1);
stubRequestAcquire(true, new SessionExpiredError());

const promise = proxy.tryAcquire(1);
// advance time as if requests were real
sandbox.useFakeTimers(100);
clock.tick(100);
const result = await promise;

expect(result).to.be.false;
expect(cpSessionManagerStub.invalidateSession.calledOnce).to.be.true;
});

it('tryAcquire: should keep trying on expired session error and specified timeout', async function () {
sandbox.useFakeTimers(0);
const clock = sandbox.useFakeTimers();
prepareAcquireSession(1);
stubRequestAcquire(true, new SessionExpiredError());

const promise = proxy.tryAcquire(1, 1000);
// advance time as if requests were real
sandbox.useFakeTimers(100);
clock.tick(100);
const result = await promise;

expect(result).to.be.true;
Expand Down

0 comments on commit b02003f

Please sign in to comment.