Skip to content

Commit

Permalink
add test for delay
Browse files Browse the repository at this point in the history
  • Loading branch information
korotkov-aerospike committed Nov 3, 2024
1 parent f2fea8f commit 158fad9
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions pkg/service/client_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (f *MockClientFactory) NewClientWithPolicyAndHost(_ *as.ClientPolicy, _ ...
return m, nil
}

func assertClientExists(t *testing.T, clientManager *ClientManagerImpl, cl *model.AerospikeCluster, shouldExist bool) {
t.Helper()
clientManager.mu.Lock()
defer clientManager.mu.Unlock()
_, exists := clientManager.clients[cl]
assert.Equal(t, shouldExist, exists)
}

func Test_GetClient(t *testing.T) {
clientManager := NewClientManager(
&MockClientFactory{},
Expand Down Expand Up @@ -79,27 +87,24 @@ func Test_CreateClient_Errors(t *testing.T) {
func Test_Close(t *testing.T) {
clientManager := NewClientManager(
&MockClientFactory{},
0*time.Second, // Immediate close for testing
100*time.Millisecond,
)

client, err := clientManager.GetClient(cluster)
assert.NoError(t, err)
assert.NotNil(t, client)
assertClientExists(t, clientManager, cluster, true)

clientManager.Close(client)
time.Sleep(150 * time.Millisecond) // Wait for timer to fire

// Wait a bit for the timer to fire
time.Sleep(10 * time.Millisecond)

// Verify that client is removed from clients map
_, exists := clientManager.clients[cluster]
assert.False(t, exists)
assertClientExists(t, clientManager, cluster, false)
}

func Test_Close_Multiple(t *testing.T) {
clientManager := NewClientManager(
&MockClientFactory{},
0*time.Second, // Immediate close for testing
100*time.Millisecond,
)

client, err := clientManager.GetClient(cluster)
Expand All @@ -110,17 +115,11 @@ func Test_Close_Multiple(t *testing.T) {
assert.NotNil(t, client)

clientManager.Close(client)

_, exists := clientManager.clients[cluster]
assert.True(t, exists)
assertClientExists(t, clientManager, cluster, true)

clientManager.Close(client)

// Wait a bit for the timer to fire
time.Sleep(10 * time.Millisecond)

_, exists = clientManager.clients[cluster]
assert.False(t, exists)
time.Sleep(150 * time.Millisecond) // Wait for timer to fire
assertClientExists(t, clientManager, cluster, false)
}

func Test_Close_CancelOnReuse(t *testing.T) {
Expand All @@ -143,11 +142,8 @@ func Test_Close_CancelOnReuse(t *testing.T) {
assert.Equal(t, client, client2)

// Wait longer than the close delay
time.Sleep(100 * time.Millisecond)

// Client should still exist because close was cancelled
_, exists := clientManager.clients[cluster]
assert.True(t, exists)
time.Sleep(150 * time.Millisecond)
assertClientExists(t, clientManager, cluster, true)
}

func Test_Close_NotExisting(t *testing.T) {
Expand Down

0 comments on commit 158fad9

Please sign in to comment.