Skip to content

Commit 804f45b

Browse files
test(firestore): fix flaky BulkWriter thread tests
Assert on individual thread pool executor configuration parameters (max_length) instead of counting the global VM thread list size. This fixes flakiness caused by: - Global active VM thread count instability. - Timing race conditions on thread execution lifetimes. Please note that the original test was checking the sum of both thread pools, and now we are checking each pool individually and directly
1 parent 3b8ce23 commit 804f45b

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

google-cloud-firestore/test/google/cloud/firestore/bulkwriter_test.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,20 @@
209209
stub = BatchWriteStub.new responses, requests, 1
210210
firestore.service.instance_variable_set :@firestore, stub
211211

212-
thread_count = Thread.list.count
213212
bw = firestore.bulk_writer request_threads: 1, batch_threads: 2
214213

215214
result_1 = bw.create "cities/NYC", { foo: "bar"}
216215
result_2 = bw.create "cities/MTV", { foo: "bar"}
217216
result_3 = bw.create "cities/KIR", { foo: "bar"}
218217
bw.flush
219-
thread_count_2 = Thread.list.count
218+
219+
write_tp = bw.instance_variable_get(:@write_thread_pool)
220+
batch_tp = bw.instance_variable_get(:@scheduler).instance_variable_get(:@batch_thread_pool)
221+
_(write_tp.max_length).must_equal 1
222+
_(batch_tp.max_length).must_equal 2
223+
220224
bw.close
221225

222-
_(thread_count_2 - thread_count).must_be :==, 3
223226
_(result_1.value).must_be_kind_of Google::Cloud::Firestore::BulkWriterOperation::WriteResult
224227
_(result_2.value).must_be_kind_of Google::Cloud::Firestore::BulkWriterOperation::WriteResult
225228
_(result_3.value).must_be_kind_of Google::Cloud::Firestore::BulkWriterOperation::WriteResult
@@ -231,18 +234,19 @@
231234
stub = BatchWriteStub.new [], [], 1
232235
firestore.service.instance_variable_set :@firestore, stub
233236

234-
thread_count = Thread.list.count
235237
bw = firestore.bulk_writer batch_threads: 3, request_threads: 1, retries: 1
236238

237239
(1..100).each do
238240
bw.create "cities/#{SecureRandom.hex(4)}", { foo: "bar"}
239241
end
240242
bw.flush
241-
thread_count_2 = Thread.list.count
242243

243-
bw.close
244+
write_tp = bw.instance_variable_get(:@write_thread_pool)
245+
batch_tp = bw.instance_variable_get(:@scheduler).instance_variable_get(:@batch_thread_pool)
246+
_(write_tp.max_length).must_equal 1
247+
_(batch_tp.max_length).must_equal 3
244248

245-
_(thread_count_2 - thread_count).must_be :==, 4
249+
bw.close
246250
end
247251
end
248252

0 commit comments

Comments
 (0)