Fix #4698: Add nil checks to ResetPools method (#4732)

* Add test for #4698: ResetPools should handle nil pools gracefully

* Fix #4698: Add nil checks to ResetPools method
This commit is contained in:
Nathan Wallace
2025-11-16 00:55:33 +08:00
committed by GitHub
parent 4f1367fe6c
commit b6aa7fc7d5
2 changed files with 27 additions and 2 deletions

View File

@@ -246,9 +246,13 @@ func (c *DbClient) ResetPools(ctx context.Context) {
log.Println("[TRACE] db_client.ResetPools start")
defer log.Println("[TRACE] db_client.ResetPools end")
if c.userPool != nil {
c.userPool.Reset()
}
if c.managementPool != nil {
c.managementPool.Reset()
}
}
func (c *DbClient) buildSchemasQuery(schemas ...string) string {
for idx, s := range schemas {

View File

@@ -288,6 +288,27 @@ func TestDbClient_ClosePools_NilPoolsHandling(t *testing.T) {
}, "closePools should handle nil pools gracefully")
}
// TestResetPools verifies that ResetPools handles nil pools gracefully without panicking.
// This test addresses bug #4698 where ResetPools panics when called on a DbClient with nil pools.
func TestResetPools(t *testing.T) {
// Create a DbClient with nil pools (simulating a partially initialized or closed client)
client := &DbClient{
userPool: nil,
managementPool: nil,
}
// ResetPools should NOT panic even with nil pools
// This is the expected correct behavior
defer func() {
if r := recover(); r != nil {
t.Errorf("ResetPools panicked with nil pools: %v", r)
}
}()
ctx := context.Background()
client.ResetPools(ctx)
}
// TestDbClient_SessionsMapInitialized verifies sessions map is initialized in NewDbClient
func TestDbClient_SessionsMapInitialized(t *testing.T) {
// Verify the initialization happens in NewDbClient