Skip to content

Commit ac1ba98

Browse files
authored
Export cassandra test setup definitions for reuse (#2581)
1 parent df18e3d commit ac1ba98

File tree

6 files changed

+115
-76
lines changed

6 files changed

+115
-76
lines changed

common/persistence/tests/cassandra_test.go

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,30 @@ import (
3131

3232
"github.com/stretchr/testify/suite"
3333

34-
"go.temporal.io/server/common/config"
3534
"go.temporal.io/server/common/log"
3635
"go.temporal.io/server/common/persistence/cassandra"
36+
"go.temporal.io/server/common/persistence/serialization"
3737
_ "go.temporal.io/server/common/persistence/sql/sqlplugin/mysql"
3838
"go.temporal.io/server/common/resolver"
39-
"go.temporal.io/server/common/shuffle"
40-
"go.temporal.io/server/environment"
4139
)
4240

43-
// TODO merge the initialization with existing persistence setup
44-
const (
45-
testCassandraClusterName = "temporal_cassandra_cluster"
41+
func setUpCassandraTest(t *testing.T) (CassandraTestData, func()) {
42+
var testData CassandraTestData
43+
testData.Cfg = NewCassandraConfig()
44+
testData.Logger = log.NewZapLogger(zaptest.NewLogger(t))
45+
SetUpCassandraDatabase(testData.Cfg, testData.Logger)
46+
SetUpCassandraSchema(testData.Cfg, testData.Logger)
4647

47-
testCassandraUser = "temporal"
48-
testCassandraPassword = "temporal"
49-
testCassandraDatabaseNamePrefix = "test_"
50-
testCassandraDatabaseNameSuffix = "temporal_persistence"
51-
)
52-
53-
type cassandraTestData struct {
54-
cfg *config.Cassandra
55-
factory *cassandra.Factory
56-
logger log.Logger
57-
}
58-
59-
func setUpCassandraTest(t *testing.T) (cassandraTestData, func()) {
60-
var testData cassandraTestData
61-
testData.cfg = newCassandraConfig()
62-
testData.logger = log.NewZapLogger(zaptest.NewLogger(t))
63-
SetUpCassandraDatabase(testData.cfg, testData.logger)
64-
SetUpCassandraSchema(testData.cfg, testData.logger)
65-
66-
testData.factory = cassandra.NewFactory(
67-
*testData.cfg,
48+
testData.Factory = cassandra.NewFactory(
49+
*testData.Cfg,
6850
resolver.NewNoopResolver(),
6951
testCassandraClusterName,
70-
testData.logger,
52+
testData.Logger,
7153
)
7254

7355
tearDown := func() {
74-
testData.factory.Close()
75-
TearDownCassandraKeyspace(testData.cfg)
56+
testData.Factory.Close()
57+
TearDownCassandraKeyspace(testData.Cfg)
7658
}
7759

7860
return testData, tearDown
@@ -82,82 +64,82 @@ func TestCassandraExecutionMutableStateStoreSuite(t *testing.T) {
8264
testData, tearDown := setUpCassandraTest(t)
8365
defer tearDown()
8466

85-
shardStore, err := testData.factory.NewShardStore()
67+
shardStore, err := testData.Factory.NewShardStore()
8668
if err != nil {
8769
t.Fatalf("unable to create Cassandra DB: %v", err)
8870
}
89-
executionStore, err := testData.factory.NewExecutionStore()
71+
executionStore, err := testData.Factory.NewExecutionStore()
9072
if err != nil {
9173
t.Fatalf("unable to create Cassandra DB: %v", err)
9274
}
9375

94-
s := NewExecutionMutableStateSuite(t, shardStore, executionStore, testData.logger)
76+
s := NewExecutionMutableStateSuite(
77+
t,
78+
shardStore,
79+
executionStore,
80+
serialization.NewSerializer(),
81+
testData.Logger)
9582
suite.Run(t, s)
9683
}
9784

9885
func TestCassandraExecutionMutableStateTaskStoreSuite(t *testing.T) {
9986
testData, tearDown := setUpCassandraTest(t)
10087
defer tearDown()
10188

102-
shardStore, err := testData.factory.NewShardStore()
89+
shardStore, err := testData.Factory.NewShardStore()
10390
if err != nil {
10491
t.Fatalf("unable to create Cassandra DB: %v", err)
10592
}
106-
executionStore, err := testData.factory.NewExecutionStore()
93+
executionStore, err := testData.Factory.NewExecutionStore()
10794
if err != nil {
10895
t.Fatalf("unable to create Cassandra DB: %v", err)
10996
}
11097

111-
s := NewExecutionMutableStateTaskSuite(t, shardStore, executionStore, testData.logger)
98+
s := NewExecutionMutableStateTaskSuite(
99+
t,
100+
shardStore,
101+
executionStore,
102+
serialization.NewSerializer(),
103+
testData.Logger,
104+
)
112105
suite.Run(t, s)
113106
}
114107

115108
func TestCassandraHistoryStoreSuite(t *testing.T) {
116109
testData, tearDown := setUpCassandraTest(t)
117110
defer tearDown()
118111

119-
store, err := testData.factory.NewExecutionStore()
112+
store, err := testData.Factory.NewExecutionStore()
120113
if err != nil {
121114
t.Fatalf("unable to create Cassandra DB: %v", err)
122115
}
123116

124-
s := NewHistoryEventsSuite(t, store, testData.logger)
117+
s := NewHistoryEventsSuite(t, store, testData.Logger)
125118
suite.Run(t, s)
126119
}
127120

128121
func TestCassandraTaskQueueSuite(t *testing.T) {
129122
testData, tearDown := setUpCassandraTest(t)
130123
defer tearDown()
131124

132-
taskQueueStore, err := testData.factory.NewTaskStore()
125+
taskQueueStore, err := testData.Factory.NewTaskStore()
133126
if err != nil {
134127
t.Fatalf("unable to create Cassandra DB: %v", err)
135128
}
136129

137-
s := NewTaskQueueSuite(t, taskQueueStore, testData.logger)
130+
s := NewTaskQueueSuite(t, taskQueueStore, testData.Logger)
138131
suite.Run(t, s)
139132
}
140133

141134
func TestCassandraTaskQueueTaskSuite(t *testing.T) {
142135
testData, tearDown := setUpCassandraTest(t)
143136
defer tearDown()
144137

145-
taskQueueStore, err := testData.factory.NewTaskStore()
138+
taskQueueStore, err := testData.Factory.NewTaskStore()
146139
if err != nil {
147140
t.Fatalf("unable to create Cassandra DB: %v", err)
148141
}
149142

150-
s := NewTaskQueueTaskSuite(t, taskQueueStore, testData.logger)
143+
s := NewTaskQueueTaskSuite(t, taskQueueStore, testData.Logger)
151144
suite.Run(t, s)
152145
}
153-
154-
// newCassandraConfig returns a new Cassandra config for test
155-
func newCassandraConfig() *config.Cassandra {
156-
return &config.Cassandra{
157-
User: testCassandraUser,
158-
Password: testCassandraPassword,
159-
Hosts: environment.GetCassandraAddress(),
160-
Port: environment.GetCassandraPort(),
161-
Keyspace: testCassandraDatabaseNamePrefix + shuffle.String(testCassandraDatabaseNameSuffix),
162-
}
163-
}

common/persistence/tests/cassandra_test_util.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
"go.temporal.io/server/common/persistence/cassandra"
4040
"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
4141
"go.temporal.io/server/common/resolver"
42+
"go.temporal.io/server/common/shuffle"
43+
"go.temporal.io/server/environment"
4244
)
4345

4446
const (
@@ -48,6 +50,22 @@ const (
4850
testCassandraVisibilitySchema = "../../../schema/cassandra/visibility/schema.cql"
4951
)
5052

53+
// TODO merge the initialization with existing persistence setup
54+
const (
55+
testCassandraClusterName = "temporal_cassandra_cluster"
56+
57+
testCassandraUser = "temporal"
58+
testCassandraPassword = "temporal"
59+
testCassandraDatabaseNamePrefix = "test_"
60+
testCassandraDatabaseNameSuffix = "temporal_persistence"
61+
)
62+
63+
type CassandraTestData struct {
64+
Cfg *config.Cassandra
65+
Factory *cassandra.Factory
66+
Logger log.Logger
67+
}
68+
5169
func SetUpCassandraDatabase(cfg *config.Cassandra, logger log.Logger) {
5270
adminCfg := *cfg
5371
// NOTE need to connect with empty name to create new database
@@ -176,3 +194,14 @@ func GetSchemaFiles(schemaDir string, logger log.Logger) []string {
176194

177195
return retVal
178196
}
197+
198+
// NewCassandraConfig returns a new Cassandra config for test
199+
func NewCassandraConfig() *config.Cassandra {
200+
return &config.Cassandra{
201+
User: testCassandraUser,
202+
Password: testCassandraPassword,
203+
Hosts: environment.GetCassandraAddress(),
204+
Port: environment.GetCassandraPort(),
205+
Keyspace: testCassandraDatabaseNamePrefix + shuffle.String(testCassandraDatabaseNameSuffix),
206+
}
207+
}

common/persistence/tests/execution_mutable_state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ func NewExecutionMutableStateSuite(
6767
t *testing.T,
6868
shardStore p.ShardStore,
6969
executionStore p.ExecutionStore,
70+
serializer serialization.Serializer,
7071
logger log.Logger,
7172
) *ExecutionMutableStateSuite {
7273
return &ExecutionMutableStateSuite{
7374
Assertions: require.New(t),
7475
ShardManager: p.NewShardManager(
7576
shardStore,
76-
serialization.NewSerializer(),
77+
serializer,
7778
),
7879
ExecutionManager: p.NewExecutionManager(
7980
executionStore,
80-
serialization.NewSerializer(),
81+
serializer,
8182
logger,
8283
dynamicconfig.GetIntPropertyFn(4*1024*1024),
8384
),

common/persistence/tests/execution_mutable_state_task.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ func NewExecutionMutableStateTaskSuite(
6262
t *testing.T,
6363
shardStore p.ShardStore,
6464
executionStore p.ExecutionStore,
65+
serializer serialization.Serializer,
6566
logger log.Logger,
6667
) *ExecutionMutableStateTaskSuite {
6768
return &ExecutionMutableStateTaskSuite{
6869
Assertions: require.New(t),
6970
ShardManager: p.NewShardManager(
7071
shardStore,
71-
serialization.NewSerializer(),
72+
serializer,
7273
),
7374
ExecutionManager: p.NewExecutionManager(
7475
executionStore,
75-
serialization.NewSerializer(),
76+
serializer,
7677
logger,
7778
dynamicconfig.GetIntPropertyFn(4*1024*1024),
7879
),
@@ -130,7 +131,7 @@ func (s *ExecutionMutableStateTaskSuite) TearDownTest() {
130131

131132
func (s *ExecutionMutableStateTaskSuite) TestAddGetTransferTasks_Multiple() {
132133
numTasks := 20
133-
transferTasks := s.addRandomTasks(
134+
transferTasks := s.AddRandomTasks(
134135
tasks.CategoryTransfer,
135136
numTasks,
136137
func(workflowKey definition.WorkflowKey, taskID int64, visibilityTimestamp time.Time) tasks.Task {
@@ -142,8 +143,8 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetTransferTasks_Multiple() {
142143
},
143144
)
144145

145-
transferTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.randomPaginateRange(transferTasks)
146-
loadedTasks := s.paginateTasks(
146+
transferTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.RandomPaginateRange(transferTasks)
147+
loadedTasks := s.PaginateTasks(
147148
tasks.CategoryTransfer,
148149
inclusiveMinTaskKey,
149150
exclusiveMaxTaskKey,
@@ -154,7 +155,7 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetTransferTasks_Multiple() {
154155

155156
func (s *ExecutionMutableStateTaskSuite) TestAddGetTimerTasks_Multiple() {
156157
numTasks := 20
157-
timerTasks := s.addRandomTasks(
158+
timerTasks := s.AddRandomTasks(
158159
tasks.CategoryTimer,
159160
numTasks,
160161
func(workflowKey definition.WorkflowKey, taskID int64, visibilityTimestamp time.Time) tasks.Task {
@@ -166,8 +167,8 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetTimerTasks_Multiple() {
166167
},
167168
)
168169

169-
timerTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.randomPaginateRange(timerTasks)
170-
loadedTasks := s.paginateTasks(
170+
timerTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.RandomPaginateRange(timerTasks)
171+
loadedTasks := s.PaginateTasks(
171172
tasks.CategoryTimer,
172173
inclusiveMinTaskKey,
173174
exclusiveMaxTaskKey,
@@ -178,7 +179,7 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetTimerTasks_Multiple() {
178179

179180
func (s *ExecutionMutableStateTaskSuite) TestAddGetReplicationTasks_Multiple() {
180181
numTasks := 20
181-
replicationTasks := s.addRandomTasks(
182+
replicationTasks := s.AddRandomTasks(
182183
tasks.CategoryReplication,
183184
numTasks,
184185
func(workflowKey definition.WorkflowKey, taskID int64, visibilityTimestamp time.Time) tasks.Task {
@@ -190,8 +191,8 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetReplicationTasks_Multiple() {
190191
},
191192
)
192193

193-
replicationTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.randomPaginateRange(replicationTasks)
194-
loadedTasks := s.paginateTasks(
194+
replicationTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.RandomPaginateRange(replicationTasks)
195+
loadedTasks := s.PaginateTasks(
195196
tasks.CategoryReplication,
196197
inclusiveMinTaskKey,
197198
exclusiveMaxTaskKey,
@@ -202,7 +203,7 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetReplicationTasks_Multiple() {
202203

203204
func (s *ExecutionMutableStateTaskSuite) TestAddGetVisibilityTasks_Multiple() {
204205
numTasks := 20
205-
visibilityTasks := s.addRandomTasks(
206+
visibilityTasks := s.AddRandomTasks(
206207
tasks.CategoryVisibility,
207208
numTasks,
208209
func(workflowKey definition.WorkflowKey, taskID int64, visibilityTimestamp time.Time) tasks.Task {
@@ -214,8 +215,8 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetVisibilityTasks_Multiple() {
214215
},
215216
)
216217

217-
visibilityTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.randomPaginateRange(visibilityTasks)
218-
loadedTasks := s.paginateTasks(
218+
visibilityTasks, inclusiveMinTaskKey, exclusiveMaxTaskKey := s.RandomPaginateRange(visibilityTasks)
219+
loadedTasks := s.PaginateTasks(
219220
tasks.CategoryVisibility,
220221
inclusiveMinTaskKey,
221222
exclusiveMaxTaskKey,
@@ -224,7 +225,7 @@ func (s *ExecutionMutableStateTaskSuite) TestAddGetVisibilityTasks_Multiple() {
224225
s.Equal(visibilityTasks, loadedTasks)
225226
}
226227

227-
func (s *ExecutionMutableStateTaskSuite) addRandomTasks(
228+
func (s *ExecutionMutableStateTaskSuite) AddRandomTasks(
228229
category tasks.Category,
229230
numTasks int,
230231
newTaskFn func(definition.WorkflowKey, int64, time.Time) tasks.Task,
@@ -253,7 +254,7 @@ func (s *ExecutionMutableStateTaskSuite) addRandomTasks(
253254
return randomTasks
254255
}
255256

256-
func (s *ExecutionMutableStateTaskSuite) paginateTasks(
257+
func (s *ExecutionMutableStateTaskSuite) PaginateTasks(
257258
category tasks.Category,
258259
inclusiveMinTaskKey tasks.Key,
259260
exclusiveMaxTaskKey tasks.Key,
@@ -280,7 +281,7 @@ func (s *ExecutionMutableStateTaskSuite) paginateTasks(
280281
return loadedTasks
281282
}
282283

283-
func (s *ExecutionMutableStateTaskSuite) randomPaginateRange(
284+
func (s *ExecutionMutableStateTaskSuite) RandomPaginateRange(
284285
createdTasks []tasks.Task,
285286
) ([]tasks.Task, tasks.Key, tasks.Key) {
286287
numTasks := len(createdTasks)

0 commit comments

Comments
 (0)