Skip to content

Commit

Permalink
Implementing a after_commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ProGM committed Dec 5, 2016
1 parent 7abe23c commit cd4f204
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/neo4j/core/cypher_session/transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def queries(options = {}, &block)
adaptor.queries(@session, {transaction: self}.merge(options), &block)
end

def after_commit_registry
@after_commit_registry ||= []
end

def after_commit(&block)
after_commit_registry << block
end

def post_close!
super
after_commit_registry.each(&:call) unless failed?
end

private

# Because we're inheriting from the old Transaction class
Expand Down
40 changes: 40 additions & 0 deletions spec/neo4j/core/shared_examples/adaptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,46 @@ def get_object_by_id(id, adaptor)
end
expect(get_object_by_id(9, adaptor)).to be_nil
end

describe 'after_commit hook' do
it 'gets called when the root transaction is closed' do
data = false
tx1 = adaptor.transaction(session_double)
tx2 = adaptor.transaction(session_double)
tx3 = adaptor.transaction(session_double)
tx3.root.after_commit { data = true }
tx3.close
tx2.close
expect { tx1.close }.to change { data }.to(true)
expect(data).to be_truthy
end

it 'is ignored when the root transaction fails' do
data = false
tx1 = adaptor.transaction(session_double)
tx2 = adaptor.transaction(session_double)
tx3 = adaptor.transaction(session_double)
tx3.root.after_commit { data = true }
tx1.mark_failed
tx3.close
tx2.close
expect { tx1.close }.not_to change { data }
expect(data).to be_falsey
end

it 'is ignored when a child transaction fails' do
data = false
tx1 = adaptor.transaction(session_double)
tx2 = adaptor.transaction(session_double)
tx3 = adaptor.transaction(session_double)
tx3.root.after_commit { data = true }
tx3.mark_failed
tx3.close
tx2.close
expect { tx1.close }.not_to change { data }
expect(data).to be_falsey
end
end
# it 'does not allow transactions in the wrong order' do
# expect { adaptor.end_transaction }.to raise_error(RuntimeError, /Cannot close transaction without starting one/)
end
Expand Down

0 comments on commit cd4f204

Please sign in to comment.