@@ -22,6 +22,54 @@ class SessionTransactionSpecError < StandardError; end
2222 collection . delete_many
2323 end
2424
25+ describe '#abort_transaction' do
26+ require_topology :replica_set
27+
28+ context 'when a non-Mongo error is raised' do
29+ before do
30+ collection . insert_one ( { foo : 1 } )
31+ end
32+
33+ it 'propagates the exception and sets state to transaction aborted' do
34+ session . start_transaction
35+ collection . insert_one ( { foo : 1 } , session : session )
36+ expect ( session ) . to receive ( :write_with_retry ) . and_raise ( SessionTransactionSpecError )
37+ expect do
38+ session . abort_transaction
39+ end . to raise_error ( SessionTransactionSpecError )
40+ expect ( session . send ( :within_states? , Mongo ::Session ::TRANSACTION_ABORTED_STATE ) ) . to be true
41+
42+ # Since we failed abort_transaction call, the transaction is still
43+ # outstanding. It will cause subsequent tests to stall until it times
44+ # out on the server side. End the session to force the server
45+ # to close the transaction.
46+ kill_all_server_sessions
47+ end
48+ end
49+
50+ context 'when a Mongo error is raised' do
51+ before do
52+ collection . insert_one ( { foo : 1 } )
53+ end
54+
55+ it 'swallows the exception and sets state to transaction aborted' do
56+ session . start_transaction
57+ collection . insert_one ( { foo : 1 } , session : session )
58+ expect ( session ) . to receive ( :write_with_retry ) . and_raise ( Mongo ::Error ::SocketError )
59+ expect do
60+ session . abort_transaction
61+ end . not_to raise_error
62+ expect ( session . send ( :within_states? , Mongo ::Session ::TRANSACTION_ABORTED_STATE ) ) . to be true
63+
64+ # Since we failed abort_transaction call, the transaction is still
65+ # outstanding. It will cause subsequent tests to stall until it times
66+ # out on the server side. End the session to force the server
67+ # to close the transaction.
68+ kill_all_server_sessions
69+ end
70+ end
71+ end
72+
2573 describe '#with_transaction' do
2674 context 'callback successful' do
2775 it 'commits' do
0 commit comments