@@ -835,21 +835,32 @@ func TestConnection(t *testing.T) {
835
835
})
836
836
837
837
t .Run ("pinning" , func (t * testing.T ) {
838
- makeConnection := func (t * testing.T ) (* pool , * Connection ) {
838
+ makeMultipleConnections := func (t * testing.T , numConns int ) (* pool , [] * Connection ) {
839
839
t .Helper ()
840
840
841
841
addr := address .Address ("" )
842
- conn , err := newConnection (addr )
843
- assert .Nil (t , err , "newConnection error: %v" , err )
844
-
845
842
pool , err := newPool (poolConfig {Address : addr })
846
843
assert .Nil (t , err , "newPool error: %v" , err )
847
- conn .pool = pool
848
- err = pool .sem .Acquire (context .Background (), 1 )
849
- assert .Nil (t , err , "error acquring semaphore: %v" , err )
850
844
851
- return pool , & Connection {connection : conn }
845
+ err = pool .sem .Acquire (context .Background (), int64 (numConns ))
846
+ assert .Nil (t , err , "error acquiring semaphore: %v" , err )
847
+
848
+ conns := make ([]* Connection , 0 , numConns )
849
+ for i := 0 ; i < numConns ; i ++ {
850
+ conn , err := newConnection (addr )
851
+ assert .Nil (t , err , "newConnection error: %v" , err )
852
+ conn .pool = pool
853
+ conns = append (conns , & Connection {connection : conn })
854
+ }
855
+ return pool , conns
856
+ }
857
+ makeOneConnection := func (t * testing.T ) (* pool , * Connection ) {
858
+ t .Helper ()
859
+
860
+ pool , conns := makeMultipleConnections (t , 1 )
861
+ return pool , conns [0 ]
852
862
}
863
+
853
864
assertPoolPinnedStats := func (t * testing.T , p * pool , cursorConns , txnConns uint64 ) {
854
865
t .Helper ()
855
866
@@ -860,7 +871,7 @@ func TestConnection(t *testing.T) {
860
871
}
861
872
862
873
t .Run ("cursors" , func (t * testing.T ) {
863
- pool , conn := makeConnection (t )
874
+ pool , conn := makeOneConnection (t )
864
875
err := conn .PinToCursor ()
865
876
assert .Nil (t , err , "PinToCursor error: %v" , err )
866
877
assertPoolPinnedStats (t , pool , 1 , 0 )
@@ -873,7 +884,7 @@ func TestConnection(t *testing.T) {
873
884
assertPoolPinnedStats (t , pool , 0 , 0 )
874
885
})
875
886
t .Run ("transactions" , func (t * testing.T ) {
876
- pool , conn := makeConnection (t )
887
+ pool , conn := makeOneConnection (t )
877
888
err := conn .PinToTransaction ()
878
889
assert .Nil (t , err , "PinToTransaction error: %v" , err )
879
890
assertPoolPinnedStats (t , pool , 0 , 1 )
@@ -886,7 +897,7 @@ func TestConnection(t *testing.T) {
886
897
assertPoolPinnedStats (t , pool , 0 , 0 )
887
898
})
888
899
t .Run ("pool is only updated for first reference" , func (t * testing.T ) {
889
- pool , conn := makeConnection (t )
900
+ pool , conn := makeOneConnection (t )
890
901
err := conn .PinToTransaction ()
891
902
assert .Nil (t , err , "PinToTransaction error: %v" , err )
892
903
assertPoolPinnedStats (t , pool , 0 , 1 )
@@ -907,6 +918,48 @@ func TestConnection(t *testing.T) {
907
918
assert .Nil (t , err , "Close error: %v" , err )
908
919
assertPoolPinnedStats (t , pool , 0 , 0 )
909
920
})
921
+ t .Run ("multiple connections from a pool" , func (t * testing.T ) {
922
+ pool , conns := makeMultipleConnections (t , 2 )
923
+ first , second := conns [0 ], conns [1 ]
924
+
925
+ err := first .PinToTransaction ()
926
+ assert .Nil (t , err , "PinToTransaction error: %v" , err )
927
+ err = second .PinToCursor ()
928
+ assert .Nil (t , err , "PinToCursor error: %v" , err )
929
+ assertPoolPinnedStats (t , pool , 1 , 1 )
930
+
931
+ err = first .UnpinFromTransaction ()
932
+ assert .Nil (t , err , "UnpinFromTransaction error: %v" , err )
933
+ err = first .Close ()
934
+ assert .Nil (t , err , "Close error: %v" , err )
935
+ assertPoolPinnedStats (t , pool , 1 , 0 )
936
+
937
+ err = second .UnpinFromCursor ()
938
+ assert .Nil (t , err , "UnpinFromCursor error: %v" , err )
939
+ err = second .Close ()
940
+ assert .Nil (t , err , "Close error: %v" , err )
941
+ assertPoolPinnedStats (t , pool , 0 , 0 )
942
+ })
943
+ t .Run ("close is ignored if connection is pinned" , func (t * testing.T ) {
944
+ pool , conn := makeOneConnection (t )
945
+ err := conn .PinToCursor ()
946
+ assert .Nil (t , err , "PinToCursor error: %v" , err )
947
+
948
+ err = conn .Close ()
949
+ assert .Nil (t , err , "Close error" )
950
+ assert .NotNil (t , conn .connection , "expected connection to be pinned but it was released to the pool" )
951
+ assertPoolPinnedStats (t , pool , 1 , 0 )
952
+ })
953
+ t .Run ("expire forcefully returns connection to pool" , func (t * testing.T ) {
954
+ pool , conn := makeOneConnection (t )
955
+ err := conn .PinToCursor ()
956
+ assert .Nil (t , err , "PinToCursor error: %v" , err )
957
+
958
+ err = conn .Expire ()
959
+ assert .Nil (t , err , "Expire error" )
960
+ assert .Nil (t , conn .connection , "expected connection to be released to the pool but was not" )
961
+ assertPoolPinnedStats (t , pool , 0 , 0 )
962
+ })
910
963
})
911
964
})
912
965
}
0 commit comments