@@ -52,8 +52,8 @@ defmodule TransactionTest do
52
52
log = & send ( parent , & 1 )
53
53
54
54
assert P . transaction ( pool , fn ( _ ) ->
55
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
56
- assert % { query: :begin , params: nil , result: { :ok , :began } } = entry
55
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
56
+ assert % { query: :begin , params: nil , result: { :ok , _ , :began } } = entry
57
57
assert is_integer ( entry . pool_time )
58
58
assert entry . pool_time >= 0
59
59
assert is_integer ( entry . connection_time )
@@ -63,19 +63,19 @@ defmodule TransactionTest do
63
63
:result
64
64
end , [ log: log ] ) == { :ok , :result }
65
65
66
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
66
+ assert_received % DBConnection.LogEntry { call: :commit } = entry
67
67
assert % { query: :commit , params: nil , result: { :ok , :committed } } = entry
68
68
assert is_nil ( entry . pool_time )
69
69
assert is_integer ( entry . connection_time )
70
70
assert entry . connection_time >= 0
71
71
assert is_nil ( entry . decode_time )
72
72
73
73
assert P . transaction ( pool , fn ( conn ) ->
74
- assert_received % DBConnection.LogEntry { }
74
+ assert_received % DBConnection.LogEntry { call: :begin }
75
75
P . rollback ( conn , :result )
76
76
end , [ log: log ] ) == { :error , :result }
77
77
78
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
78
+ assert_received % DBConnection.LogEntry { call: :rollback } = entry
79
79
assert % { query: :rollback , params: nil , result: { :ok , :rolledback } } = entry
80
80
assert is_nil ( entry . pool_time )
81
81
assert is_integer ( entry . connection_time )
@@ -304,7 +304,7 @@ defmodule TransactionTest do
304
304
P . transaction ( pool , fn ( _ ) -> flunk ( "transaction ran" ) end , [ log: log ] )
305
305
end
306
306
307
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
307
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
308
308
assert % { query: :begin , params: nil , result: { :error , ^ err } } = entry
309
309
assert is_integer ( entry . pool_time )
310
310
assert entry . pool_time >= 0
@@ -341,7 +341,7 @@ defmodule TransactionTest do
341
341
P . transaction ( pool , fn ( _ ) -> flunk ( "transaction ran" ) end , [ log: log ] )
342
342
end
343
343
344
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
344
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
345
345
assert % { query: :begin , params: nil , result: { :error , err } } = entry
346
346
assert % DBConnection.ConnectionError { message: "an exception was raised: ** (RuntimeError) oops" <> _ } = err
347
347
assert is_integer ( entry . pool_time )
@@ -494,11 +494,11 @@ defmodule TransactionTest do
494
494
assert_raise RuntimeError , "oops" ,
495
495
fn ( ) ->
496
496
P . transaction ( pool , fn ( _ ) ->
497
- assert_received % DBConnection.LogEntry { }
497
+ assert_received % DBConnection.LogEntry { call: :begin }
498
498
end , [ log: log ] )
499
499
end
500
500
501
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
501
+ assert_received % DBConnection.LogEntry { call: :commit } = entry
502
502
assert % { query: :commit , params: nil , result: { :error , ^ err } } = entry
503
503
assert is_nil ( entry . pool_time )
504
504
assert is_integer ( entry . connection_time )
@@ -635,10 +635,10 @@ defmodule TransactionTest do
635
635
P . transaction ( pool , fn ( _ ) -> :ok end , [ log: log ] )
636
636
end
637
637
638
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
639
- assert % { query: :begin , params: nil , result: { :ok , :began } } = entry
638
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
639
+ assert % { query: :begin , params: nil , result: { :ok , _ , :began } } = entry
640
640
641
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
641
+ assert_received % DBConnection.LogEntry { call: :commit } = entry
642
642
assert % { query: :commit , params: nil , result: { :error , err } } = entry
643
643
assert % DBConnection.ConnectionError { message: "an exception was raised: ** (RuntimeError) oops" <> _ } = err
644
644
assert is_nil ( entry . pool_time )
@@ -725,10 +725,10 @@ defmodule TransactionTest do
725
725
P . transaction ( pool , & P . rollback ( & 1 , :oops ) , [ log: log ] )
726
726
end
727
727
728
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
729
- assert % { query: :begin , params: nil , result: { :ok , :began } } = entry
728
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
729
+ assert % { query: :begin , params: nil , result: { :ok , _ , :began } } = entry
730
730
731
- assert_received % DBConnection.LogEntry { call: :transaction } = entry
731
+ assert_received % DBConnection.LogEntry { call: :rollback } = entry
732
732
assert % { query: :rollback , params: nil , result: { :error , err } } = entry
733
733
assert % DBConnection.ConnectionError { message: "an exception was raised: ** (RuntimeError) oops" <> _ } = err
734
734
assert is_nil ( entry . pool_time )
@@ -760,17 +760,197 @@ defmodule TransactionTest do
760
760
assert_raise RuntimeError , "oops" ,
761
761
fn ( ) ->
762
762
P . transaction ( pool , fn ( _ ) ->
763
- assert_received % DBConnection.LogEntry { call: :transaction ,
764
- query: :begin }
763
+ assert_received % DBConnection.LogEntry { call: :begin , query: :begin }
765
764
raise "oops"
766
765
end , [ log: log ] )
767
766
end
768
767
769
- assert_received % DBConnection.LogEntry { call: :transaction , query: :rollback }
768
+ assert_received % DBConnection.LogEntry { call: :rollback , query: :rollback }
770
769
771
770
assert [
772
771
connect: [ _ ] ,
773
772
handle_begin: [ _ , :state ] ,
774
773
handle_rollback: [ _ , :new_state ] ] = A . record ( agent )
775
774
end
775
+
776
+ test "transaction logs begin :commit and :rollback" do
777
+ stack = [
778
+ { :ok , :state } ,
779
+ { :commit , :new_state } ,
780
+ { :rollback , :newer_state } ,
781
+ ]
782
+ { :ok , agent } = A . start_link ( stack )
783
+
784
+ parent = self ( )
785
+ opts = [ agent: agent , parent: parent ]
786
+ { :ok , pool } = P . start_link ( opts )
787
+
788
+ log = & send ( parent , & 1 )
789
+
790
+ assert P . transaction ( pool , fn ( _ ) -> flunk ( "transaction ran" ) end ,
791
+ [ log: log ] ) == { :error , :rollback }
792
+
793
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
794
+ err = DBConnection.TransactionError . exception ( :commit )
795
+ assert % { query: :begin , params: nil , result: { :error , ^ err } } = entry
796
+ assert is_integer ( entry . pool_time )
797
+ assert entry . pool_time >= 0
798
+ assert is_integer ( entry . connection_time )
799
+ assert entry . connection_time >= 0
800
+ assert is_nil ( entry . decode_time )
801
+
802
+ refute_received % DBConnection.LogEntry { }
803
+
804
+ assert P . transaction ( pool , fn ( _ ) -> flunk ( "transaction ran" ) end ,
805
+ [ log: log ] ) == { :error , :rollback }
806
+
807
+ assert_received % DBConnection.LogEntry { call: :begin } = entry
808
+ err = DBConnection.TransactionError . exception ( :rollback )
809
+ assert % { query: :begin , params: nil , result: { :error , ^ err } } = entry
810
+ assert is_integer ( entry . pool_time )
811
+ assert entry . pool_time >= 0
812
+ assert is_integer ( entry . connection_time )
813
+ assert entry . connection_time >= 0
814
+ assert is_nil ( entry . decode_time )
815
+
816
+ refute_received % DBConnection.LogEntry { }
817
+
818
+ assert [
819
+ connect: [ _ ] ,
820
+ handle_begin: [ _ , :state ] ,
821
+ handle_begin: [ _ , :new_state ] ] = A . record ( agent )
822
+ end
823
+
824
+ test "transaction logs commit :begin and :rollback" do
825
+ stack = [
826
+ { :ok , :state } ,
827
+ { :ok , :began , :new_state } ,
828
+ { :begin , :newer_state } ,
829
+ { :ok , :began , :newest_state } ,
830
+ { :rollback , :state2 } ,
831
+ { :ok , :rolledback , :new_state2 }
832
+ ]
833
+ { :ok , agent } = A . start_link ( stack )
834
+
835
+ parent = self ( )
836
+ opts = [ agent: agent , parent: parent ]
837
+ { :ok , pool } = P . start_link ( opts )
838
+
839
+ log = & send ( parent , & 1 )
840
+
841
+ assert P . transaction ( pool ,
842
+ fn ( _ ) -> assert_receive % DBConnection.LogEntry { call: :begin } end ,
843
+ [ log: log ] ) == { :error , :rollback }
844
+
845
+ assert_received % DBConnection.LogEntry { call: :commit } = entry
846
+ err = DBConnection.TransactionError . exception ( :begin )
847
+ assert % { query: :commit , params: nil , result: { :error , ^ err } } = entry
848
+ assert is_nil ( entry . pool_time )
849
+ assert is_integer ( entry . connection_time )
850
+ assert entry . connection_time >= 0
851
+ assert is_nil ( entry . decode_time )
852
+
853
+ refute_received % DBConnection.LogEntry { }
854
+
855
+ assert P . transaction ( pool ,
856
+ fn ( _ ) -> assert_receive % DBConnection.LogEntry { call: :begin } end ,
857
+ [ log: log ] ) == { :error , :rollback }
858
+
859
+ assert_received % DBConnection.LogEntry { call: :commit } = entry
860
+ assert % { query: :rollback , params: nil , result: { :ok , :rolledback } } = entry
861
+ assert is_nil ( entry . pool_time )
862
+ assert is_integer ( entry . connection_time )
863
+ assert entry . connection_time >= 0
864
+ assert is_nil ( entry . decode_time )
865
+
866
+ refute_received % DBConnection.LogEntry { }
867
+
868
+ assert [
869
+ connect: [ _ ] ,
870
+ handle_begin: [ _ , :state ] ,
871
+ handle_commit: [ _ , :new_state ] ,
872
+ handle_begin: [ _ , :newer_state ] ,
873
+ handle_commit: [ _ , :newest_state ] ,
874
+ handle_rollback: [ _ , :state2 ] ] = A . record ( agent )
875
+ end
876
+
877
+ test "transaction logs rollback :begin" do
878
+ stack = [
879
+ { :ok , :state } ,
880
+ { :ok , :began , :new_state } ,
881
+ { :begin , :newer_state }
882
+ ]
883
+ { :ok , agent } = A . start_link ( stack )
884
+
885
+ parent = self ( )
886
+ opts = [ agent: agent , parent: parent ]
887
+ { :ok , pool } = P . start_link ( opts )
888
+
889
+ log = & send ( parent , & 1 )
890
+
891
+ assert P . transaction ( pool ,
892
+ fn ( conn ) ->
893
+ assert_receive % DBConnection.LogEntry { call: :begin }
894
+ P . rollback ( conn , :oops )
895
+ end ,
896
+ [ log: log ] ) == { :error , :oops }
897
+
898
+ assert_received % DBConnection.LogEntry { call: :rollback } = entry
899
+ err = DBConnection.TransactionError . exception ( :begin )
900
+ assert % { query: :rollback , params: nil , result: { :error , ^ err } } = entry
901
+ assert is_nil ( entry . pool_time )
902
+ assert is_integer ( entry . connection_time )
903
+ assert entry . connection_time >= 0
904
+ assert is_nil ( entry . decode_time )
905
+
906
+ refute_received % DBConnection.LogEntry { }
907
+
908
+ assert [
909
+ connect: [ _ ] ,
910
+ handle_begin: [ _ , :state ] ,
911
+ handle_rollback: [ _ , :new_state ] ] = A . record ( agent )
912
+ end
913
+
914
+ test "transaction with explicit begin/rollback/commit call causes rollback" do
915
+ stack = [
916
+ { :ok , :state } ,
917
+ { :ok , :began , :new_state } ,
918
+ { :ok , :rolledback , :newer_state } ,
919
+ { :ok , :began , :newest_state } ,
920
+ { :ok , :rolledback , :state2 } ,
921
+ { :ok , :began , :new_state2 } ,
922
+ { :ok , :rolledback , :newer_state2 }
923
+ ]
924
+ { :ok , agent } = A . start_link ( stack )
925
+
926
+ opts = [ agent: agent , parent: self ( ) ]
927
+ { :ok , pool } = P . start_link ( opts )
928
+
929
+ assert P . transaction ( pool , fn ( conn ) ->
930
+ assert_raise DBConnection.ConnectionError ,
931
+ "can not begin inside legacy transaction" ,
932
+ fn -> P . begin! ( conn ) end
933
+ end ) == { :error , :rollback }
934
+
935
+ assert P . transaction ( pool , fn ( conn ) ->
936
+ assert_raise DBConnection.ConnectionError ,
937
+ "can not rollback inside legacy transaction" ,
938
+ fn -> P . rollback! ( conn ) end
939
+ end ) == { :error , :rollback }
940
+
941
+ assert P . transaction ( pool , fn ( conn ) ->
942
+ assert_raise DBConnection.ConnectionError ,
943
+ "can not commit inside legacy transaction" ,
944
+ fn -> P . commit! ( conn ) end
945
+ end ) == { :error , :rollback }
946
+
947
+ assert [
948
+ connect: [ _ ] ,
949
+ handle_begin: [ _ , :state ] ,
950
+ handle_rollback: [ _ , :new_state ] ,
951
+ handle_begin: [ _ , :newer_state ] ,
952
+ handle_rollback: [ _ , :newest_state ] ,
953
+ handle_begin: [ _ , :state2 ] ,
954
+ handle_rollback: [ _ , :new_state2 ] ] = A . record ( agent )
955
+ end
776
956
end
0 commit comments