Skip to content

Commit ad5f5a3

Browse files
Peter Wilhelmsson2hdddg
authored andcommitted
Send connect address to routing procedure
1 parent 4a6bfff commit ad5f5a3

File tree

4 files changed

+76
-55
lines changed

4 files changed

+76
-55
lines changed

src/internal/connection-provider-routing.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
5757

5858
this._seedRouter = address
5959
this._routingTables = {}
60-
this._rediscovery = new Rediscovery(new RoutingUtil(routingContext))
60+
this._rediscovery = new Rediscovery(
61+
new RoutingUtil(routingContext, address)
62+
)
6163
this._loadBalancingStrategy = new LeastConnectedLoadBalancingStrategy(
6264
this._connectionPool
6365
)

src/internal/routing-util.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ const PROCEDURE_NOT_FOUND_CODE = 'Neo.ClientError.Procedure.ProcedureNotFound'
3333
const DATABASE_NOT_FOUND_CODE = 'Neo.ClientError.Database.DatabaseNotFound'
3434

3535
export default class RoutingUtil {
36-
constructor (routingContext) {
36+
constructor (routingContext, initialAddress) {
3737
this._routingContext = routingContext
38+
// The address that the driver is connecting to, used by routing as a fallback when routing
39+
// and clustering isn't configured.
40+
this._initialAddress = initialAddress
3841
}
3942

4043
/**
@@ -148,6 +151,10 @@ export default class RoutingUtil {
148151
context: this._routingContext,
149152
database: database || null
150153
}
154+
if (protocolVersion >= 4.1) {
155+
params.context = params.context || {}
156+
params.context.address = this._initialAddress
157+
}
151158
} else {
152159
query = CALL_GET_ROUTING_TABLE
153160
params = { context: this._routingContext }

test/internal/routing-util.test.js

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,58 @@ import {
3737

3838
const ROUTER_ADDRESS = ServerAddress.fromUrl('test.router.com:4242')
3939

40+
class FakeSession {
41+
constructor (runResponse, fakeConnection) {
42+
this._runResponse = runResponse
43+
this._fakeConnection = fakeConnection
44+
this._closed = false
45+
}
46+
47+
static successful (result) {
48+
return new FakeSession(Promise.resolve(result), null)
49+
}
50+
51+
static failed (error) {
52+
return new FakeSession(Promise.reject(error), null)
53+
}
54+
55+
static withFakeConnection (connection) {
56+
return new FakeSession(null, connection)
57+
}
58+
59+
_run (ignoreQuery, ignoreParameters, queryRunner) {
60+
if (this._runResponse) {
61+
return this._runResponse
62+
}
63+
queryRunner(this._fakeConnection)
64+
return Promise.resolve()
65+
}
66+
67+
withBookmark (bookmark) {
68+
this._lastBookmark = bookmark
69+
return this
70+
}
71+
72+
withDatabase (database) {
73+
this._database = database || ''
74+
return this
75+
}
76+
77+
withMode (mode) {
78+
this._mode = mode
79+
return this
80+
}
81+
82+
close () {
83+
this._closed = true
84+
return Promise.resolve()
85+
}
86+
87+
isClosed () {
88+
return this._closed
89+
}
90+
}
91+
4092
describe('#unit RoutingUtil', () => {
4193
it('should return retrieved records when query succeeds', done => {
4294
const session = FakeSession.successful({ records: ['foo', 'bar', 'baz'] })
@@ -261,6 +313,18 @@ describe('#unit RoutingUtil', () => {
261313
))
262314
})
263315

316+
it('should pass initial address while invoking routing procedure', async () => {
317+
const connection = new FakeConnection().withServerVersion('Neo4j/4.1.0')
318+
const session = FakeSession.withFakeConnection(connection)
319+
const util = new RoutingUtil({}, 'initialAddr')
320+
321+
await util.callRoutingProcedure(session, '', ROUTER_ADDRESS)
322+
323+
expect(connection.seenParameters).toEqual([
324+
{ context: { address: 'initialAddr' }, database: null }
325+
])
326+
})
327+
264328
it('should parse valid ttl', () => {
265329
const clock = lolex.install()
266330
try {
@@ -503,56 +567,4 @@ describe('#unit RoutingUtil', () => {
503567
done()
504568
})
505569
}
506-
507-
class FakeSession {
508-
constructor (runResponse, fakeConnection) {
509-
this._runResponse = runResponse
510-
this._fakeConnection = fakeConnection
511-
this._closed = false
512-
}
513-
514-
static successful (result) {
515-
return new FakeSession(Promise.resolve(result), null)
516-
}
517-
518-
static failed (error) {
519-
return new FakeSession(Promise.reject(error), null)
520-
}
521-
522-
static withFakeConnection (connection) {
523-
return new FakeSession(null, connection)
524-
}
525-
526-
_run (ignoreQuery, ignoreParameters, queryRunner) {
527-
if (this._runResponse) {
528-
return this._runResponse
529-
}
530-
queryRunner(this._fakeConnection)
531-
return Promise.resolve()
532-
}
533-
534-
withBookmark (bookmark) {
535-
this._lastBookmark = bookmark
536-
return this
537-
}
538-
539-
withDatabase (database) {
540-
this._database = database || ''
541-
return this
542-
}
543-
544-
withMode (mode) {
545-
this._mode = mode
546-
return this
547-
}
548-
549-
close () {
550-
this._closed = true
551-
return Promise.resolve()
552-
}
553-
554-
isClosed () {
555-
return this._closed
556-
}
557-
}
558570
})

test/session.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('#integration session', () => {
4141
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
4242
session = driver.session()
4343
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
44-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
44+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 70000
4545

4646
protocolVersion = await sharedNeo4j.cleanupAndGetProtocolVersion(driver)
4747
})

0 commit comments

Comments
 (0)