Skip to content

Commit 5a262fa

Browse files
committed
Add a couple of tests
1 parent 50f80c5 commit 5a262fa

File tree

6 files changed

+286
-78
lines changed

6 files changed

+286
-78
lines changed

src/internal/connection-delegate.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export default class DelegateConnection extends Connection {
2727
constructor (delegate, errorHandler) {
2828
super(errorHandler)
2929

30-
this._originalErrorHandler = delegate._errorHandler
31-
delegate._errorHandler = this._errorHandler
30+
if (errorHandler) {
31+
this._originalErrorHandler = delegate._errorHandler
32+
delegate._errorHandler = this._errorHandler
33+
}
3234

3335
this._delegate = delegate
3436
}
@@ -45,12 +47,8 @@ export default class DelegateConnection extends Connection {
4547
this._delegate.databaseId = value
4648
}
4749

48-
isOpen () {
49-
return this._delegate.isOpen()
50-
}
51-
52-
protocol () {
53-
return this._delegate.protocol()
50+
get server () {
51+
return this._delegate.server
5452
}
5553

5654
get address () {
@@ -65,8 +63,12 @@ export default class DelegateConnection extends Connection {
6563
this._delegate.version = value
6664
}
6765

68-
get server () {
69-
return this._delegate.server
66+
isOpen () {
67+
return this._delegate.isOpen()
68+
}
69+
70+
protocol () {
71+
return this._delegate.protocol()
7072
}
7173

7274
connect (userAgent, authToken) {
@@ -86,7 +88,10 @@ export default class DelegateConnection extends Connection {
8688
}
8789

8890
_release () {
89-
this._delegate._errorHandler = this._originalErrorHandler
91+
if (this._originalErrorHandler) {
92+
this._delegate._errorHandler = this._originalErrorHandler
93+
}
94+
9095
this._delegate._release()
9196
}
9297
}

src/internal/connection-provider-direct.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import PooledConnectionProvider from './connection-provider-pooled'
21+
import DelegateConnection from './connection-delegate'
2122

2223
export default class DirectConnectionProvider extends PooledConnectionProvider {
2324
constructor ({ id, config, log, address, userAgent, authToken }) {
@@ -27,6 +28,8 @@ export default class DirectConnectionProvider extends PooledConnectionProvider {
2728
}
2829

2930
acquireConnection (accessMode, database) {
30-
return this._connectionPool.acquire(this._address)
31+
return this._connectionPool
32+
.acquire(this._address)
33+
.then(connection => new DelegateConnection(connection, null))
3134
}
3235
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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 DelegateConnection from '../../src/internal/connection-delegate'
20+
import Connection from '../../src/internal/connection'
21+
import BoltProtocol from '../../src/internal/bolt-protocol-v1'
22+
import BoltAddress from '../../src/internal/server-address'
23+
import ConnectionErrorHandler from '../../src/internal/connection-error-handler'
24+
25+
describe('#unit DelegateConnection', () => {
26+
it('should delegate get id', () => {
27+
const delegate = new Connection(null)
28+
const spy = spyOnProperty(delegate, 'id', 'get').and.returnValue(5)
29+
const connection = new DelegateConnection(delegate, null)
30+
31+
expect(connection.id).toBe(5)
32+
expect(spy).toHaveBeenCalledTimes(1)
33+
})
34+
35+
it('should delegate get databaseId', () => {
36+
const delegate = new Connection(null)
37+
const spy = spyOnProperty(delegate, 'databaseId', 'get').and.returnValue(
38+
'123-456'
39+
)
40+
const connection = new DelegateConnection(delegate, null)
41+
42+
expect(connection.databaseId).toBe('123-456')
43+
expect(spy).toHaveBeenCalledTimes(1)
44+
})
45+
46+
it('should delegate set databaseId', () => {
47+
const delegate = new Connection(null)
48+
const spy = spyOnProperty(delegate, 'databaseId', 'set')
49+
const connection = new DelegateConnection(delegate, null)
50+
51+
connection.databaseId = '345-678'
52+
53+
expect(spy).toHaveBeenCalledTimes(1)
54+
})
55+
56+
it('should delegate get server', () => {
57+
const server = {
58+
address: BoltAddress.fromUrl('bolt://127.0.0.1:8798'),
59+
version: 'Neo4j/3.5.6'
60+
}
61+
const delegate = new Connection(null)
62+
const spy = spyOnProperty(delegate, 'server', 'get').and.returnValue(server)
63+
const connection = new DelegateConnection(delegate, null)
64+
65+
expect(connection.server).toBe(server)
66+
expect(spy).toHaveBeenCalledTimes(1)
67+
})
68+
69+
it('should delegate get address', () => {
70+
const address = BoltAddress.fromUrl('bolt://127.0.0.1:8080')
71+
const delegate = new Connection(null)
72+
const spy = spyOnProperty(delegate, 'address', 'get').and.returnValue(
73+
address
74+
)
75+
const connection = new DelegateConnection(delegate, null)
76+
77+
expect(connection.address).toBe(address)
78+
expect(spy).toHaveBeenCalledTimes(1)
79+
})
80+
81+
it('should delegate get version', () => {
82+
const version = 'Neo4j/3.5.6'
83+
const delegate = new Connection(null)
84+
const spy = spyOnProperty(delegate, 'version', 'get').and.returnValue(
85+
version
86+
)
87+
const connection = new DelegateConnection(delegate, null)
88+
89+
expect(connection.version).toBe(version)
90+
expect(spy).toHaveBeenCalledTimes(1)
91+
})
92+
93+
it('should delegate set version', () => {
94+
const delegate = new Connection(null)
95+
const spy = spyOnProperty(delegate, 'version', 'set')
96+
const connection = new DelegateConnection(delegate, null)
97+
98+
connection.version = 'Neo4j/3.4.9'
99+
100+
expect(spy).toHaveBeenCalledTimes(1)
101+
})
102+
103+
it('should delegate isOpen', () => {
104+
const delegate = new Connection(null)
105+
const spy = spyOn(delegate, 'isOpen').and.returnValue(true)
106+
const connection = new DelegateConnection(delegate, null)
107+
108+
expect(connection.isOpen()).toBeTruthy()
109+
expect(spy).toHaveBeenCalledTimes(1)
110+
})
111+
112+
it('should delegate protocol', () => {
113+
const protocol = new BoltProtocol()
114+
const delegate = new Connection(null)
115+
const spy = spyOn(delegate, 'protocol').and.returnValue(protocol)
116+
const connection = new DelegateConnection(delegate, null)
117+
118+
expect(connection.protocol()).toBe(protocol)
119+
expect(spy).toHaveBeenCalledTimes(1)
120+
})
121+
122+
it('should delegate connect', () => {
123+
const delegate = new Connection(null)
124+
const spy = spyOn(delegate, 'connect')
125+
const connection = new DelegateConnection(delegate, null)
126+
127+
connection.connect('neo4j/js-driver', {})
128+
129+
expect(spy).toHaveBeenCalledTimes(1)
130+
})
131+
132+
it('should delegate write', () => {
133+
const delegate = new Connection(null)
134+
const spy = spyOn(delegate, 'write')
135+
const connection = new DelegateConnection(delegate, null)
136+
137+
connection.write({}, null, true)
138+
139+
expect(spy).toHaveBeenCalledTimes(1)
140+
})
141+
142+
it('should delegate resetAndFlush', () => {
143+
const delegate = new Connection(null)
144+
const spy = spyOn(delegate, 'resetAndFlush')
145+
const connection = new DelegateConnection(delegate, null)
146+
147+
connection.resetAndFlush()
148+
149+
expect(spy).toHaveBeenCalledTimes(1)
150+
})
151+
152+
it('should delegate close', () => {
153+
const delegate = new Connection(null)
154+
const spy = spyOn(delegate, 'close')
155+
const connection = new DelegateConnection(delegate, null)
156+
157+
connection.close()
158+
159+
expect(spy).toHaveBeenCalledTimes(1)
160+
})
161+
162+
it('should delegate _release', () => {
163+
const delegate = new Connection(null)
164+
delegate._release = () => {}
165+
const spy = spyOn(delegate, '_release')
166+
const connection = new DelegateConnection(delegate, null)
167+
168+
connection._release()
169+
170+
expect(spy).toHaveBeenCalledTimes(1)
171+
})
172+
173+
it('should override errorHandler on create and restore on release', () => {
174+
const errorHandlerOriginal = new ConnectionErrorHandler('code1')
175+
const delegate = new Connection(errorHandlerOriginal)
176+
delegate._release = () => {}
177+
178+
expect(delegate._errorHandler).toBe(errorHandlerOriginal)
179+
180+
const errorHandlerNew = new ConnectionErrorHandler('code2')
181+
const connection = new DelegateConnection(delegate, errorHandlerNew)
182+
183+
expect(delegate._errorHandler).toBe(errorHandlerNew)
184+
185+
connection._release()
186+
187+
expect(delegate._errorHandler).toBe(errorHandlerOriginal)
188+
})
189+
})

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,31 @@ import Pool from '../../src/internal/pool'
2323
import ServerAddress from '../../src/internal/server-address'
2424
import Connection from '../../src/internal/connection'
2525
import Logger from '../../src/internal/logger'
26+
import DelegateConnection from '../../src/internal/connection-delegate'
2627

2728
describe('#unit DirectConnectionProvider', () => {
2829
it('acquires connection from the pool', done => {
2930
const address = ServerAddress.fromUrl('localhost:123')
3031
const pool = newPool()
3132
const connectionProvider = newDirectConnectionProvider(address, pool)
3233

33-
connectionProvider.acquireConnection(READ).then(connection => {
34+
connectionProvider.acquireConnection(READ, '').then(connection => {
3435
expect(connection).toBeDefined()
3536
expect(connection.address).toEqual(address)
36-
expect(connection.release).toBeDefined()
3737
expect(pool.has(address)).toBeTruthy()
3838

3939
done()
4040
})
4141
})
42+
43+
it('acquires connection and returns a DelegateConnection', async () => {
44+
const address = ServerAddress.fromUrl('localhost:123')
45+
const pool = newPool()
46+
const connectionProvider = newDirectConnectionProvider(address, pool)
47+
48+
const conn = await connectionProvider.acquireConnection(READ, '')
49+
expect(conn instanceof DelegateConnection).toBeTruthy()
50+
})
4251
})
4352

4453
function newDirectConnectionProvider (address, pool) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import ServerAddress from '../../src/internal/server-address'
2828
import RoutingConnectionProvider from '../../src/internal/connection-provider-routing'
2929
import { VERSION_IN_DEV } from '../../src/internal/server-version'
3030
import Connection from '../../src/internal/connection'
31+
import DelegateConnection from '../../src/internal/connection-delegate'
3132

3233
describe('#unit RoutingConnectionProvider', () => {
3334
let originalTimeout
@@ -169,6 +170,23 @@ describe('#unit RoutingConnectionProvider', () => {
169170
)
170171
})
171172

173+
it('acquires connection and returns a DelegateConnection', async () => {
174+
const pool = newPool()
175+
const connectionProvider = newRoutingConnectionProvider(
176+
'',
177+
[server1, server2],
178+
[server3, server4],
179+
[server5, server6],
180+
pool
181+
)
182+
183+
const conn1 = await connectionProvider.acquireConnection(READ, '')
184+
expect(conn1 instanceof DelegateConnection).toBeTruthy()
185+
186+
const conn2 = await connectionProvider.acquireConnection(WRITE, '')
187+
expect(conn2 instanceof DelegateConnection).toBeTruthy()
188+
})
189+
172190
it('acquires read connection with up-to-date routing table', done => {
173191
const pool = newPool()
174192
const connectionProvider = newRoutingConnectionProvider(

0 commit comments

Comments
 (0)