Skip to content

Commit c75c1e8

Browse files
authored
Merge pull request #425 from lutovich/1.7-js-numbers-ttl
Fix TTL parsing when driver configured to use JS numbers
2 parents 479ffb6 + 205a187 commit c75c1e8

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

src/v1/internal/routing-util.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ export default class RoutingUtil {
6161
parseTtl(record, routerAddress) {
6262
try {
6363
const now = int(Date.now());
64-
const expires = record.get('ttl').multiply(1000).add(now);
64+
const expires = int(record.get('ttl')).multiply(1000).add(now);
6565
// if the server uses a really big expire time like Long.MAX_VALUE this may have overflowed
6666
if (expires.lessThan(now)) {
6767
return Integer.MAX_VALUE;
6868
}
6969
return expires;
7070
} catch (error) {
7171
throw newError(
72-
'Unable to parse TTL entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record),
72+
`Unable to parse TTL entry from router ${routerAddress} from record:\n${JSON.stringify(record)}\nError message: ${error.message}`,
7373
PROTOCOL_ERROR);
7474
}
7575
}
@@ -102,9 +102,9 @@ export default class RoutingUtil {
102102
readers: readers,
103103
writers: writers
104104
}
105-
} catch (ignore) {
105+
} catch (error) {
106106
throw newError(
107-
'Unable to parse servers entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record),
107+
`Unable to parse servers entry from router ${routerAddress} from record:\n${JSON.stringify(record)}\nError message: ${error.message}`,
108108
PROTOCOL_ERROR);
109109
}
110110
}

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

+32-24
Original file line numberDiff line numberDiff line change
@@ -1999,31 +1999,11 @@ describe('routing driver with stub server', () => {
19991999
});
20002000

20012001
it('should rediscover using older getServers procedure when server is old', done => {
2002-
if (!boltStub.supported) {
2003-
done();
2004-
return;
2005-
}
2006-
2007-
const router = boltStub.start('./test/resources/boltstub/acquire_endpoints_old_routing_procedure.script', 9001);
2008-
const reader = boltStub.start('./test/resources/boltstub/read_server.script', 9005);
2009-
2010-
boltStub.run(() => {
2011-
const driver = boltStub.newDriver('bolt+routing://127.0.0.1:9001');
2002+
testDiscoveryAndReadQueryInAutoCommitTx('./test/resources/boltstub/acquire_endpoints_old_routing_procedure.script', {}, done);
2003+
});
20122004

2013-
const session = driver.session(READ);
2014-
session.run('MATCH (n) RETURN n.name').then(result => {
2015-
expect(result.records.map(record => record.get(0))).toEqual(['Bob', 'Alice', 'Tina']);
2016-
session.close();
2017-
driver.close();
2018-
router.exit(code1 => {
2019-
reader.exit(code2 => {
2020-
expect(code1).toEqual(0);
2021-
expect(code2).toEqual(0);
2022-
done();
2023-
});
2024-
});
2025-
}).catch(done.fail);
2026-
});
2005+
it('should connect to cluster when disableLosslessIntegers is on', done => {
2006+
testDiscoveryAndReadQueryInAutoCommitTx('./test/resources/boltstub/acquire_endpoints.script', {disableLosslessIntegers: true}, done);
20272007
});
20282008

20292009
function testAddressPurgeOnDatabaseError(query, accessMode, done) {
@@ -2114,6 +2094,34 @@ describe('routing driver with stub server', () => {
21142094
});
21152095
}
21162096

2097+
function testDiscoveryAndReadQueryInAutoCommitTx(routerScript, driverConfig, done) {
2098+
if (!boltStub.supported) {
2099+
done();
2100+
return;
2101+
}
2102+
2103+
const router = boltStub.start(routerScript, 9001);
2104+
const reader = boltStub.start('./test/resources/boltstub/read_server.script', 9005);
2105+
2106+
boltStub.run(() => {
2107+
const driver = boltStub.newDriver('bolt+routing://127.0.0.1:9001', driverConfig);
2108+
2109+
const session = driver.session(READ);
2110+
session.run('MATCH (n) RETURN n.name').then(result => {
2111+
expect(result.records.map(record => record.get(0))).toEqual(['Bob', 'Alice', 'Tina']);
2112+
session.close();
2113+
driver.close();
2114+
router.exit(code1 => {
2115+
reader.exit(code2 => {
2116+
expect(code1).toEqual(0);
2117+
expect(code2).toEqual(0);
2118+
done();
2119+
});
2120+
});
2121+
}).catch(done.fail);
2122+
});
2123+
}
2124+
21172125
function testForProtocolError(scriptFile, done) {
21182126
if (!boltStub.supported) {
21192127
done();

test/internal/routing-util.test.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,18 @@ describe('RoutingUtil', () => {
238238
});
239239

240240
function testValidTtlParsing(currentTime, ttlSeconds) {
241-
const record = newRecord({ttl: int(ttlSeconds)});
242241
clock.setSystemTime(currentTime);
242+
const expectedExpirationTime = currentTime + ttlSeconds * 1000;
243243

244-
const expirationTime = parseTtl(record).toNumber();
244+
// verify parsing when TTL is an Integer
245+
const record1 = newRecord({ttl: int(ttlSeconds)});
246+
const expirationTime1 = parseTtl(record1).toNumber();
247+
expect(expirationTime1).toEqual(expectedExpirationTime);
245248

246-
expect(expirationTime).toEqual(currentTime + ttlSeconds * 1000);
249+
// verify parsing when TTL is a JavaScript Number, this can happen when driver is configured with {disableLosslessIntegers: true}
250+
const record2 = newRecord({ttl: ttlSeconds});
251+
const expirationTime2 = parseTtl(record2).toNumber();
252+
expect(expirationTime2).toEqual(expectedExpirationTime);
247253
}
248254

249255
function testValidServersParsing(routerAddresses, readerAddresses, writerAddresses) {

0 commit comments

Comments
 (0)