Skip to content

Commit bf647e7

Browse files
committed
Localize all global clock and timer mocks
1 parent ca8739d commit bf647e7

File tree

5 files changed

+115
-112
lines changed

5 files changed

+115
-112
lines changed

test/driver.test.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { ServerVersion, VERSION_4_0_0 } from '../src/internal/server-version'
2828
import testUtils from './internal/test-utils'
2929

3030
describe('#integration driver', () => {
31-
let clock
3231
let driver
3332
let serverVersion
3433

@@ -42,10 +41,6 @@ describe('#integration driver', () => {
4241
})
4342

4443
afterEach(() => {
45-
if (clock) {
46-
clock.uninstall()
47-
clock = null
48-
}
4944
if (driver) {
5045
driver.close()
5146
driver = null
@@ -295,38 +290,38 @@ describe('#integration driver', () => {
295290
})
296291
})
297292

298-
it('should discard old connections', done => {
293+
it('should discard old connections', async () => {
299294
const maxLifetime = 100000
300295
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {
301296
maxConnectionLifetime: maxLifetime
302297
})
303298

304299
const session1 = driver.session()
305-
session1.run('CREATE () RETURN 42').then(() => {
306-
session1.close()
300+
await session1.run('CREATE () RETURN 42')
301+
session1.close()
307302

308-
// one connection should be established
309-
const connections1 = openConnectionFrom(driver)
310-
expect(connections1.length).toEqual(1)
303+
// one connection should be established
304+
const connections1 = openConnectionFrom(driver)
305+
expect(connections1.length).toEqual(1)
311306

312-
// make existing connection look very old by advancing the `Date.now()` value
313-
const currentTime = Date.now()
314-
clock = lolex.install()
307+
// make existing connection look very old by advancing the `Date.now()` value
308+
const currentTime = Date.now()
309+
const clock = lolex.install()
310+
try {
315311
clock.setSystemTime(currentTime + maxLifetime * 2)
316312

317313
const session2 = driver.session()
318-
session2.run('RETURN 1').then(() => {
319-
session2.close()
314+
await session2.run('RETURN 1')
315+
session2.close()
320316

321-
// old connection should be disposed and new one should be created
322-
const connections2 = openConnectionFrom(driver)
323-
expect(connections2.length).toEqual(1)
317+
// old connection should be disposed and new one should be created
318+
const connections2 = openConnectionFrom(driver)
319+
expect(connections2.length).toEqual(1)
324320

325-
expect(connections1[0]).not.toEqual(connections2[0])
326-
327-
done()
328-
})
329-
})
321+
expect(connections1[0]).not.toEqual(connections2[0])
322+
} finally {
323+
clock.uninstall()
324+
}
330325
})
331326

332327
const exposedTypes = [

test/internal/connection-channel.test.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,9 @@ const FAILURE_MESSAGE = { signature: 0x7f, fields: [newError('Hello')] }
4141
const RECORD_MESSAGE = { signature: 0x71, fields: [{ value: 'Hello' }] }
4242

4343
describe('#integration ChannelConnection', () => {
44-
let clock
4544
let connection
4645

4746
afterEach(done => {
48-
if (clock) {
49-
clock.uninstall()
50-
clock = null
51-
}
52-
5347
const usedConnection = connection
5448
connection = null
5549
if (usedConnection) {
@@ -59,12 +53,16 @@ describe('#integration ChannelConnection', () => {
5953
})
6054

6155
it('should have correct creation timestamp', () => {
62-
clock = lolex.install()
63-
clock.setSystemTime(424242)
56+
const clock = lolex.install()
57+
try {
58+
clock.setSystemTime(424242)
6459

65-
connection = createConnection('bolt://localhost')
60+
connection = createConnection('bolt://localhost')
6661

67-
expect(connection.creationTimestamp).toEqual(424242)
62+
expect(connection.creationTimestamp).toEqual(424242)
63+
} finally {
64+
clock.uninstall()
65+
}
6866
})
6967

7068
it('should read/write basic messages', done => {
@@ -231,12 +229,12 @@ describe('#integration ChannelConnection', () => {
231229
})
232230
})
233231

234-
it('should respect connection timeout', done => {
235-
testConnectionTimeout(false, done)
232+
it('should respect connection timeout', async () => {
233+
await testConnectionTimeout(false)
236234
})
237235

238-
it('should respect encrypted connection timeout', done => {
239-
testConnectionTimeout(true, done)
236+
it('should respect encrypted connection timeout', async () => {
237+
await testConnectionTimeout(true)
240238
})
241239

242240
it('should not queue INIT observer when broken', done => {
@@ -471,30 +469,38 @@ describe('#integration ChannelConnection', () => {
471469
}
472470
}
473471

474-
function testConnectionTimeout (encrypted, done) {
475-
const boltUri = 'bolt://10.0.0.0' // use non-routable IP address which never responds
476-
connection = createConnection(
477-
boltUri,
478-
{ encrypted: encrypted, connectionTimeout: 1000 },
479-
'TestErrorCode'
480-
)
481-
482-
connection
483-
.connect('mydriver/0.0.0', basicAuthToken())
484-
.then(() => done.fail('Should not be able to connect'))
485-
.catch(error => {
486-
expect(error.code).toEqual('TestErrorCode')
472+
async function testConnectionTimeout (encrypted) {
473+
const clock = jasmine.clock()
474+
clock.install()
475+
476+
try {
477+
const boltUri = 'bolt://10.0.0.0' // use non-routable IP address which never responds
478+
connection = createConnection(
479+
boltUri,
480+
{ encrypted: encrypted, connectionTimeout: 1000 },
481+
'TestErrorCode'
482+
)
483+
484+
clock.tick(1001)
485+
486+
await connection.connect('mydriver/0.0.0', basicAuthToken())
487+
} catch (error) {
488+
expect(error.code).toEqual('TestErrorCode')
489+
490+
// in some environments non-routable address results in immediate 'connection refused' error and connect
491+
// timeout is not fired; skip message assertion for such cases, it is important for connect attempt to not hang
492+
if (error.message.indexOf('Failed to establish connection') === 0) {
493+
expect(error.message).toEqual(
494+
'Failed to establish connection in 1000ms'
495+
)
496+
}
487497

488-
// in some environments non-routable address results in immediate 'connection refused' error and connect
489-
// timeout is not fired; skip message assertion for such cases, it is important for connect attempt to not hang
490-
if (error.message.indexOf('Failed to establish connection') === 0) {
491-
expect(error.message).toEqual(
492-
'Failed to establish connection in 1000ms'
493-
)
494-
}
498+
return
499+
} finally {
500+
clock.uninstall()
501+
}
495502

496-
done()
497-
})
503+
expect(false).toBeTruthy('exception expected')
498504
}
499505

500506
function testQueueingOfObserversWithBrokenConnection (connectionAction, done) {

test/internal/connection-provider-pooled.test.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ import FakeConnection from './fake-connection'
2121
import lolex from 'lolex'
2222

2323
describe('#unit PooledConnectionProvider', () => {
24-
let clock
25-
26-
beforeEach(() => {
27-
if (clock) {
28-
clock.uninstall()
29-
clock = null
30-
}
31-
})
32-
3324
it('should treat closed connections as invalid', () => {
3425
const provider = new PooledConnectionProvider({
3526
id: 0,
@@ -52,11 +43,15 @@ describe('#unit PooledConnectionProvider', () => {
5243
})
5344

5445
const connection = new FakeConnection().withCreationTimestamp(12)
55-
clock = lolex.install()
56-
clock.setSystemTime(20)
57-
const connectionValid = provider._validateConnection(connection)
46+
const clock = lolex.install()
47+
try {
48+
clock.setSystemTime(20)
49+
const connectionValid = provider._validateConnection(connection)
5850

59-
expect(connectionValid).toBeTruthy()
51+
expect(connectionValid).toBeTruthy()
52+
} finally {
53+
clock.uninstall()
54+
}
6055
})
6156

6257
it('should treat old open connections as invalid', () => {
@@ -68,10 +63,14 @@ describe('#unit PooledConnectionProvider', () => {
6863
})
6964

7065
const connection = new FakeConnection().withCreationTimestamp(5)
71-
clock = lolex.install()
72-
clock.setSystemTime(20)
73-
const connectionValid = provider._validateConnection(connection)
66+
const clock = lolex.install()
67+
try {
68+
clock.setSystemTime(20)
69+
const connectionValid = provider._validateConnection(connection)
7470

75-
expect(connectionValid).toBeFalsy()
71+
expect(connectionValid).toBeFalsy()
72+
} finally {
73+
clock.uninstall()
74+
}
7675
})
7776
})

test/internal/node/routing.driver.boltkit.test.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import ServerAddress from '../../../src/internal/server-address'
2727

2828
describe('#stub-routing routing driver with stub server', () => {
2929
let originalTimeout
30-
let clock
3130

3231
beforeAll(() => {
3332
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
@@ -1586,18 +1585,19 @@ describe('#stub-routing routing driver with stub server', () => {
15861585
const driver = boltStub.newDriver('neo4j://127.0.0.1:9001')
15871586
const session = driver.session()
15881587

1588+
let clock
15891589
let invocations = 0
15901590
const resultPromise = session.readTransaction(tx => {
15911591
invocations++
15921592
if (invocations === 2) {
15931593
// make retries stop after two invocations
1594-
moveTime30SecondsForward()
1594+
clock = moveTime30SecondsForward()
15951595
}
15961596
return tx.run('MATCH (n) RETURN n.name')
15971597
})
15981598

15991599
resultPromise.catch(error => {
1600-
removeTimeMocking() // uninstall lolex mocking to make test complete, boltkit uses timers
1600+
removeTimeMocking(clock) // uninstall lolex mocking to make test complete, boltkit uses timers
16011601

16021602
expect(error.code).toEqual(SESSION_EXPIRED)
16031603
expect(invocations).toEqual(2)
@@ -1642,18 +1642,19 @@ describe('#stub-routing routing driver with stub server', () => {
16421642
const driver = boltStub.newDriver('neo4j://127.0.0.1:9001')
16431643
const session = driver.session()
16441644

1645+
let clock = null
16451646
let invocations = 0
16461647
const resultPromise = session.writeTransaction(tx => {
16471648
invocations++
16481649
if (invocations === 2) {
16491650
// make retries stop after two invocations
1650-
moveTime30SecondsForward()
1651+
clock = moveTime30SecondsForward()
16511652
}
16521653
return tx.run("CREATE (n {name:'Bob'})")
16531654
})
16541655

16551656
resultPromise.catch(error => {
1656-
removeTimeMocking() // uninstall lolex mocking to make test complete, boltStub uses timers
1657+
removeTimeMocking(clock) // uninstall lolex mocking to make test complete, boltStub uses timers
16571658

16581659
expect(error.code).toEqual(SESSION_EXPIRED)
16591660
expect(invocations).toEqual(2)
@@ -2939,14 +2940,14 @@ describe('#stub-routing routing driver with stub server', () => {
29392940

29402941
function moveTime30SecondsForward () {
29412942
const currentTime = Date.now()
2942-
clock = lolex.install()
2943+
const clock = lolex.install()
29432944
clock.setSystemTime(currentTime + 30 * 1000 + 1)
2945+
return clock
29442946
}
29452947

2946-
function removeTimeMocking () {
2948+
function removeTimeMocking (clock) {
29472949
if (clock) {
29482950
clock.uninstall()
2949-
clock = null
29502951
}
29512952
}
29522953

0 commit comments

Comments
 (0)