Skip to content

Commit 5759bd7

Browse files
committed
Add unit tests for ConnectionPool borrowing, releasing, and timeout handling
1 parent f590397 commit 5759bd7

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/pool/connection_pool_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pool
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
// TestConnectionPoolBorrowRelease tests borrowing and releasing connections from the pool.
9+
func TestConnectionPoolBorrowRelease(t *testing.T) {
10+
pool := NewConnectionPool(2, 1*time.Second) // Pool with 2 connections and 1-second timeout
11+
12+
// Test borrowing the first connection
13+
conn1, err := pool.BorrowConnection()
14+
if err != nil {
15+
t.Errorf("Failed to borrow connection: %s", err)
16+
}
17+
18+
// Test borrowing the second connection
19+
conn2, err := pool.BorrowConnection()
20+
if err != nil {
21+
t.Errorf("Failed to borrow connection: %s", err)
22+
}
23+
24+
// Attempt to borrow a third connection, should timeout
25+
_, err = pool.BorrowConnection()
26+
if err == nil {
27+
t.Error("Expected error when borrowing third connection, but got none")
28+
}
29+
30+
// Release one connection and try again
31+
pool.ReleaseConnection(conn1)
32+
_, err = pool.BorrowConnection()
33+
if err != nil {
34+
t.Errorf("Failed to borrow connection after release: %s", err)
35+
}
36+
37+
// Cleanup: release all connections
38+
pool.ReleaseConnection(conn2)
39+
}
40+
41+
// TestConnectionResetOnRelease tests if connections are reset when released back to the pool.
42+
func TestConnectionResetOnRelease(t *testing.T) {
43+
pool := NewConnectionPool(1, 1*time.Second)
44+
conn, err := pool.BorrowConnection()
45+
if err != nil {
46+
t.Fatalf("Failed to borrow connection: %s", err)
47+
}
48+
49+
// Simulate using the connection
50+
conn.Execute("UPDATE SET something")
51+
52+
// Release and borrow again to test reset
53+
pool.ReleaseConnection(conn)
54+
conn2, err := pool.BorrowConnection()
55+
if err != nil {
56+
t.Fatalf("Failed to borrow connection again: %s", err)
57+
}
58+
59+
// Assume the Reset method clears specific flags or states
60+
if conn2.ID != conn.ID {
61+
t.Errorf("Connection ID mismatch, expected %d got %d", conn.ID, conn2.ID)
62+
}
63+
}

0 commit comments

Comments
 (0)