Files
steampipe/pkg/db/db_client/db_client_test.go

51 lines
1.9 KiB
Go

package db_client
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestSessionMapCleanupImplemented verifies that the session map memory leak is fixed
// Reference: https://github.com/turbot/steampipe/issues/3737
//
// This test verifies that a BeforeClose callback is registered to clean up
// session map entries when connections are dropped by pgx.
//
// Without this fix, sessions accumulate indefinitely causing a memory leak.
func TestSessionMapCleanupImplemented(t *testing.T) {
// Read the db_client_connect.go file to verify BeforeClose callback exists
content, err := os.ReadFile("db_client_connect.go")
require.NoError(t, err, "should be able to read db_client_connect.go")
sourceCode := string(content)
// Verify BeforeClose callback is registered
assert.Contains(t, sourceCode, "config.BeforeClose",
"BeforeClose callback must be registered to clean up sessions when connections close")
// Verify the callback deletes from sessions map
assert.Contains(t, sourceCode, "delete(c.sessions, backendPid)",
"BeforeClose callback must delete session entries to prevent memory leak")
// Verify the comment in db_client.go documents automatic cleanup
clientContent, err := os.ReadFile("db_client.go")
require.NoError(t, err, "should be able to read db_client.go")
clientCode := string(clientContent)
// The comment should document automatic cleanup, not a TODO
assert.NotContains(t, clientCode, "TODO: there's no code which cleans up this map",
"TODO comment should be removed after implementing the fix")
// Should document the automatic cleanup mechanism
hasCleanupComment := strings.Contains(clientCode, "automatically cleaned up") ||
strings.Contains(clientCode, "automatic cleanup") ||
strings.Contains(clientCode, "BeforeClose")
assert.True(t, hasCleanupComment,
"Comment should document automatic cleanup mechanism")
}