Skip to content

Commit f4172b1

Browse files
committed
test rediscovery
1 parent d13b637 commit f4172b1

File tree

2 files changed

+195
-2
lines changed

2 files changed

+195
-2
lines changed

src/internal/rediscovery.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import RoutingTable from './routing-table'
2020
import Session from '../session'
2121
import { RoutingTableGetterFactory } from './routing-table-getter'
22+
import ServerAddress from './server-address'
2223

2324
export default class Rediscovery {
2425
/**
@@ -33,10 +34,10 @@ export default class Rediscovery {
3334
* Try to fetch new routing table from the given router.
3435
* @param {Session} session the session to use.
3536
* @param {string} database the database for which to lookup routing table.
36-
* @param {string} routerAddress the URL of the router.
37+
* @param {ServerAddress} routerAddress the URL of the router.
3738
* @return {Promise<RoutingTable>} promise resolved with new routing table or null when connection error happened.
3839
*/
39-
async lookupRoutingTableOnRouter (session, database, routerAddress) {
40+
lookupRoutingTableOnRouter (session, database, routerAddress) {
4041
return session._acquireConnection(connection => {
4142
const routingTableGetter = this._routingTableGetterFactory.create(
4243
connection

test/internal/rediscovery.test.js

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/**
2+
* Copyright (c) 2002-2020 "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+
20+
import Rediscovery from '../../src/internal/rediscovery'
21+
import RoutingTable from '../../src/internal/routing-table'
22+
import ServerAddress from '../../src/internal/server-address'
23+
24+
describe('#unit Rediscovery', () => {
25+
it('should return the routing table when it available', async () => {
26+
const expectedRoutingTable = new RoutingTable({
27+
database: 'db',
28+
expirationTime: 113,
29+
routers: [ServerAddress.fromUrl('bolt://localhost:7687')],
30+
writers: [ServerAddress.fromUrl('bolt://localhost:7686')],
31+
readers: [ServerAddress.fromUrl('bolt://localhost:7683')]
32+
})
33+
const routingTableGetter = new FakeRoutingTableGetter(
34+
Promise.resolve(expectedRoutingTable)
35+
)
36+
37+
const routingTable = await lookupRoutingTableOnRouter({
38+
routingTableGetter
39+
})
40+
41+
expect(routingTable).toBe(expectedRoutingTable)
42+
})
43+
44+
it('should call getter once with correct arguments', async () => {
45+
const expectedRoutingTable = new RoutingTable()
46+
const connection = { connection: 'abc' }
47+
const database = 'adb'
48+
const session = new FakeSession(connection)
49+
const routerAddress = ServerAddress.fromUrl('bolt://localhost:7682')
50+
const routingTableGetter = new FakeRoutingTableGetter(
51+
Promise.resolve(expectedRoutingTable)
52+
)
53+
54+
await lookupRoutingTableOnRouter({
55+
routingTableGetter,
56+
connection,
57+
session,
58+
database,
59+
routerAddress
60+
})
61+
62+
expect(routingTableGetter._called).toEqual(1)
63+
expect(routingTableGetter._arguments).toEqual([
64+
connection,
65+
database,
66+
routerAddress,
67+
session
68+
])
69+
})
70+
71+
it('should acquire connection once', async () => {
72+
const expectedRoutingTable = new RoutingTable()
73+
const connection = { connection: 'abc' }
74+
const database = 'adb'
75+
const session = new FakeSession(connection)
76+
const routerAddress = ServerAddress.fromUrl('bolt://localhost:7682')
77+
const routingTableGetter = new FakeRoutingTableGetter(
78+
Promise.resolve(expectedRoutingTable)
79+
)
80+
81+
await lookupRoutingTableOnRouter({
82+
routingTableGetter,
83+
connection,
84+
session,
85+
database,
86+
routerAddress
87+
})
88+
89+
expect(session._called).toEqual(1)
90+
})
91+
92+
it('should create the routingTableGetter with the correct arguments', async () => {
93+
const routingTable = new RoutingTable()
94+
const connection = { connection: 'abc' }
95+
const routingTableGetter = new FakeRoutingTableGetter(
96+
Promise.resolve(routingTable)
97+
)
98+
const factory = new FakeRoutingTableGetterFactory(routingTableGetter)
99+
100+
await lookupRoutingTableOnRouter({
101+
routingTableGetter,
102+
factory,
103+
connection
104+
})
105+
106+
expect(factory._called).toEqual(1)
107+
expect(factory._arguments).toEqual([connection])
108+
})
109+
110+
it('should return null when the getter resolves the table as null', async () => {
111+
const routingTableGetter = new FakeRoutingTableGetter(Promise.resolve(null))
112+
113+
const routingTable = await lookupRoutingTableOnRouter({
114+
routingTableGetter
115+
})
116+
117+
expect(routingTable).toBeNull()
118+
})
119+
120+
it('should fail when the getter fails', async () => {
121+
const expectedError = 'error'
122+
try {
123+
const routingTableGetter = new FakeRoutingTableGetter(
124+
Promise.reject(expectedError)
125+
)
126+
await lookupRoutingTableOnRouter({ routingTableGetter })
127+
fail('should not complete with success')
128+
} catch (error) {
129+
expect(error).toBe(expectedError)
130+
}
131+
})
132+
})
133+
134+
function lookupRoutingTableOnRouter ({
135+
database = 'db',
136+
routerAddress = ServerAddress.fromUrl('bolt://localhost:7687'),
137+
routingTableGetter = new FakeRoutingTableGetter(
138+
Promise.resolve(new RoutingTable())
139+
),
140+
session,
141+
factory,
142+
connection = {}
143+
} = {}) {
144+
const _factory =
145+
factory || new FakeRoutingTableGetterFactory(routingTableGetter)
146+
const _session = session || new FakeSession(connection)
147+
const rediscovery = new Rediscovery(_factory)
148+
149+
return rediscovery.lookupRoutingTableOnRouter(
150+
_session,
151+
database,
152+
routerAddress
153+
)
154+
}
155+
156+
class FakeRoutingTableGetter {
157+
constructor (result) {
158+
this._result = result
159+
this._called = 0
160+
}
161+
162+
get () {
163+
this._called++
164+
this._arguments = [...arguments]
165+
return this._result
166+
}
167+
}
168+
169+
class FakeRoutingTableGetterFactory {
170+
constructor (routingTableGetter) {
171+
this._routingTableGetter = routingTableGetter
172+
this._called = 0
173+
}
174+
175+
create () {
176+
this._called++
177+
this._arguments = [...arguments]
178+
return this._routingTableGetter
179+
}
180+
}
181+
182+
class FakeSession {
183+
constructor (connection) {
184+
this._connection = connection
185+
this._called = 0
186+
}
187+
188+
_acquireConnection (callback) {
189+
this._called++
190+
return callback(this._connection)
191+
}
192+
}

0 commit comments

Comments
 (0)