Skip to content

Fix TTL parsing when driver configured to use JS numbers #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/v1/internal/routing-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export default class RoutingUtil {
parseTtl(record, routerAddress) {
try {
const now = int(Date.now());
const expires = record.get('ttl').multiply(1000).add(now);
const expires = int(record.get('ttl')).multiply(1000).add(now);
// if the server uses a really big expire time like Long.MAX_VALUE this may have overflowed
if (expires.lessThan(now)) {
return Integer.MAX_VALUE;
}
return expires;
} catch (error) {
throw newError(
'Unable to parse TTL entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record),
`Unable to parse TTL entry from router ${routerAddress} from record:\n${JSON.stringify(record)}\nError message: ${error.message}`,
PROTOCOL_ERROR);
}
}
Expand Down Expand Up @@ -102,9 +102,9 @@ export default class RoutingUtil {
readers: readers,
writers: writers
}
} catch (ignore) {
} catch (error) {
throw newError(
'Unable to parse servers entry from router ' + routerAddress + ' from record:\n' + JSON.stringify(record),
`Unable to parse servers entry from router ${routerAddress} from record:\n${JSON.stringify(record)}\nError message: ${error.message}`,
PROTOCOL_ERROR);
}
}
Expand Down
56 changes: 32 additions & 24 deletions test/internal/node/routing.driver.boltkit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1999,31 +1999,11 @@ describe('routing driver with stub server', () => {
});

it('should rediscover using older getServers procedure when server is old', done => {
if (!boltStub.supported) {
done();
return;
}

const router = boltStub.start('./test/resources/boltstub/acquire_endpoints_old_routing_procedure.script', 9001);
const reader = boltStub.start('./test/resources/boltstub/read_server.script', 9005);

boltStub.run(() => {
const driver = boltStub.newDriver('bolt+routing://127.0.0.1:9001');
testDiscoveryAndReadQueryInAutoCommitTx('./test/resources/boltstub/acquire_endpoints_old_routing_procedure.script', {}, done);
});

const session = driver.session(READ);
session.run('MATCH (n) RETURN n.name').then(result => {
expect(result.records.map(record => record.get(0))).toEqual(['Bob', 'Alice', 'Tina']);
session.close();
driver.close();
router.exit(code1 => {
reader.exit(code2 => {
expect(code1).toEqual(0);
expect(code2).toEqual(0);
done();
});
});
}).catch(done.fail);
});
it('should connect to cluster when disableLosslessIntegers is on', done => {
testDiscoveryAndReadQueryInAutoCommitTx('./test/resources/boltstub/acquire_endpoints.script', {disableLosslessIntegers: true}, done);
});

function testAddressPurgeOnDatabaseError(query, accessMode, done) {
Expand Down Expand Up @@ -2114,6 +2094,34 @@ describe('routing driver with stub server', () => {
});
}

function testDiscoveryAndReadQueryInAutoCommitTx(routerScript, driverConfig, done) {
if (!boltStub.supported) {
done();
return;
}

const router = boltStub.start(routerScript, 9001);
const reader = boltStub.start('./test/resources/boltstub/read_server.script', 9005);

boltStub.run(() => {
const driver = boltStub.newDriver('bolt+routing://127.0.0.1:9001', driverConfig);

const session = driver.session(READ);
session.run('MATCH (n) RETURN n.name').then(result => {
expect(result.records.map(record => record.get(0))).toEqual(['Bob', 'Alice', 'Tina']);
session.close();
driver.close();
router.exit(code1 => {
reader.exit(code2 => {
expect(code1).toEqual(0);
expect(code2).toEqual(0);
done();
});
});
}).catch(done.fail);
});
}

function testForProtocolError(scriptFile, done) {
if (!boltStub.supported) {
done();
Expand Down
12 changes: 9 additions & 3 deletions test/internal/routing-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,18 @@ describe('RoutingUtil', () => {
});

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

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

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

function testValidServersParsing(routerAddresses, readerAddresses, writerAddresses) {
Expand Down