Skip to content

Commit 52735ea

Browse files
committed
perf: reuse Cassandra session across BDD scenario cleanups
gocql sessions are expensive to create (~500-1000ms each due to topology discovery and connection pool setup). Previously, each BDD scenario cleanup created and closed a new session, adding significant overhead across 1355 scenarios. Now we lazily create a single long-lived session and reuse it for all cleanup operations.
1 parent 631e77c commit 52735ea

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

tests/bdd/bdd_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ var (
5959
backend string
6060
composeFiles []string
6161
containerCmd string // "podman" or "docker"
62+
63+
// cassandraSession is a long-lived session reused across all BDD scenario cleanups.
64+
// gocql sessions are expensive to create (topology discovery, connection pool setup),
65+
// so we create one at first use and close it in TestMain.
66+
cassandraSession *gocql.Session
6267
)
6368

6469
func TestMain(m *testing.M) {
@@ -95,6 +100,12 @@ func TestMain(m *testing.M) {
95100

96101
code := m.Run()
97102

103+
// Close the long-lived Cassandra session before tearing down containers.
104+
if cassandraSession != nil {
105+
cassandraSession.Close()
106+
cassandraSession = nil
107+
}
108+
98109
if dockerMode {
99110
log.Println("Stopping compose...")
100111
composeDown(composeFiles)
@@ -284,8 +295,12 @@ func cleanMySQL() error {
284295
return nil
285296
}
286297

287-
// cleanCassandra truncates all tables in the schemaregistry keyspace.
288-
func cleanCassandra() error {
298+
// getCassandraSession returns a long-lived session for BDD cleanup.
299+
// The session is created once and reused across all scenarios.
300+
func getCassandraSession() (*gocql.Session, error) {
301+
if cassandraSession != nil {
302+
return cassandraSession, nil
303+
}
289304
portStr := envOrDefault("CASSANDRA_PORT", "19042")
290305
port, _ := strconv.Atoi(portStr)
291306
cluster := gocql.NewCluster("localhost")
@@ -296,9 +311,18 @@ func cleanCassandra() error {
296311

297312
session, err := cluster.CreateSession()
298313
if err != nil {
299-
return fmt.Errorf("connect cassandra: %w", err)
314+
return nil, fmt.Errorf("connect cassandra: %w", err)
315+
}
316+
cassandraSession = session
317+
return session, nil
318+
}
319+
320+
// cleanCassandra truncates all tables in the schemaregistry keyspace.
321+
func cleanCassandra() error {
322+
session, err := getCassandraSession()
323+
if err != nil {
324+
return err
300325
}
301-
defer session.Close()
302326

303327
tables := []string{
304328
"api_keys_by_hash", "api_keys_by_user", "api_keys_by_id",

0 commit comments

Comments
 (0)