Skip to content

Commit a1d5609

Browse files
authored
Merge pull request #916 from estolfo/event-sub
Use EventSubscriber singleton and common subscribed client in tests
2 parents 9710973 + 9316344 commit a1d5609

File tree

14 files changed

+123
-194
lines changed

14 files changed

+123
-194
lines changed

spec/mongo/bulk_write_spec.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,32 +1889,26 @@
18891889
it_behaves_like 'an operation using a session'
18901890
end
18911891

1892-
context 'when retryable writes are supported', if: (sessions_enabled? && (replica_set? || sharded?)) do
1892+
context 'when retryable writes are supported', if: test_sessions? do
18931893

18941894
let(:client) do
1895-
authorized_client.with(heartbeat_frequency: 100, retry_writes: true).tap do |cl|
1896-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
1897-
end
1895+
authorized_client_with_retry_writes
18981896
end
18991897

19001898
let(:collection) do
19011899
client[authorized_collection.name]
19021900
end
19031901

1904-
let(:subscriber) do
1905-
EventSubscriber.new
1906-
end
1907-
19081902
let!(:result) do
19091903
bulk_write.execute
19101904
end
19111905

19121906
let(:first_txn_number) do
1913-
subscriber.started_events[-2].command['txnNumber'].instance_variable_get(:@integer)
1907+
EventSubscriber.started_events[-2].command['txnNumber'].instance_variable_get(:@integer)
19141908
end
19151909

19161910
let(:second_txn_number) do
1917-
subscriber.started_events[-1].command['txnNumber'].instance_variable_get(:@integer)
1911+
EventSubscriber.started_events[-1].command['txnNumber'].instance_variable_get(:@integer)
19181912
end
19191913

19201914
it 'inserts the documents' do

spec/mongo/client_spec.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,24 +1360,23 @@
13601360
end
13611361

13621362
let(:client) do
1363-
# Monitoring subscribers won't be set up when using Client#with, so a new client must be created.
13641363
Mongo::Client.new(ADDRESSES, client_options).tap do |cl|
1365-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
1364+
cl.subscribe(Mongo::Monitoring::COMMAND, EventSubscriber.clear_events!)
13661365
end
13671366
end
13681367

1369-
let(:subscriber) do
1370-
EventSubscriber.new
1371-
end
1372-
13731368
let(:command) do
1374-
subscriber.started_events.find { |c| c.command_name == :listDatabases }.command
1369+
EventSubscriber.started_events.find { |c| c.command_name == :listDatabases }.command
13751370
end
13761371

13771372
before do
13781373
client.list_databases({}, true)
13791374
end
13801375

1376+
after do
1377+
client.close
1378+
end
1379+
13811380
it 'sends the command with the nameOnly flag set to true' do
13821381
expect(command[:nameOnly]).to be(true)
13831382
end

spec/mongo/collection/view/aggregation_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,20 @@
231231
end
232232

233233
let(:client) do
234-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
235-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
236-
end
234+
subscribed_client
237235
end
238236

239237
let(:session) do
240238
client.start_session
241239
end
242240

243-
let(:subscriber) do
244-
EventSubscriber.new
245-
end
246-
247241
let(:view) do
248242
Mongo::Collection::View.new(client[TEST_COLL], selector, view_options)
249243
end
250244

251245
let(:command) do
252246
aggregation.explain
253-
subscriber.started_events.find { |c| c.command_name == 'aggregate'}.command
247+
EventSubscriber.started_events.find { |c| c.command_name == 'aggregate'}.command
254248
end
255249

256250
it 'sends the session id' do

spec/mongo/collection/view/map_reduce_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,11 @@
240240
end
241241

242242
let(:client) do
243-
authorized_client.with(monitoring: true).tap do |cl|
244-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
245-
end
246-
end
247-
248-
let(:subscriber) do
249-
EventSubscriber.new
243+
subscribed_client
250244
end
251245

252246
let(:find_command) do
253-
subscriber.started_events[-1].command
247+
EventSubscriber.started_events[-1].command
254248
end
255249

256250
before do

spec/mongo/collection/view/readable_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191

192192
let(:command) do
193193
operation
194-
subscriber.started_events.find { |cmd| cmd.command_name == 'mapreduce' }.command
194+
EventSubscriber.started_events.find { |cmd| cmd.command_name == 'mapreduce' }.command
195195
end
196196

197197
it_behaves_like 'an operation supporting causally consistent reads'

spec/mongo/collection_spec.rb

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -900,26 +900,20 @@
900900
end
901901

902902
let(:client) do
903-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
904-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
905-
end
903+
subscribed_client
906904
end
907905

908906
let(:session) do
909907
client.start_session
910908
end
911909

912-
let(:subscriber) do
913-
EventSubscriber.new
914-
end
915-
916910
let(:view) do
917911
Mongo::Collection::View.new(client[TEST_COLL], selector, view_options)
918912
end
919913

920914
let(:command) do
921915
client[TEST_COLL].find({}, session: session).explain
922-
subscriber.started_events.find { |c| c.command_name == :explain }.command
916+
EventSubscriber.started_events.find { |c| c.command_name == :explain }.command
923917
end
924918

925919
it 'sends the session id' do
@@ -935,7 +929,7 @@
935929

936930
let(:command) do
937931
operation
938-
subscriber.started_events.find { |cmd| cmd.command_name == 'find' }.command
932+
EventSubscriber.started_events.find { |cmd| cmd.command_name == 'find' }.command
939933
end
940934

941935
it_behaves_like 'an operation supporting causally consistent reads'
@@ -1205,13 +1199,7 @@ def generate
12051199
context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do
12061200

12071201
let(:client) do
1208-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
1209-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
1210-
end
1211-
end
1212-
1213-
let(:subscriber) do
1214-
EventSubscriber.new
1202+
subscribed_client
12151203
end
12161204

12171205
let(:documents) do
@@ -1227,7 +1215,7 @@ def generate
12271215
end
12281216

12291217
let(:insert_events) do
1230-
subscriber.started_events.select { |e| e.command_name == :insert }
1218+
EventSubscriber.started_events.select { |e| e.command_name == :insert }
12311219
end
12321220

12331221
it 'sends the documents in one OP_MSG' do
@@ -1574,7 +1562,7 @@ def generate
15741562

15751563
let(:command) do
15761564
operation
1577-
subscriber.started_events.find { |cmd| cmd.command_name == 'aggregate' }.command
1565+
EventSubscriber.started_events.find { |cmd| cmd.command_name == 'aggregate' }.command
15781566
end
15791567

15801568
it_behaves_like 'an operation supporting causally consistent reads'
@@ -1738,7 +1726,7 @@ def generate
17381726

17391727
let(:command) do
17401728
operation
1741-
subscriber.started_events.find { |cmd| cmd.command_name == :count }.command
1729+
EventSubscriber.started_events.find { |cmd| cmd.command_name == :count }.command
17421730
end
17431731

17441732
it_behaves_like 'an operation supporting causally consistent reads'
@@ -1852,7 +1840,7 @@ def generate
18521840

18531841
let(:command) do
18541842
operation
1855-
subscriber.started_events.find { |cmd| cmd.command_name == :distinct }.command
1843+
EventSubscriber.started_events.find { |cmd| cmd.command_name == :distinct }.command
18561844
end
18571845

18581846
it_behaves_like 'an operation supporting causally consistent reads'
@@ -2344,7 +2332,7 @@ def generate
23442332

23452333
let(:command) do
23462334
operation
2347-
subscriber.started_events.find { |cmd| cmd.command_name == :parallelCollectionScan }.command
2335+
EventSubscriber.started_events.find { |cmd| cmd.command_name == :parallelCollectionScan }.command
23482336
end
23492337

23502338
it_behaves_like 'an operation supporting causally consistent reads'
@@ -3486,13 +3474,7 @@ def generate
34863474
context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do
34873475

34883476
let(:client) do
3489-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
3490-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
3491-
end
3492-
end
3493-
3494-
let(:subscriber) do
3495-
EventSubscriber.new
3477+
subscribed_client
34963478
end
34973479

34983480
let(:documents) do
@@ -3509,7 +3491,7 @@ def generate
35093491
end
35103492

35113493
let(:update_events) do
3512-
subscriber.started_events.select { |e| e.command_name == :update }
3494+
EventSubscriber.started_events.select { |e| e.command_name == :update }
35133495
end
35143496

35153497
it 'sends the documents in one OP_MSG' do

spec/mongo/cursor_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
context 'when an implicit session is used', if: sessions_enabled? do
330330

331331
let(:collection) do
332-
authorized_client_with_subscriber[TEST_COLL]
332+
subscribed_client[TEST_COLL]
333333
end
334334

335335
before do
@@ -354,7 +354,7 @@
354354
end
355355

356356
let(:find_events) do
357-
event_subscriber.started_events.select { |e| e.command_name == "find" }
357+
EventSubscriber.started_events.select { |e| e.command_name == "find" }
358358
end
359359

360360
context 'when all results are retrieved in the first response' do

spec/mongo/database_spec.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,15 @@
256256
end
257257

258258
let(:client) do
259-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
260-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
261-
end
262-
end
263-
264-
let(:subscriber) do
265-
EventSubscriber.new
259+
subscribed_client
266260
end
267261

268262
it_behaves_like 'an operation using a session'
269263
it_behaves_like 'a failed operation using a session'
270264

271265

272266
let(:full_command) do
273-
subscriber.started_events.find { |cmd| cmd.command_name == :ismaster }.command
267+
EventSubscriber.started_events.find { |cmd| cmd.command_name == :ismaster }.command
274268
end
275269

276270
it 'does not add a afterClusterTime field' do

spec/mongo/retryable_writes_spec.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@
1717
end
1818

1919
let(:client) do
20-
authorized_client_with_retry_writes.tap do |cl|
21-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
22-
end
23-
end
24-
25-
let(:subscriber) do
26-
EventSubscriber.new
20+
authorized_client_with_retry_writes
2721
end
2822

2923
before do

spec/mongo/session/session_pool_spec.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,13 @@
150150
end
151151

152152
let(:client) do
153-
authorized_client.with(heartbeat_frequency: 100).tap do |cl|
154-
cl.subscribe(Mongo::Monitoring::COMMAND, subscriber)
155-
end
153+
subscribed_client
156154
end
157155

158156
after do
159157
client.close
160158
end
161159

162-
let(:subscriber) do
163-
EventSubscriber.new
164-
end
165-
166160
context 'when the number of ids is not larger than 10,000' do
167161

168162
before do
@@ -177,7 +171,7 @@
177171

178172
let(:end_sessions_command) do
179173
pool.end_sessions
180-
subscriber.started_events.find { |c| c.command_name == :endSessions}
174+
EventSubscriber.started_events.find { |c| c.command_name == :endSessions}
181175
end
182176

183177
it 'sends the endSessions command with all the session ids' do
@@ -216,7 +210,7 @@
216210
end
217211

218212
let(:end_sessions_commands) do
219-
subscriber.started_events.select { |c| c.command_name == :endSessions}
213+
EventSubscriber.started_events.select { |c| c.command_name == :endSessions}
220214
end
221215

222216
it 'sends the command more than once' do

0 commit comments

Comments
 (0)