Skip to content

Make sure session does not have bookmark after unsuccessful tx #184

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
Jan 16, 2017
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
7 changes: 3 additions & 4 deletions src/v1/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Transaction {
* @param {function()} onClose - Function to be called when transaction is committed or rolled back.
* @param errorTransformer callback use to transform error
* @param bookmark optional bookmark
* @param onBookmark callback invoked when new bookmark is produced
*/
constructor(connectionPromise, onClose, errorTransformer, bookmark, onBookmark) {
this._connectionPromise = connectionPromise;
Expand Down Expand Up @@ -129,10 +130,8 @@ class _TransactionStreamObserver extends StreamObserver {

onCompleted(meta) {
super.onCompleted(meta);
let bookmark = meta.bookmark;
if (bookmark) {
this._tx._onBookmark(bookmark);
}
const bookmark = meta.bookmark;
this._tx._onBookmark(bookmark);
}

serverMeta() {
Expand Down
71 changes: 67 additions & 4 deletions test/v1/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ describe('transaction', function() {
});

it('should provide bookmark on commit', function (done) {
//bookmarking is not in 3.0
if (!server) {
done();
if (neo4jVersionOlderThan31(done)) {
return;
}

Expand All @@ -232,11 +230,71 @@ describe('transaction', function() {
tx.run("CREATE (:TXNode2)");
tx.commit()
.then(function () {
expect(session.lastBookmark()).toBeDefined();
expectValidLastBookmark(session);
done();
});
});

it('should have no bookmark when tx is rolled back', function (done) {
if (neo4jVersionOlderThan31(done)) {
return;
}

expect(session.lastBookmark()).not.toBeDefined();
const tx1 = session.beginTransaction();

tx1.run('CREATE ()').then(() => {
tx1.commit().then(() => {
expectValidLastBookmark(session);

const tx2 = session.beginTransaction();
tx2.run('CREATE ()').then(() => {
tx2.rollback().then(() => {
expect(session.lastBookmark()).not.toBeDefined();

const tx3 = session.beginTransaction();
tx3.run('CREATE ()').then(() => {
tx3.commit().then(() => {
expectValidLastBookmark(session);
done();
});
});
});
});
});
});
});

it('should have no bookmark when tx fails', function (done) {
if (neo4jVersionOlderThan31(done)) {
return;
}

expect(session.lastBookmark()).not.toBeDefined();
const tx1 = session.beginTransaction();

tx1.run('CREATE ()').then(() => {
tx1.commit().then(() => {
expectValidLastBookmark(session);

const tx2 = session.beginTransaction();

tx2.run('RETURN').catch(error => {
expectSyntaxError(error);
expect(session.lastBookmark()).not.toBeDefined();

const tx3 = session.beginTransaction();
tx3.run('CREATE ()').then(() => {
tx3.commit().then(() => {
expectValidLastBookmark(session);
done();
});
});
});
});
});
});

it('should rollback when very first run fails', done => {
const tx1 = session.beginTransaction();
tx1.run('RETURN foo')
Expand Down Expand Up @@ -349,6 +407,11 @@ describe('transaction', function() {
expect(code).toBe('Neo.ClientError.Statement.SyntaxError');
}

function expectValidLastBookmark(session) {
expect(session.lastBookmark()).toBeDefined();
expect(session.lastBookmark()).not.toBeNull();
}

function neo4jVersionOlderThan31(done) {
//lazy way of checking the version number
//if server has been set we know it is at least
Expand Down