Skip to content

Commit

Permalink
Merge pull request #398 from sylwiaszunejko/fix-lwt-tests
Browse files Browse the repository at this point in the history
Call createKeyspace separately in both versions of createSessionFromCluster
  • Loading branch information
dkropachev authored Feb 21, 2025
2 parents a5bb9e2 + a74f982 commit b631024
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ func TestCAS(t *testing.T) {
t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
}

if err := createTable(session, `CREATE TABLE gocql_test.cas_table (
if err := createTable(session, `CREATE TABLE cas_table (
title varchar,
revid timeuuid,
last_modified timestamp,
Expand Down Expand Up @@ -700,7 +700,7 @@ func TestMapScanCAS(t *testing.T) {
t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
}

if err := createTable(session, `CREATE TABLE gocql_test.cas_table2 (
if err := createTable(session, `CREATE TABLE cas_table2 (
title varchar,
revid timeuuid,
last_modified timestamp,
Expand Down Expand Up @@ -1271,7 +1271,7 @@ func TestScanCASWithNilArguments(t *testing.T) {
t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
}

if err := createTable(session, `CREATE TABLE gocql_test.scan_cas_with_nil_arguments (
if err := createTable(session, `CREATE TABLE scan_cas_with_nil_arguments (
foo varchar,
bar varchar,
PRIMARY KEY (foo, bar)
Expand Down
49 changes: 41 additions & 8 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,29 @@ func addSslOptions(cluster *ClusterConfig) *ClusterConfig {
return cluster
}

var initOnce sync.Once
type OnceManager struct {
mu sync.Mutex
keyspaces map[string]*sync.Once
}

func NewOnceManager() *OnceManager {
return &OnceManager{
keyspaces: make(map[string]*sync.Once),
}
}

func (o *OnceManager) GetOnce(key string) *sync.Once {
o.mu.Lock()
defer o.mu.Unlock()

if once, exists := o.keyspaces[key]; exists {
return once
}
o.keyspaces[key] = &sync.Once{}
return o.keyspaces[key]
}

var initKeyspaceOnce = NewOnceManager()

func createTable(s *Session, table string) error {
// lets just be really sure
Expand Down Expand Up @@ -193,14 +215,25 @@ func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string, disa
}
}

func createSessionFromClusterHelper(cluster *ClusterConfig, tb testing.TB, tabletsDisabled bool) *Session {
type testKeyspaceOpts struct {
tabletsDisabled bool
}

func (o *testKeyspaceOpts) KeyspaceName() string {
if o.tabletsDisabled {
return "gocql_test_tablets_disabled"
}
return "gocql_test"
}

func createSessionFromClusterHelper(cluster *ClusterConfig, tb testing.TB, opts testKeyspaceOpts) *Session {
// Drop and re-create the keyspace once. Different tests should use their own
// individual tables, but can assume that the table does not exist before.
initOnce.Do(func() {
createKeyspace(tb, cluster, "gocql_test", tabletsDisabled)
initKeyspaceOnce.GetOnce(opts.KeyspaceName()).Do(func() {
createKeyspace(tb, cluster, opts.KeyspaceName(), opts.tabletsDisabled)
})

cluster.Keyspace = "gocql_test"
cluster.Keyspace = opts.KeyspaceName()
session, err := cluster.CreateSession()
if err != nil {
tb.Fatal("createSession:", err)
Expand All @@ -214,11 +247,11 @@ func createSessionFromClusterHelper(cluster *ClusterConfig, tb testing.TB, table
}

func createSessionFromClusterTabletsDisabled(cluster *ClusterConfig, tb testing.TB) *Session {
return createSessionFromClusterHelper(cluster, tb, true)
return createSessionFromClusterHelper(cluster, tb, testKeyspaceOpts{tabletsDisabled: true})
}

func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
return createSessionFromClusterHelper(cluster, tb, false)
return createSessionFromClusterHelper(cluster, tb, testKeyspaceOpts{tabletsDisabled: false})
}

func createSessionFromMultiNodeCluster(cluster *ClusterConfig, tb testing.TB) *Session {
Expand All @@ -229,7 +262,7 @@ func createSessionFromMultiNodeCluster(cluster *ClusterConfig, tb testing.TB) *S
tb.Fatal("createSession:", err)
}

initOnce.Do(func() {
initKeyspaceOnce.GetOnce(keyspace).Do(func() {
if err = createTable(session, `DROP KEYSPACE IF EXISTS `+keyspace); err != nil {
panic(fmt.Sprintf("unable to drop keyspace: %v", err))
}
Expand Down

0 comments on commit b631024

Please sign in to comment.