Skip to content

Commit 35ed135

Browse files
p-mongop
andauthored
RUBY-2617 redo the test to not set mocks in a thread (#2235)
* RUBY-2617 redo the test to not set mocks in a thread * Fix with_transaction helper and exclude jruby * go back to 10 iterations for mri * try retrying Co-authored-by: Oleg Pudeyev <oleg@bsdpower.com> Co-authored-by: Oleg Pudeyev <code@olegp.name>
1 parent 896da8d commit 35ed135

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

lib/mongo/session.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def with_transaction(options=nil)
436436
transaction_in_progress = false
437437
raise
438438
end
439+
@state = NO_TRANSACTION_STATE
439440
next
440441
else
441442
transaction_in_progress = false

spec/mongo/session_transaction_spec.rb

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,15 @@ class SessionTransactionSpecError < StandardError; end
117117
max_example_run_time 7
118118

119119
it 'times out' do
120-
warp = Mongo::Utils.monotonic_time + 200
121-
entered = false
120+
start = Mongo::Utils.monotonic_time
122121

123-
Thread.new do
124-
until entered
125-
sleep 0.1
126-
end
127-
allow(Mongo::Utils).to receive(:monotonic_time).and_return(warp)
128-
end
122+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start)
123+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start + 1)
124+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start + 2)
125+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start + 200)
129126

130127
expect do
131128
session.with_transaction do
132-
entered = true
133-
134-
# This sleep is to give the interrupting thread a chance to run,
135-
# it significantly affects how much time is burned in this
136-
# looping thread
137-
sleep 0.1
138-
139129
exc = Mongo::Error::OperationFailure.new('timeout test')
140130
exc.add_label('TransientTransactionError')
141131
raise exc
@@ -148,21 +138,23 @@ class SessionTransactionSpecError < StandardError; end
148138
context "timeout with commit raising with #{label}" do
149139
max_example_run_time 7
150140

141+
# JRuby seems to burn through the monotonic time expectations
142+
# very quickly and the retries of the transaction get the original
143+
# time which causes the transaction to be stuck there.
144+
fails_on_jruby
145+
151146
before do
152147
# create collection if it does not exist
153148
collection.insert_one(a: 1)
154149
end
155150

156-
it 'times out' do
157-
warp = Mongo::Utils.monotonic_time + 200
158-
entered = false
151+
it 'times out', retry: 3 do
152+
start = Mongo::Utils.monotonic_time
159153

160-
Thread.new do
161-
until entered
162-
sleep 0.1
163-
end
164-
allow(Mongo::Utils).to receive(:monotonic_time).and_return(warp)
154+
10.times do |i|
155+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start + i)
165156
end
157+
expect(Mongo::Utils).to receive(:monotonic_time).ordered.and_return(start + 200)
166158

167159
exc = Mongo::Error::OperationFailure.new('timeout test')
168160
exc.add_label(label)
@@ -171,13 +163,6 @@ class SessionTransactionSpecError < StandardError; end
171163

172164
expect do
173165
session.with_transaction do
174-
entered = true
175-
176-
# This sleep is to give the interrupting thread a chance to run,
177-
# it significantly affects how much time is burned in this
178-
# looping thread
179-
sleep 0.1
180-
181166
collection.insert_one(a: 2)
182167
end
183168
end.to raise_error(Mongo::Error::OperationFailure, 'timeout test')

0 commit comments

Comments
 (0)