-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration_test.go
442 lines (400 loc) · 16.4 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package spream_test
import (
"context"
"fmt"
"log"
"math/rand/v2"
"os"
"strconv"
"testing"
"time"
"cloud.google.com/go/spanner"
database "cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
instance "cloud.google.com/go/spanner/admin/instance/apiv1"
"cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/toga4/spream"
"github.com/toga4/spream/partitionstorage"
)
var (
projectID string = os.Getenv("GCP_PROJECT")
databaseName string
)
func TestMain(m *testing.M) {
tearDown := setup()
exitCode := m.Run()
tearDown()
os.Exit(exitCode)
}
func setup() func() {
// skip if env var GCP_PROJECT not set
if projectID == "" {
return func() {}
}
ctx := context.Background()
log.Print("Creating spanner instance...")
instanceName, err := createInstance(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create spanner instance: %v", err)
}
log.Printf("Created spanner instance: %q", instanceName)
log.Print("Creating spanner database...")
_databaseName, err := createDatabase(ctx, instanceName)
if err != nil {
log.Printf("Deleting spanner database: %q...", instanceName)
if err := deleteInstance(ctx, instanceName); err != nil {
log.Fatalf("Failed to delete spanner instance: %v", err)
}
log.Printf("Deleted spanner database: %q", instanceName)
log.Fatalf("Failed to create spanner database: %v", err)
}
databaseName = _databaseName
log.Printf("Created spanner database: %q", databaseName)
return func() {
log.Printf("Deleting spanner database: %q...", instanceName)
if err := deleteInstance(ctx, instanceName); err != nil {
log.Fatalf("Failed to delete spanner instance: %v", err)
}
log.Printf("Deleted spanner database: %q", instanceName)
}
}
func generateUniqueId(prefix string) string {
return fmt.Sprintf("%s-%s", prefix, strconv.FormatUint(rand.Uint64(), 36))
}
func generateUniqueName(prefix string) string {
return fmt.Sprintf("%s_%s", prefix, strconv.FormatUint(rand.Uint64(), 36))
}
func createInstance(ctx context.Context, parentProjectID string) (string, error) {
instanceAdminClient, err := instance.NewInstanceAdminClient(ctx)
if err != nil {
return "", err
}
defer instanceAdminClient.Close()
instanceID := generateUniqueId("instance")
op, err := instanceAdminClient.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: "projects/" + parentProjectID,
InstanceId: instanceID,
Instance: &instancepb.Instance{
Config: "projects/spream/instanceConfigs/regional-us-central1",
DisplayName: instanceID,
ProcessingUnits: 100,
},
})
if err != nil {
return "", err
}
resp, err := op.Wait(ctx)
if err != nil {
return "", err
}
return resp.Name, nil
}
func deleteInstance(ctx context.Context, instanceName string) error {
instanceAdminClient, err := instance.NewInstanceAdminClient(ctx)
if err != nil {
return err
}
defer instanceAdminClient.Close()
return instanceAdminClient.DeleteInstance(ctx, &instancepb.DeleteInstanceRequest{
Name: instanceName,
})
}
func createDatabase(ctx context.Context, parentInstanceName string) (string, error) {
databaseAdminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return "", err
}
defer databaseAdminClient.Close()
databaseID := generateUniqueId("database")
op, err := databaseAdminClient.CreateDatabase(ctx, &databasepb.CreateDatabaseRequest{
Parent: parentInstanceName,
CreateStatement: fmt.Sprintf("CREATE DATABASE `%s`", databaseID),
})
if err != nil {
return "", err
}
resp, err := op.Wait(ctx)
if err != nil {
return "", err
}
return resp.Name, nil
}
func createTableAndChangeStream(ctx context.Context, databaseName string) (string, string, error) {
databaseAdminClient, err := database.NewDatabaseAdminClient(ctx)
if err != nil {
return "", "", err
}
defer databaseAdminClient.Close()
tableName := generateUniqueName("table")
streamName := generateUniqueName("stream")
op, err := databaseAdminClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
Database: databaseName,
Statements: []string{
fmt.Sprintf(`CREATE TABLE %s (
Bool BOOL,
Int64 INT64,
Float64 FLOAT64,
Timestamp TIMESTAMP,
Date DATE,
String STRING(MAX),
Bytes BYTES(MAX),
Numeric NUMERIC,
Json JSON,
BoolArray ARRAY<BOOL>,
Int64Array ARRAY<INT64>,
Float64Array ARRAY<FLOAT64>,
TimestampArray ARRAY<TIMESTAMP>,
DateArray ARRAY<DATE>,
StringArray ARRAY<STRING(MAX)>,
BytesArray ARRAY<BYTES(MAX)>,
NumericArray ARRAY<NUMERIC>,
JsonArray ARRAY<JSON>,
) PRIMARY KEY (Int64)`, tableName),
fmt.Sprintf(`CREATE CHANGE STREAM %s FOR %s`, streamName, tableName),
},
})
if err != nil {
return "", "", err
}
if err := op.Wait(ctx); err != nil {
return "", "", err
}
return tableName, streamName, err
}
type consumer struct {
changes []*spream.DataChangeRecord
}
func (c *consumer) Consume(change *spream.DataChangeRecord) error {
c.changes = append(c.changes, change)
return nil
}
func TestSubscriber(t *testing.T) {
if projectID == "" {
t.Skip("Skip integration tests: env var GCP_PROJECT not set.")
}
ctx := context.Background()
spannerClient, err := spanner.NewClient(ctx, databaseName)
if err != nil {
t.Errorf("Failed to setup: %v", err)
return
}
t.Log("Creating table and change stream...")
tableName, streamName, err := createTableAndChangeStream(ctx, spannerClient.DatabaseName())
if err != nil {
t.Errorf("Failed to create table and change stream: %v", err)
return
}
t.Logf("Created table: %q, change stream: %q", tableName, streamName)
tests := []struct {
name string
statements []string
expected []*spream.DataChangeRecord
}{
{
name: "change",
statements: []string{
fmt.Sprintf(`
INSERT INTO %s
(Bool, Int64, Float64, Timestamp, Date, String, Bytes, Numeric, Json, BoolArray, Int64Array, Float64Array, TimestampArray, DateArray, StringArray, BytesArray, NumericArray, JsonArray)
VALUES (
TRUE,
1,
0.5,
'2023-12-31T23:59:59.999999999Z',
'2023-01-01',
'string',
B'bytes',
NUMERIC '123.456',
JSON '{"name":"foobar"}',
[TRUE, FALSE],
[1, 2],
[0.5, 0.25],
[TIMESTAMP '2023-12-31T23:59:59.999999999Z', TIMESTAMP '2023-01-01T00:00:00Z'],
[DATE '2023-01-01', DATE '2023-02-01'],
['string1', 'string2'],
[B'bytes1', B'bytes2'],
[NUMERIC '12.345', NUMERIC '67.89'],
[JSON '{"name":"foobar"}', JSON '{"name":"barbaz"}']
)
`, tableName),
fmt.Sprintf(`UPDATE %s SET Bool = FALSE WHERE Int64 = 1`, tableName),
fmt.Sprintf(`DELETE FROM %s WHERE Int64 = 1`, tableName),
},
expected: []*spream.DataChangeRecord{
{
RecordSequence: "00000000",
IsLastRecordInTransactionInPartition: false,
TableName: tableName,
ColumnTypes: []*spream.ColumnType{
{Name: "Int64", Type: spream.Type{Code: spream.TypeCode_INT64}, OrdinalPosition: 2, IsPrimaryKey: true},
{Name: "Bool", Type: spream.Type{Code: spream.TypeCode_BOOL}, OrdinalPosition: 1},
{Name: "Float64", Type: spream.Type{Code: spream.TypeCode_FLOAT64}, OrdinalPosition: 3},
{Name: "Timestamp", Type: spream.Type{Code: spream.TypeCode_TIMESTAMP}, OrdinalPosition: 4},
{Name: "Date", Type: spream.Type{Code: spream.TypeCode_DATE}, OrdinalPosition: 5},
{Name: "String", Type: spream.Type{Code: spream.TypeCode_STRING}, OrdinalPosition: 6},
{Name: "Bytes", Type: spream.Type{Code: spream.TypeCode_BYTES}, OrdinalPosition: 7},
{Name: "Numeric", Type: spream.Type{Code: spream.TypeCode_NUMERIC}, OrdinalPosition: 8},
{Name: "Json", Type: spream.Type{Code: spream.TypeCode_JSON}, OrdinalPosition: 9},
{Name: "BoolArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_BOOL}, OrdinalPosition: 10},
{Name: "Int64Array", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_INT64}, OrdinalPosition: 11},
{Name: "Float64Array", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_FLOAT64}, OrdinalPosition: 12},
{Name: "TimestampArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_TIMESTAMP}, OrdinalPosition: 13},
{Name: "DateArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_DATE}, OrdinalPosition: 14},
{Name: "StringArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_STRING}, OrdinalPosition: 15},
{Name: "BytesArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_BYTES}, OrdinalPosition: 16},
{Name: "NumericArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_NUMERIC}, OrdinalPosition: 17},
{Name: "JsonArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_JSON}, OrdinalPosition: 18},
},
Mods: []*spream.Mod{
{
Keys: map[string]interface{}{"Int64": "1"},
NewValues: map[string]interface{}{
"Bool": true,
"BoolArray": []interface{}{true, false},
"Bytes": "Ynl0ZXM=",
"BytesArray": []interface{}{"Ynl0ZXMx", "Ynl0ZXMy"},
"Date": "2023-01-01",
"DateArray": []interface{}{"2023-01-01", "2023-02-01"},
"Float64": 0.5,
"Float64Array": []interface{}{0.5, 0.25},
"Int64Array": []interface{}{"1", "2"},
"Json": "{\"name\":\"foobar\"}",
"JsonArray": []interface{}{"{\"name\":\"foobar\"}", "{\"name\":\"barbaz\"}"},
"Numeric": "123.456",
"NumericArray": []interface{}{"12.345", "67.89"},
"String": "string",
"StringArray": []interface{}{"string1", "string2"},
"Timestamp": "2023-12-31T23:59:59.999999999Z",
"TimestampArray": []interface{}{"2023-12-31T23:59:59.999999999Z", "2023-01-01T00:00:00Z"},
},
OldValues: map[string]interface{}{},
},
},
ModType: spream.ModType_INSERT,
ValueCaptureType: "OLD_AND_NEW_VALUES",
NumberOfRecordsInTransaction: 3,
NumberOfPartitionsInTransaction: 1,
TransactionTag: "",
IsSystemTransaction: false,
},
{
RecordSequence: "00000001",
IsLastRecordInTransactionInPartition: false,
TableName: tableName,
ColumnTypes: []*spream.ColumnType{
{Name: "Int64", Type: spream.Type{Code: spream.TypeCode_INT64}, OrdinalPosition: 2, IsPrimaryKey: true},
{Name: "Bool", Type: spream.Type{Code: spream.TypeCode_BOOL}, OrdinalPosition: 1},
},
Mods: []*spream.Mod{
{
Keys: map[string]interface{}{"Int64": "1"},
NewValues: map[string]interface{}{"Bool": false},
OldValues: map[string]interface{}{"Bool": true},
},
},
ModType: spream.ModType_UPDATE,
ValueCaptureType: "OLD_AND_NEW_VALUES",
NumberOfRecordsInTransaction: 3,
NumberOfPartitionsInTransaction: 1,
TransactionTag: "",
IsSystemTransaction: false,
},
{
RecordSequence: "00000002",
IsLastRecordInTransactionInPartition: true,
TableName: tableName,
ColumnTypes: []*spream.ColumnType{
{Name: "Int64", Type: spream.Type{Code: spream.TypeCode_INT64}, OrdinalPosition: 2, IsPrimaryKey: true},
{Name: "Bool", Type: spream.Type{Code: spream.TypeCode_BOOL}, OrdinalPosition: 1},
{Name: "Float64", Type: spream.Type{Code: spream.TypeCode_FLOAT64}, OrdinalPosition: 3},
{Name: "Timestamp", Type: spream.Type{Code: spream.TypeCode_TIMESTAMP}, OrdinalPosition: 4},
{Name: "Date", Type: spream.Type{Code: spream.TypeCode_DATE}, OrdinalPosition: 5},
{Name: "String", Type: spream.Type{Code: spream.TypeCode_STRING}, OrdinalPosition: 6},
{Name: "Bytes", Type: spream.Type{Code: spream.TypeCode_BYTES}, OrdinalPosition: 7},
{Name: "Numeric", Type: spream.Type{Code: spream.TypeCode_NUMERIC}, OrdinalPosition: 8},
{Name: "Json", Type: spream.Type{Code: spream.TypeCode_JSON}, OrdinalPosition: 9},
{Name: "BoolArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_BOOL}, OrdinalPosition: 10},
{Name: "Int64Array", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_INT64}, OrdinalPosition: 11},
{Name: "Float64Array", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_FLOAT64}, OrdinalPosition: 12},
{Name: "TimestampArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_TIMESTAMP}, OrdinalPosition: 13},
{Name: "DateArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_DATE}, OrdinalPosition: 14},
{Name: "StringArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_STRING}, OrdinalPosition: 15},
{Name: "BytesArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_BYTES}, OrdinalPosition: 16},
{Name: "NumericArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_NUMERIC}, OrdinalPosition: 17},
{Name: "JsonArray", Type: spream.Type{Code: spream.TypeCode_ARRAY, ArrayElementType: spream.TypeCode_JSON}, OrdinalPosition: 18},
},
Mods: []*spream.Mod{
{
Keys: map[string]interface{}{"Int64": "1"},
NewValues: map[string]interface{}{},
OldValues: map[string]interface{}{
"Bool": false,
"BoolArray": []interface{}{true, false},
"Bytes": "Ynl0ZXM=",
"BytesArray": []interface{}{"Ynl0ZXMx", "Ynl0ZXMy"},
"Date": "2023-01-01",
"DateArray": []interface{}{"2023-01-01", "2023-02-01"},
"Float64": 0.5,
"Float64Array": []interface{}{0.5, 0.25},
"Int64Array": []interface{}{"1", "2"},
"Json": "{\"name\":\"foobar\"}",
"JsonArray": []interface{}{"{\"name\":\"foobar\"}", "{\"name\":\"barbaz\"}"},
"Numeric": "123.456",
"NumericArray": []interface{}{"12.345", "67.89"},
"String": "string",
"StringArray": []interface{}{"string1", "string2"},
"Timestamp": "2023-12-31T23:59:59.999999999Z",
"TimestampArray": []interface{}{"2023-12-31T23:59:59.999999999Z", "2023-01-01T00:00:00Z"},
},
},
},
ModType: spream.ModType_DELETE,
ValueCaptureType: "OLD_AND_NEW_VALUES",
NumberOfRecordsInTransaction: 3,
NumberOfPartitionsInTransaction: 1,
TransactionTag: "",
IsSystemTransaction: false,
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
partitionStorage := partitionstorage.NewSpanner(spannerClient, generateUniqueName("partition"))
t.Log("Creating metadata table...")
if err := partitionStorage.CreateTableIfNotExists(ctx); err != nil {
t.Errorf("Failed to create metadata table: %v", err)
return
}
subscriber := spream.NewSubscriber(spannerClient, streamName, partitionStorage)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
consumer := &consumer{}
go func() {
_ = subscriber.Subscribe(ctx, consumer)
}()
t.Log("Subscribe started.")
t.Log("Executing DML statements...")
if _, err := spannerClient.ReadWriteTransaction(ctx, func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
for _, stmt := range test.statements {
if _, err := tx.Update(ctx, spanner.NewStatement(stmt)); err != nil {
return err
}
}
return nil
}); err != nil {
t.Errorf("Failed to execute change statements: %v", err)
return
}
t.Log("Waiting subscription...")
time.Sleep(5 * time.Second)
cancel()
opt := cmpopts.IgnoreFields(spream.DataChangeRecord{}, "CommitTimestamp", "ServerTransactionID")
if diff := cmp.Diff(test.expected, consumer.changes, opt); diff != "" {
t.Errorf("diff = %v", diff)
}
})
}
}