|
2 | 2 |
|
3 | 3 | require "spec_helper"
|
4 | 4 |
|
| 5 | +def capture_exception |
| 6 | + e = nil |
| 7 | + begin |
| 8 | + yield |
| 9 | + rescue => ex |
| 10 | + e = ex |
| 11 | + end |
| 12 | + e |
| 13 | +end |
| 14 | + |
5 | 15 | describe Mongoid::Clients::Sessions do
|
6 | 16 | before(:all) do
|
7 | 17 | if Gem::Version.new(Mongo::VERSION) < Gem::Version.new('2.6')
|
|
200 | 210 |
|
201 | 211 | context 'using #with_session' do
|
202 | 212 | let!(:error) do
|
203 |
| - e = nil |
204 |
| - begin |
| 213 | + capture_exception do |
205 | 214 | Person.with_session do |s|
|
206 | 215 | s.start_transaction
|
207 | 216 | Person.create!
|
208 | 217 | Person.create!
|
209 | 218 | Post.create!
|
210 | 219 | s.commit_transaction
|
211 | 220 | end
|
212 |
| - rescue => ex |
213 |
| - e = ex |
214 | 221 | end
|
215 |
| - e |
216 | 222 | end
|
217 | 223 |
|
218 | 224 | include_examples 'it does not abort the transaction'
|
219 | 225 | end
|
220 | 226 |
|
221 | 227 | context 'using #transaction' do
|
222 | 228 | let!(:error) do
|
223 |
| - e = nil |
224 |
| - begin |
| 229 | + capture_exception do |
225 | 230 | Person.transaction do
|
226 | 231 | Person.create!
|
227 | 232 | Person.create!
|
228 | 233 | Post.create!
|
229 | 234 | end
|
230 |
| - rescue => ex |
231 |
| - e = ex |
232 | 235 | end
|
233 |
| - e |
234 | 236 | end
|
235 | 237 |
|
236 | 238 | include_examples 'it does not abort the transaction'
|
|
252 | 254 |
|
253 | 255 | context 'using #with_session' do
|
254 | 256 | let!(:error) do
|
255 |
| - e = nil |
256 |
| - begin |
| 257 | + capture_exception do |
257 | 258 | Person.with_session do |s|
|
258 | 259 | s.start_transaction
|
259 | 260 | s.start_transaction
|
260 | 261 | Person.create!
|
261 | 262 | Post.create!
|
262 | 263 | s.commit_transaction
|
263 | 264 | end
|
264 |
| - rescue => ex |
265 |
| - e = ex |
266 | 265 | end
|
267 |
| - e |
268 | 266 | end
|
269 | 267 |
|
270 | 268 | include_examples 'it aborts the transaction', Mongo::Error::InvalidTransactionOperation
|
271 | 269 | end
|
272 | 270 |
|
273 | 271 | context 'using #transaction' do
|
274 | 272 | let!(:error) do
|
275 |
| - e = nil |
276 |
| - begin |
| 273 | + capture_exception do |
277 | 274 | Person.transaction do
|
278 | 275 | Person.transaction do
|
279 | 276 | Person.create!
|
280 | 277 | Post.create!
|
281 | 278 | end
|
282 | 279 | end
|
283 |
| - rescue => ex |
284 |
| - e = ex |
285 | 280 | end
|
286 |
| - e |
287 | 281 | end
|
288 | 282 |
|
289 | 283 | include_examples 'it aborts the transaction', Mongoid::Errors::InvalidTransactionNesting
|
|
331 | 325 |
|
332 | 326 | context 'when Mongoid::Errors:Rollback raised' do
|
333 | 327 | let!(:error) do
|
334 |
| - error = nil |
335 |
| - begin |
| 328 | + capture_exception do |
336 | 329 | Person.transaction do
|
337 | 330 | Person.create!
|
338 | 331 | raise Mongoid::Errors::Rollback
|
339 | 332 | end
|
340 |
| - rescue => e |
341 |
| - error = e |
342 | 333 | end
|
343 |
| - error |
344 | 334 | end
|
345 | 335 |
|
346 | 336 | it 'does not bass on the exception' do
|
|
368 | 358 |
|
369 | 359 | context 'using #with_session' do
|
370 | 360 | let!(:error) do
|
371 |
| - e = nil |
372 |
| - begin |
| 361 | + capture_exception do |
373 | 362 | Person.with_session do |s|
|
374 | 363 | s.start_transaction
|
375 | 364 | Person.create!
|
376 | 365 | s.commit_transaction
|
377 | 366 | end
|
378 |
| - rescue => ex |
379 |
| - e = ex |
380 | 367 | end
|
381 |
| - e |
382 | 368 | end
|
383 | 369 |
|
384 | 370 | include_examples 'it raises a transactions not supported error'
|
385 | 371 | end
|
386 | 372 |
|
387 | 373 | context 'using #transaction' do
|
388 | 374 | let!(:error) do
|
389 |
| - e = nil |
390 |
| - begin |
| 375 | + capture_exception do |
391 | 376 | Person.transaction do
|
392 | 377 | Person.create!
|
393 | 378 | end
|
394 |
| - rescue => ex |
395 |
| - e = ex |
396 | 379 | end
|
397 |
| - e |
398 | 380 | end
|
399 | 381 |
|
400 | 382 | include_examples 'it raises a transactions not supported error'
|
|
537 | 519 |
|
538 | 520 | context 'using #with_session' do
|
539 | 521 | let!(:error) do
|
540 |
| - e = nil |
541 |
| - begin |
| 522 | + capture_exception do |
542 | 523 | person.with_session do |s|
|
543 | 524 | s.start_transaction
|
544 | 525 | person.username = 'Emily'
|
545 | 526 | person.save!
|
546 | 527 | person.posts << Post.create!
|
547 | 528 | s.commit_transaction
|
548 | 529 | end
|
549 |
| - rescue => ex |
550 |
| - e = ex |
551 | 530 | end
|
552 |
| - e |
553 | 531 | end
|
554 | 532 |
|
555 | 533 | include_examples 'does not abort the transaction'
|
556 | 534 | end
|
557 | 535 |
|
558 | 536 | context 'using #transaction' do
|
559 | 537 | let!(:error) do
|
560 |
| - e = nil |
561 |
| - begin |
| 538 | + capture_exception do |
562 | 539 | person.transaction do
|
563 | 540 | person.username = 'Emily'
|
564 | 541 | person.save!
|
565 | 542 | person.posts << Post.create!
|
566 | 543 | end
|
567 | 544 | rescue => ex
|
568 |
| - e = ex |
569 | 545 | end
|
570 |
| - e |
571 | 546 | end
|
572 | 547 |
|
573 | 548 | include_examples 'does not abort the transaction'
|
|
577 | 552 | context 'when transactions are nested' do
|
578 | 553 | context 'use #with_session' do
|
579 | 554 | let!(:error) do
|
580 |
| - e = nil |
581 |
| - begin |
| 555 | + capture_exception do |
582 | 556 | person.with_session do |s|
|
583 | 557 | s.start_transaction
|
584 | 558 | s.start_transaction
|
|
587 | 561 | person.posts << Post.create!
|
588 | 562 | s.commit_transaction
|
589 | 563 | end
|
590 |
| - rescue => ex |
591 |
| - e = ex |
592 | 564 | end
|
593 |
| - e |
594 | 565 | end
|
595 | 566 |
|
596 | 567 | it 'raises an error' do
|
|
606 | 577 |
|
607 | 578 | context 'use #transaction' do
|
608 | 579 | let!(:error) do
|
609 |
| - e = nil |
610 |
| - begin |
| 580 | + capture_exception do |
611 | 581 | person.transaction do
|
612 | 582 | person.transaction do
|
613 | 583 | person.username = 'Emily'
|
614 | 584 | person.save!
|
615 | 585 | person.posts << Post.create!
|
616 | 586 | end
|
617 | 587 | end
|
618 |
| - rescue => ex |
619 |
| - e = ex |
620 | 588 | end
|
621 |
| - e |
622 | 589 | end
|
623 | 590 |
|
624 | 591 | it 'raises an error' do
|
|
637 | 604 |
|
638 | 605 | context 'when Mongoid::Errors:Rollback raised' do
|
639 | 606 | let!(:error) do
|
640 |
| - error = nil |
641 |
| - begin |
| 607 | + capture_exception do |
642 | 608 | person.transaction do
|
643 | 609 | person.username = 'John'
|
644 | 610 | person.save!
|
645 | 611 | raise Mongoid::Errors::Rollback
|
646 | 612 | end
|
647 |
| - rescue => e |
648 |
| - error = e |
649 | 613 | end
|
650 |
| - error |
651 | 614 | end
|
652 | 615 |
|
653 | 616 | it 'does not bass on the exception' do
|
|
682 | 645 |
|
683 | 646 | context 'using #with_session' do
|
684 | 647 | let!(:error) do
|
685 |
| - e = nil |
686 |
| - begin |
| 648 | + capture_exception do |
687 | 649 | person.with_session do |s|
|
688 | 650 | s.start_transaction
|
689 | 651 | person.username = 'Emily'
|
690 | 652 | person.save!
|
691 | 653 | s.commit_transaction
|
692 | 654 | end
|
693 |
| - rescue => ex |
694 |
| - e = ex |
695 | 655 | end
|
696 |
| - e |
697 | 656 | end
|
698 | 657 |
|
699 | 658 | it 'raises a transactions not supported error' do
|
|
704 | 663 |
|
705 | 664 | context 'using #transaction' do
|
706 | 665 | let!(:error) do
|
707 |
| - e = nil |
708 |
| - begin |
| 666 | + capture_exception do |
709 | 667 | person.transaction do
|
710 | 668 | person.username = 'Emily'
|
711 | 669 | person.save!
|
712 | 670 | end
|
713 |
| - rescue => ex |
714 |
| - e = ex |
715 | 671 | end
|
716 |
| - e |
717 | 672 | end
|
718 | 673 |
|
719 | 674 | it 'raises a transactions not supported error' do
|
|
735 | 690 | end
|
736 | 691 | end
|
737 | 692 | end
|
| 693 | + |
| 694 | + context 'when a transaction is used on Mongoid module' do |
| 695 | + let(:subscriber) do |
| 696 | + Mongoid::Clients.with_name(:default).send(:monitoring).subscribers['Command'].find do |s| |
| 697 | + s.is_a?(EventSubscriber) |
| 698 | + end |
| 699 | + end |
| 700 | + |
| 701 | + before do |
| 702 | + Mongoid::Clients.with_name(:default).database.collections.each(&:drop) |
| 703 | + Person.collection.create |
| 704 | + Account.collection.create |
| 705 | + Mongoid::Clients.with_name(:default).subscribe(Mongo::Monitoring::COMMAND, EventSubscriber.new) |
| 706 | + subscriber.clear_events! |
| 707 | + end |
| 708 | + |
| 709 | + after do |
| 710 | + Mongoid::Clients.with_name(:default).database.collections.each(&:drop) |
| 711 | + end |
| 712 | + |
| 713 | + context 'when transactions are supported' do |
| 714 | + require_transaction_support |
| 715 | + |
| 716 | + context 'when no error raised' do |
| 717 | + before do |
| 718 | + Mongoid.transaction do |
| 719 | + Person.create! |
| 720 | + end |
| 721 | + end |
| 722 | + |
| 723 | + it 'commits the transacrion' do |
| 724 | + expect(other_events.count { |e| e.command_name == 'abortTransaction'}).to be(0) |
| 725 | + expect(other_events.count { |e| e.command_name == 'commitTransaction'}).to be(1) |
| 726 | + end |
| 727 | + |
| 728 | + it 'executes the commands inside the transaction' do |
| 729 | + expect(Person.count).to be(1) |
| 730 | + end |
| 731 | + end |
| 732 | + |
| 733 | + context 'When an error raised' do |
| 734 | + let!(:error) do |
| 735 | + capture_exception do |
| 736 | + Mongoid.transaction do |
| 737 | + Person.create! |
| 738 | + Account.create! |
| 739 | + end |
| 740 | + end |
| 741 | + end |
| 742 | + |
| 743 | + it 'aborts the transaction' do |
| 744 | + expect(other_events.count { |e| e.command_name == 'abortTransaction'}).to be(1) |
| 745 | + expect(other_events.count { |e| e.command_name == 'commitTransaction'}).to be(0) |
| 746 | + end |
| 747 | + |
| 748 | + it 'passes on the error' do |
| 749 | + expect(error).to be_a(Mongoid::Errors::Validations) |
| 750 | + end |
| 751 | + |
| 752 | + it 'reverts changes' do |
| 753 | + expect(Account.count).to be(0) |
| 754 | + expect(Person.count).to be(0) |
| 755 | + end |
| 756 | + end |
| 757 | + end |
| 758 | + end |
738 | 759 | end
|
0 commit comments