|
328 | 328 |
|
329 | 329 | context 'when an implicit session is used', if: sessions_enabled? do |
330 | 330 |
|
331 | | - let(:documents) do |
332 | | - (1..4).map{ |i| { field: "test#{i}" }} |
| 331 | + let(:collection) do |
| 332 | + authorized_client_with_subscriber[TEST_COLL] |
333 | 333 | end |
334 | 334 |
|
335 | 335 | before do |
336 | | - authorized_collection.insert_many(documents) |
| 336 | + collection.insert_many(documents) |
337 | 337 | end |
338 | 338 |
|
339 | 339 | after do |
340 | | - authorized_collection.delete_many |
341 | | - end |
342 | | - |
343 | | - let(:view) do |
344 | | - authorized_collection.find({}, batch_size: 2, limit: 4) |
| 340 | + collection.delete_many |
345 | 341 | end |
346 | 342 |
|
347 | 343 | let(:cursor) do |
|
352 | 348 | view.to_enum |
353 | 349 | end |
354 | 350 |
|
355 | | - let!(:server_session) do |
356 | | - enum.next |
357 | | - cursor.instance_variable_get(:@session).instance_variable_get(:@server_session) |
| 351 | + let(:session_pool_ids) do |
| 352 | + queue = view.client.cluster.session_pool.instance_variable_get(:@queue) |
| 353 | + queue.collect { |s| s.session_id } |
358 | 354 | end |
359 | 355 |
|
360 | | - let(:session_pool_queue) do |
361 | | - view.client.cluster.session_pool.instance_variable_get(:@queue) |
| 356 | + let(:find_events) do |
| 357 | + event_subscriber.started_events.select { |e| e.command_name == "find" } |
| 358 | + end |
| 359 | + |
| 360 | + context 'when all results are retrieved in the first response' do |
| 361 | + |
| 362 | + let(:documents) do |
| 363 | + (1..2).map{ |i| { field: "test#{i}" }} |
| 364 | + end |
| 365 | + |
| 366 | + let(:view) do |
| 367 | + collection.find |
| 368 | + end |
| 369 | + |
| 370 | + it 'returns the session to the cluster session pool' do |
| 371 | + 1.times { enum.next } |
| 372 | + expect(find_events.collect { |event| event.command['lsid'] }.uniq.size).to eq(1) |
| 373 | + expect(session_pool_ids).to include(find_events.collect { |event| event.command['lsid'] }.uniq.first) |
| 374 | + end |
362 | 375 | end |
363 | 376 |
|
364 | | - context 'when all results are retrieved from the server', if: sessions_enabled? && !sharded? do |
| 377 | + context 'when a getmore is needed to retrieve all results', if: sessions_enabled? && !sharded? do |
| 378 | + |
| 379 | + let(:documents) do |
| 380 | + (1..4).map{ |i| { field: "test#{i}" }} |
| 381 | + end |
| 382 | + |
| 383 | + let(:view) do |
| 384 | + collection.find({}, batch_size: 2, limit: 4) |
| 385 | + end |
| 386 | + |
365 | 387 |
|
366 | 388 | context 'when not all documents are iterated' do |
367 | 389 |
|
368 | 390 | it 'returns the session to the cluster session pool' do |
369 | | - 2.times { enum.next } |
370 | | - expect(session_pool_queue).to include(server_session) |
| 391 | + 3.times { enum.next } |
| 392 | + expect(find_events.collect { |event| event.command['lsid'] }.uniq.size).to eq(1) |
| 393 | + expect(session_pool_ids).to include(find_events.collect { |event| event.command['lsid'] }.uniq.first) |
371 | 394 | end |
372 | 395 | end |
373 | 396 |
|
374 | 397 | context 'when all documents are iterated' do |
375 | 398 |
|
376 | 399 | it 'returns the session to the cluster session pool' do |
377 | | - 3.times { enum.next } |
378 | | - expect(session_pool_queue).to include(server_session) |
| 400 | + 4.times { enum.next } |
| 401 | + expect(find_events.collect { |event| event.command['lsid'] }.uniq.size).to eq(1) |
| 402 | + expect(session_pool_ids).to include(find_events.collect { |event| event.command['lsid'] }.uniq.first) |
379 | 403 | end |
380 | 404 | end |
381 | 405 | end |
|
0 commit comments