diff --git a/plugin/storage/cassandra/factory.go b/plugin/storage/cassandra/factory.go index dfa12fcb974..a761207e219 100644 --- a/plugin/storage/cassandra/factory.go +++ b/plugin/storage/cassandra/factory.go @@ -232,11 +232,19 @@ var _ io.Closer = (*Factory)(nil) // Close closes the resources held by the factory func (f *Factory) Close() error { - f.Options.Get(archiveStorageConfig) + if f.primarySession != nil { + f.primarySession.Close() + } + if f.archiveSession != nil { + f.archiveSession.Close() + } + + var errs []error if cfg := f.Options.Get(archiveStorageConfig); cfg != nil { - cfg.TLS.Close() + errs = append(errs, cfg.TLS.Close()) } - return f.Options.GetPrimary().TLS.Close() + errs = append(errs, f.Options.GetPrimary().TLS.Close()) + return errors.Join(errs...) } // PrimarySession is used from integration tests to clean database between tests diff --git a/plugin/storage/cassandra/factory_test.go b/plugin/storage/cassandra/factory_test.go index 97f1bf20acd..5f9fbb30757 100644 --- a/plugin/storage/cassandra/factory_test.go +++ b/plugin/storage/cassandra/factory_test.go @@ -65,6 +65,7 @@ func TestCassandraFactory(t *testing.T) { query = &mocks.Query{} ) session.On("Query", mock.AnythingOfType("string"), mock.Anything).Return(query) + session.On("Close").Return() query.On("Exec").Return(nil) f.primaryConfig = newMockSessionBuilder(session, nil) f.archiveConfig = newMockSessionBuilder(nil, errors.New("made-up error")) diff --git a/plugin/storage/integration/cassandra_test.go b/plugin/storage/integration/cassandra_test.go index 0ba286a87e9..56ca2655c53 100644 --- a/plugin/storage/integration/cassandra_test.go +++ b/plugin/storage/integration/cassandra_test.go @@ -92,6 +92,9 @@ func (s *CassandraStorageIntegration) initializeCassandra(t *testing.T) { s.SamplingStore, err = f.CreateSamplingStore(0) require.NoError(t, err) s.initializeDependencyReaderAndWriter(t, f) + t.Cleanup(func() { + require.NoError(t, f.Close()) + }) } func (s *CassandraStorageIntegration) initializeDependencyReaderAndWriter(t *testing.T, f *cassandra.Factory) {