Skip to content

Commit 043ef08

Browse files
author
Divjot Arora
committed
add new tests
1 parent 9466223 commit 043ef08

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

x/mongo/driver/topology/connection_test.go

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -835,21 +835,32 @@ func TestConnection(t *testing.T) {
835835
})
836836

837837
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) {
839839
t.Helper()
840840

841841
addr := address.Address("")
842-
conn, err := newConnection(addr)
843-
assert.Nil(t, err, "newConnection error: %v", err)
844-
845842
pool, err := newPool(poolConfig{Address: addr})
846843
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)
850844

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]
852862
}
863+
853864
assertPoolPinnedStats := func(t *testing.T, p *pool, cursorConns, txnConns uint64) {
854865
t.Helper()
855866

@@ -860,7 +871,7 @@ func TestConnection(t *testing.T) {
860871
}
861872

862873
t.Run("cursors", func(t *testing.T) {
863-
pool, conn := makeConnection(t)
874+
pool, conn := makeOneConnection(t)
864875
err := conn.PinToCursor()
865876
assert.Nil(t, err, "PinToCursor error: %v", err)
866877
assertPoolPinnedStats(t, pool, 1, 0)
@@ -873,7 +884,7 @@ func TestConnection(t *testing.T) {
873884
assertPoolPinnedStats(t, pool, 0, 0)
874885
})
875886
t.Run("transactions", func(t *testing.T) {
876-
pool, conn := makeConnection(t)
887+
pool, conn := makeOneConnection(t)
877888
err := conn.PinToTransaction()
878889
assert.Nil(t, err, "PinToTransaction error: %v", err)
879890
assertPoolPinnedStats(t, pool, 0, 1)
@@ -886,7 +897,7 @@ func TestConnection(t *testing.T) {
886897
assertPoolPinnedStats(t, pool, 0, 0)
887898
})
888899
t.Run("pool is only updated for first reference", func(t *testing.T) {
889-
pool, conn := makeConnection(t)
900+
pool, conn := makeOneConnection(t)
890901
err := conn.PinToTransaction()
891902
assert.Nil(t, err, "PinToTransaction error: %v", err)
892903
assertPoolPinnedStats(t, pool, 0, 1)
@@ -907,6 +918,48 @@ func TestConnection(t *testing.T) {
907918
assert.Nil(t, err, "Close error: %v", err)
908919
assertPoolPinnedStats(t, pool, 0, 0)
909920
})
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+
})
910963
})
911964
})
912965
}

0 commit comments

Comments
 (0)