Skip to content

Commit 50f80c5

Browse files
committed
Removed driver.onError callbacks
1 parent 9d03115 commit 50f80c5

File tree

6 files changed

+128
-88
lines changed

6 files changed

+128
-88
lines changed

test/driver.test.js

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import neo4j from '../src'
2121
import sharedNeo4j from './internal/shared-neo4j'
22-
import FakeConnection from './internal/fake-connection'
2322
import lolex from 'lolex'
2423
import {
2524
DEFAULT_ACQUISITION_TIMEOUT,
@@ -65,20 +64,20 @@ describe('#integration driver', () => {
6564
driver.close()
6665
})
6766

68-
it('should handle connection errors', done => {
67+
it('should handle connection errors', async () => {
6968
// Given
7069
driver = neo4j.driver('bolt://local-host', sharedNeo4j.authToken)
71-
72-
// Expect
73-
driver.onError = error => {
74-
// the error message is different whether in browser or node
70+
const session = driver.session()
71+
const txc = session.beginTransaction()
72+
try {
73+
await txc.run('RETURN 1')
74+
expect(true).toBeFalsy('exception expected')
75+
} catch (error) {
7576
expect(error.message).not.toBeNull()
7677
expect(error.code).toEqual(neo4j.error.SERVICE_UNAVAILABLE)
77-
done()
78+
} finally {
79+
session.close()
7880
}
79-
80-
// When
81-
startNewTransaction(driver)
8281
}, 10000)
8382

8483
it('should fail with correct error message when connecting to port 80', done => {
@@ -131,19 +130,20 @@ describe('#integration driver', () => {
131130
}).toBeDefined()
132131
})
133132

134-
it('should fail early on wrong credentials', done => {
133+
it('should fail early on wrong credentials', async () => {
135134
// Given
136135
driver = neo4j.driver('bolt://localhost', wrongCredentials())
137-
138-
// Expect
139-
driver.onError = err => {
140-
// the error message is different whether in browser or node
141-
expect(err.code).toEqual('Neo.ClientError.Security.Unauthorized')
142-
done()
136+
const session = driver.session()
137+
const txc = session.beginTransaction()
138+
try {
139+
await txc.run('RETURN 1')
140+
expect(true).toBeFalsy('exception expected')
141+
} catch (error) {
142+
expect(error.message).not.toBeNull()
143+
expect(error.code).toEqual('Neo.ClientError.Security.Unauthorized')
144+
} finally {
145+
session.close()
143146
}
144-
145-
// When
146-
startNewTransaction(driver)
147147
})
148148

149149
it('should fail queries on wrong credentials', done => {
@@ -220,26 +220,25 @@ describe('#integration driver', () => {
220220
})
221221
})
222222

223-
it('should fail nicely when connecting with routing to standalone server', done => {
223+
it('should fail nicely when connecting with routing to standalone server', async () => {
224224
if (!routingProcedureOnlyAvailableOnCores()) {
225-
done()
226-
return
225+
return Promise.resolve(null)
227226
}
228227

229228
// Given
230229
driver = neo4j.driver('neo4j://localhost', sharedNeo4j.authToken)
231-
232-
// Expect
233-
driver.onError = error => {
230+
const session = driver.session()
231+
try {
232+
await session.run('RETURN 1')
233+
expect(true).toBeFalsy('exception expected')
234+
} catch (error) {
234235
expect(error.message).toContain(
235236
'Could not perform discovery. No routing servers available.'
236237
)
237238
expect(error.code).toEqual(neo4j.error.SERVICE_UNAVAILABLE)
238-
done()
239+
} finally {
240+
session.close()
239241
}
240-
241-
// When
242-
startNewTransaction(driver)
243242
})
244243

245244
it('should have correct user agent', () => {
@@ -267,42 +266,6 @@ describe('#integration driver', () => {
267266
)
268267
})
269268

270-
it('should treat closed connections as invalid', () => {
271-
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
272-
273-
const connectionValid = driver._validateConnection(
274-
new FakeConnection().closed()
275-
)
276-
277-
expect(connectionValid).toBeFalsy()
278-
})
279-
280-
it('should treat not old open connections as valid', () => {
281-
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {
282-
maxConnectionLifetime: 10
283-
})
284-
285-
const connection = new FakeConnection().withCreationTimestamp(12)
286-
clock = lolex.install()
287-
clock.setSystemTime(20)
288-
const connectionValid = driver._validateConnection(connection)
289-
290-
expect(connectionValid).toBeTruthy()
291-
})
292-
293-
it('should treat old open connections as invalid', () => {
294-
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken, {
295-
maxConnectionLifetime: 10
296-
})
297-
298-
const connection = new FakeConnection().withCreationTimestamp(5)
299-
clock = lolex.install()
300-
clock.setSystemTime(20)
301-
const connectionValid = driver._validateConnection(connection)
302-
303-
expect(connectionValid).toBeFalsy()
304-
})
305-
306269
it('should discard closed connections', done => {
307270
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
308271

@@ -514,7 +477,9 @@ describe('#integration driver', () => {
514477
}
515478

516479
function openConnectionFrom (driver) {
517-
return Array.from(Object.values(driver._openConnections))
480+
return Array.from(
481+
Object.values(driver._connectionProvider._openConnections)
482+
)
518483
}
519484

520485
function routingProcedureOnlyAvailableOnCores () {

test/examples.test.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,27 @@ describe('#integration examples', () => {
271271
// tag::driver-lifecycle[]
272272
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
273273

274-
driver.verifyConnectivity().then(() => {
275-
console.log('Driver created')
276-
})
277-
278-
driver.onError = error => {
279-
console.log(error)
280-
}
274+
driver
275+
.verifyConnectivity()
276+
.then(() => {
277+
console.log('Driver created')
278+
})
279+
.catch(error => {
280+
console.log(`connectivity verification failed. ${error}`)
281+
})
281282

282283
const session = driver.session()
283-
session.run('CREATE (i:Item)').then(() => {
284-
session.close()
284+
session
285+
.run('CREATE (i:Item)')
286+
.then(() => {
287+
session.close()
285288

286-
// ... on application exit:
287-
driver.close()
288-
})
289+
// ... on application exit:
290+
driver.close()
291+
})
292+
.catch(error => {
293+
console.log(`unable to execute statement. ${error}`)
294+
})
289295
// end::driver-lifecycle[]
290296

291297
testResultPromise.then(loggedMsg => {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
import PooledConnectionProvider from '../../src/internal/connection-provider-pooled'
20+
import FakeConnection from './fake-connection'
21+
import lolex from 'lolex'
22+
23+
describe('#unit PooledConnectionProvider', () => {
24+
let clock
25+
26+
beforeEach(() => {
27+
if (clock) {
28+
clock.uninstall()
29+
clock = null
30+
}
31+
})
32+
33+
it('should treat closed connections as invalid', () => {
34+
const provider = new PooledConnectionProvider({
35+
id: 0,
36+
config: {}
37+
})
38+
39+
const connectionValid = provider._validateConnection(
40+
new FakeConnection().closed()
41+
)
42+
43+
expect(connectionValid).toBeFalsy()
44+
})
45+
46+
it('should treat not old open connections as valid', () => {
47+
const provider = new PooledConnectionProvider({
48+
id: 0,
49+
config: {
50+
maxConnectionLifetime: 10
51+
}
52+
})
53+
54+
const connection = new FakeConnection().withCreationTimestamp(12)
55+
clock = lolex.install()
56+
clock.setSystemTime(20)
57+
const connectionValid = provider._validateConnection(connection)
58+
59+
expect(connectionValid).toBeTruthy()
60+
})
61+
62+
it('should treat old open connections as invalid', () => {
63+
const provider = new PooledConnectionProvider({
64+
id: 0,
65+
config: {
66+
maxConnectionLifetime: 10
67+
}
68+
})
69+
70+
const connection = new FakeConnection().withCreationTimestamp(5)
71+
clock = lolex.install()
72+
clock.setSystemTime(20)
73+
const connectionValid = provider._validateConnection(connection)
74+
75+
expect(connectionValid).toBeFalsy()
76+
})
77+
})

test/session.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ describe('#integration session', () => {
12741274
}
12751275

12761276
function numberOfAcquiredConnectionsFromPool () {
1277-
const pool = driver._pool
1277+
const pool = driver._connectionProvider._connectionPool
12781278
return pool.activeResourceCount(ServerAddress.fromUrl('localhost:7687'))
12791279
}
12801280

test/types/driver.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,3 @@ driver.verifyConnectivity().then((serverInfo: ServerInfo) => {
101101
console.log(serverInfo.version)
102102
console.log(serverInfo.address)
103103
})
104-
105-
driver.onError = (error: Neo4jError) => {
106-
console.log(error)
107-
}
108-
109-
driver.onError(new Neo4jError('message', 'code'))

types/driver.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ declare interface Driver {
7575
close(): void
7676

7777
verifyConnectivity(): Promise<ServerInfo>
78-
79-
onError?: (error: Neo4jError) => void
8078
}
8179

8280
export {

0 commit comments

Comments
 (0)