-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool_test.go
120 lines (97 loc) · 2.88 KB
/
pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package universum
import (
"context"
"errors"
"testing"
"time"
)
// MockOptions creates mock options for testing purposes
func mockOptions() *Options {
return &Options{
ConnPoolsize: 10,
ConnWaitTimeout: 10 * time.Second,
ConnMaxLifetime: 1 * time.Hour,
HostAddr: "localhost:11191",
DialTimeout: 1 * time.Second,
MaxRetries: 5,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
// TestNewConnPool creates a new connection pool and verifies its initialization
func TestNewConnPool(t *testing.T) {
opts := mockOptions()
pool, err := newConnPool(opts)
if err != nil {
t.Fatalf("Expected to create conn pool, got error: %v", err)
}
if pool == nil {
t.Fatalf("Expected non-nil pool")
}
if len(pool.connections) != int(pool.poolsize) {
t.Errorf("Expected pool size %d, got %d", pool.poolsize, len(pool.connections))
}
}
// TestGetConn acquires a connection from the pool
func TestGetConn(t *testing.T) {
opts := mockOptions()
connPool, _ := newConnPool(opts)
ctx := context.Background()
conn, err := connPool.GetConn(ctx)
if err != nil {
t.Fatalf("Expected to acquire connection, got error: %v", err)
}
if conn == nil {
t.Fatalf("Expected non-nil connection, got nil")
}
}
// TestReleaseConn verifies releasing a connection back to the pool
func TestReleaseConn(t *testing.T) {
opts := mockOptions()
pool, _ := newConnPool(opts)
ctx := context.Background()
conn, err := pool.GetConn(ctx)
if err != nil {
t.Fatalf("Failed to acquire connection: %v", err)
}
// Release connection back to the pool
pool.ReleaseConn(ctx, conn)
if pool.IdleLen() != 1 {
t.Errorf("Expected idle connections to be 1, got %d", pool.IdleLen())
}
}
// TestCloseConnPool tests the closing of the pool
func TestCloseConnPool(t *testing.T) {
opts := mockOptions()
pool, _ := newConnPool(opts)
err := pool.Close()
if err != nil {
t.Fatalf("Expected to close connection pool without error, got: %v", err)
}
if !pool.closed() {
t.Fatal("Expected pool to be closed")
}
}
// TestWaitForTurnTimeout verifies that waitForTurn times out correctly
func TestWaitForTurnTimeout(t *testing.T) {
opts := mockOptions()
opts.ConnWaitTimeout = 500 * time.Millisecond
pool, _ := newConnPool(opts)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
time.Sleep(300 * time.Millisecond)
err := pool.waitForTurn(ctx)
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expected context deadline exceeded error, got %v", err)
}
}
// TestIsActiveConnection verifies if the connection is active
func TestIsActiveConnection(t *testing.T) {
opts := mockOptions()
pool, _ := newConnPool(opts)
conn, _ := pool.GetConn(context.Background())
conn.setCreatedAt(time.Now().Add(-15 * time.Minute))
if !pool.isActiveConnection(conn) {
t.Fatal("Expected connection to be inactive, but it is active")
}
}