Skip to content

Commit 901e11c

Browse files
committed
test(pool): update test to ensure idle capacity is assert properly
1 parent 10f1d23 commit 901e11c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pool_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,14 @@ func TestIdlePool_CapacityLimit(t *testing.T) {
129129

130130
ctx := context.Background()
131131

132-
// Should get the newest connections (oldest ones were evicted)
133-
retrieved := pool.Get(ctx)
134-
require.NotNil(t, retrieved)
135-
// With capacity 3 and 5 connections added, conn1 and conn2 should be evicted
136-
// We should have conn3, conn4, conn5
137-
assert.True(t, retrieved.connID() >= 3, "should only contain newer connections")
132+
// Expect the pool to return connections [3, 4, 5] in order
133+
// connections (1) and (2) will have been evicted due to capacity
134+
for i := 0; i < 3; i++ {
135+
retrieved := pool.Get(ctx)
136+
require.NotNil(t, retrieved)
137+
138+
assert.True(t, retrieved.connID() == 3+i, "unexpected connection ID")
139+
}
138140
}
139141

140142
func TestIdlePool_ExpiredConnectionNotReturned(t *testing.T) {
@@ -185,7 +187,7 @@ func TestIdlePool_PutExpiredConnection(t *testing.T) {
185187
assert.Equal(t, 0, pool.Length())
186188
}
187189

188-
func TestIdlePool_PutOlderThanMinimum(t *testing.T) {
190+
func TestIdlePool_PutOlderThanMinimumWithCapacity(t *testing.T) {
189191
pool := newIdlePool(time.Hour, 5)
190192
defer pool.Close()
191193

@@ -195,17 +197,17 @@ func TestIdlePool_PutOlderThanMinimum(t *testing.T) {
195197
conn1 := &mockTransport{connectedAt: now, id: 1}
196198
pool.Put(conn1)
197199

198-
// Try to add an older connection
200+
// Try to add an older connection that current minimum
199201
olderConn := &mockTransport{connectedAt: now.Add(-time.Minute), id: 2}
200202
pool.Put(olderConn)
201203

202-
// Pool should reject the older connection
203-
assert.Equal(t, 1, pool.Length())
204+
// Pool should insert connection into min
205+
assert.Equal(t, 2, pool.Length())
204206

205207
ctx := context.Background()
206208
retrieved := pool.Get(ctx)
207209
require.NotNil(t, retrieved)
208-
assert.Equal(t, 1, retrieved.connID(), "should only have the newer connection")
210+
assert.Equal(t, 2, retrieved.connID(), "should retrieve the oldest connection")
209211
}
210212

211213
func TestIdlePool_GetWithCancelledContext(t *testing.T) {

0 commit comments

Comments
 (0)