-
Notifications
You must be signed in to change notification settings - Fork 645
Expand file tree
/
Copy pathschema_events_test.go
More file actions
465 lines (358 loc) · 19.1 KB
/
schema_events_test.go
File metadata and controls
465 lines (358 loc) · 19.1 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//go:build cassandra
// +build cassandra
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gocql
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type schemaChangesTestListener struct {
KeyspaceCreatedEvents []OnKeyspaceCreatedEvent
KeyspaceUpdatedEvents []OnKeyspaceUpdatedEvent
KeyspaceDroppedEvents []OnKeyspaceDroppedEvent
TableCreatedEvents []OnTableCreatedEvent
TableUpdatedEvents []OnTableUpdatedEvent
TableDroppedEvents []OnTableDroppedEvent
FunctionCreatedEvents []OnFunctionCreatedEvent
FunctionUpdatedEvents []OnFunctionUpdatedEvent
FunctionDroppedEvents []OnFunctionDroppedEvent
AggregateCreatedEvents []OnAggregateCreatedEvent
AggregateUpdatedEvents []OnAggregateUpdatedEvent
AggregateDroppedEvents []OnAggregateDroppedEvent
TypeCreatedEvents []OnUserTypeCreatedEvent
TypeUpdatedEvents []OnUserTypeUpdatedEvent
TypeDroppedEvents []OnUserTypeDroppedEvent
}
// OnAggregateCreated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnAggregateCreated(event OnAggregateCreatedEvent) {
(*s).AggregateCreatedEvents = append(s.AggregateCreatedEvents, event)
}
// OnAggregateDropped implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnAggregateDropped(event OnAggregateDroppedEvent) {
(*s).AggregateDroppedEvents = append(s.AggregateDroppedEvents, event)
}
// OnAggregateUpdated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnAggregateUpdated(event OnAggregateUpdatedEvent) {
(*s).AggregateUpdatedEvents = append(s.AggregateUpdatedEvents, event)
}
// OnFunctionCreated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnFunctionCreated(event OnFunctionCreatedEvent) {
(*s).FunctionCreatedEvents = append(s.FunctionCreatedEvents, event)
}
// OnFunctionDropped implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnFunctionDropped(event OnFunctionDroppedEvent) {
(*s).FunctionDroppedEvents = append(s.FunctionDroppedEvents, event)
}
// OnFunctionUpdated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnFunctionUpdated(event OnFunctionUpdatedEvent) {
(*s).FunctionUpdatedEvents = append(s.FunctionUpdatedEvents, event)
}
// OnKeyspaceCreated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnKeyspaceCreated(event OnKeyspaceCreatedEvent) {
(*s).KeyspaceCreatedEvents = append(s.KeyspaceCreatedEvents, event)
}
// OnKeyspaceDropped implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnKeyspaceDropped(event OnKeyspaceDroppedEvent) {
(*s).KeyspaceDroppedEvents = append(s.KeyspaceDroppedEvents, event)
}
// OnKeyspaceUpdated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnKeyspaceUpdated(event OnKeyspaceUpdatedEvent) {
(*s).KeyspaceUpdatedEvents = append(s.KeyspaceUpdatedEvents, event)
}
// OnTableCreated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnTableCreated(event OnTableCreatedEvent) {
(*s).TableCreatedEvents = append(s.TableCreatedEvents, event)
}
// OnTableDropped implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnTableDropped(event OnTableDroppedEvent) {
(*s).TableDroppedEvents = append(s.TableDroppedEvents, event)
}
// OnTableUpdated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnTableUpdated(event OnTableUpdatedEvent) {
(*s).TableUpdatedEvents = append(s.TableUpdatedEvents, event)
}
// OnTypeCreated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnUserTypeCreated(event OnUserTypeCreatedEvent) {
(*s).TypeCreatedEvents = append(s.TypeCreatedEvents, event)
}
// OnTypeDropped implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnUserTypeDropped(event OnUserTypeDroppedEvent) {
(*s).TypeDroppedEvents = append(s.TypeDroppedEvents, event)
}
// OnTypeUpdated implements [SchemaChangeListener].
func (s *schemaChangesTestListener) OnUserTypeUpdated(event OnUserTypeUpdatedEvent) {
(*s).TypeUpdatedEvents = append(s.TypeUpdatedEvents, event)
}
func (s *schemaChangesTestListener) clear() {
s.KeyspaceCreatedEvents = nil
s.KeyspaceDroppedEvents = nil
s.KeyspaceUpdatedEvents = nil
s.TableCreatedEvents = nil
s.TableUpdatedEvents = nil
s.TableDroppedEvents = nil
s.FunctionCreatedEvents = nil
s.FunctionUpdatedEvents = nil
s.FunctionDroppedEvents = nil
s.AggregateCreatedEvents = nil
s.AggregateUpdatedEvents = nil
s.AggregateDroppedEvents = nil
s.TypeCreatedEvents = nil
s.TypeUpdatedEvents = nil
s.TypeDroppedEvents = nil
}
func TestSchemaEvents(t *testing.T) {
listener := &schemaChangesTestListener{}
session := createSession(t, func(config *ClusterConfig) {
config.Metadata.SchemaListener = SchemaListenersConfig{
KeyspaceChangeListener: listener,
TableChangeListener: listener,
UserTypeChangeListener: listener,
FunctionChangeListener: listener,
AggregateChangeListener: listener,
}
config.Events.DisableSchemaEvents = false
})
defer session.Close()
// Verify that listeners are not called during session initialization.
// During init, existing keyspaces/tables are detected as "new" in the metadata diff,
// but the listener must not be notified because the session is not yet initialized.
t.Run("no-events-during-initialization", func(t *testing.T) {
require.Empty(t, listener.KeyspaceCreatedEvents)
require.Empty(t, listener.KeyspaceUpdatedEvents)
require.Empty(t, listener.KeyspaceDroppedEvents)
require.Empty(t, listener.TableCreatedEvents)
require.Empty(t, listener.TableUpdatedEvents)
require.Empty(t, listener.TableDroppedEvents)
require.Empty(t, listener.FunctionCreatedEvents)
require.Empty(t, listener.FunctionUpdatedEvents)
require.Empty(t, listener.FunctionDroppedEvents)
require.Empty(t, listener.AggregateCreatedEvents)
require.Empty(t, listener.AggregateUpdatedEvents)
require.Empty(t, listener.AggregateDroppedEvents)
require.Empty(t, listener.TypeCreatedEvents)
require.Empty(t, listener.TypeUpdatedEvents)
require.Empty(t, listener.TypeDroppedEvents)
})
t.Run("keyspace", func(t *testing.T) {
testSchemaEventsKeyspace(t, session, listener)
})
t.Run("table", func(t *testing.T) {
testSchemaEventsTable(t, session, listener)
})
t.Run("user-defined-function", func(t *testing.T) {
testSchemaEventsFunction(t, session, listener)
})
t.Run("user-defined-aggregate", func(t *testing.T) {
testSchemaEventsAggregate(t, session, listener)
})
t.Run("user-defined-type", func(t *testing.T) {
testSchemaEventsType(t, session, listener)
})
}
func testSchemaEventsKeyspace(t *testing.T, session *Session, listener *schemaChangesTestListener) {
ks := randomNameWithPrefix("gocql_keyspace_events_")
listener.clear()
err := session.Query(fmt.Sprintf(`CREATE KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}`, ks)).Exec()
require.NoError(t, err, "Expected no error creating keyspace")
require.Eventually(t, func() bool {
return len(listener.KeyspaceCreatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected keyspace created event to be received")
// Verify that the listener received the keyspace created event
require.Equal(t, ks, listener.KeyspaceCreatedEvents[0].Keyspace.Name, "Expected keyspace created event to have correct keyspace name")
require.Contains(t, listener.KeyspaceCreatedEvents[0].Keyspace.StrategyClass, "SimpleStrategy", "Expected keyspace created event to have correct replication class")
require.Equal(t, "1", listener.KeyspaceCreatedEvents[0].Keyspace.StrategyOptions["replication_factor"], "Expected keyspace created event to have correct replication factor")
listener.clear()
err = session.Query(fmt.Sprintf(`ALTER KEYSPACE %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '2'}`, ks)).Exec()
require.NoError(t, err, "Expected no error updating keyspace")
// Verify that the listener received the keyspace updated event
// and that the old and new metadata are correct
require.Eventually(t, func() bool {
return len(listener.KeyspaceUpdatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected keyspace updated event to be received")
// Old metadata
require.Equal(t, ks, listener.KeyspaceUpdatedEvents[0].Old.Name, "Expected keyspace updated event to have correct keyspace name")
require.Contains(t, listener.KeyspaceUpdatedEvents[0].Old.StrategyClass, "SimpleStrategy", "Expected keyspace created event to have correct replication class")
require.Equal(t, "1", listener.KeyspaceUpdatedEvents[0].Old.StrategyOptions["replication_factor"], "Expected keyspace updated event to have correct old replication factor")
// New metadata
require.Equal(t, ks, listener.KeyspaceUpdatedEvents[0].New.Name, "Expected keyspace updated event to have correct keyspace name")
require.Contains(t, listener.KeyspaceUpdatedEvents[0].New.StrategyClass, "SimpleStrategy", "Expected keyspace updated event to have correct replication class")
require.Equal(t, "2", listener.KeyspaceUpdatedEvents[0].New.StrategyOptions["replication_factor"], "Expected keyspace updated event to have correct new replication factor")
time.Sleep(2 * time.Second)
listener.clear()
err = session.Query(fmt.Sprintf(`DROP KEYSPACE %s`, ks)).
RetryPolicy(&SimpleRetryPolicy{}).
Consistency(All).
Exec()
require.NoError(t, err, "Expected no error dropping keyspace")
require.Eventually(t, func() bool {
return len(listener.KeyspaceDroppedEvents) > 0
}, time.Second*60, time.Millisecond*100, "Expected keyspace dropped event to be received")
// Verify that the listener received the keyspace dropped event
require.Equal(t, ks, listener.KeyspaceDroppedEvents[0].Keyspace.Name, "Expected keyspace dropped event to have correct keyspace name")
}
func randomNameWithPrefix(prefix string) string {
return prefix + strings.ToLower(randomText(10))
}
func testSchemaEventsTable(t *testing.T, session *Session, listener *schemaChangesTestListener) {
table := randomNameWithPrefix("gocql_table_events_")
listener.clear()
err := session.Query(fmt.Sprintf("CREATE TABLE %s (id UUID PRIMARY KEY)", table)).Exec()
require.NoError(t, err, "Expected no error creating table")
require.Eventually(t, func() bool {
return len(listener.TableCreatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected table created event to be received")
// Verify that the listener received the table created event
require.Equal(t, table, listener.TableCreatedEvents[0].Table.Name, "Expected table created event to have correct table name")
require.Contains(t, listener.TableCreatedEvents[0].Table.Columns, "id")
require.IsType(t, listener.TableCreatedEvents[0].Table.Columns["id"].Type, uuidType{})
listener.clear()
err = session.Query(fmt.Sprintf("ALTER TABLE %s ADD name text", table)).Exec()
require.NoError(t, err, "Expected no error updating table")
// Verify that the listener received the table updated event
// and that the old and new metadata are correct
require.Eventually(t, func() bool {
return len(listener.TableUpdatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected table updated event to be received")
// Old metadata
require.Equal(t, table, listener.TableUpdatedEvents[0].Old.Name, "Expected table updated event to have correct table name")
require.NotContains(t, listener.TableUpdatedEvents[0].Old.Columns, "name")
// New metadata
require.Equal(t, table, listener.TableUpdatedEvents[0].New.Name, "Expected table updated event to have correct table name")
require.Contains(t, listener.TableUpdatedEvents[0].New.Columns, "name")
err = session.Query(fmt.Sprintf("DROP TABLE %s", table)).Exec()
require.NoError(t, err, "Expected no error dropping table")
require.Eventually(t, func() bool {
return len(listener.TableDroppedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected table dropped event to be received")
// Verify that the listener received the table dropped event
require.Equal(t, table, listener.TableDroppedEvents[0].Table.Name, "Expected table dropped event to have correct table name")
require.Contains(t, listener.TableDroppedEvents[0].Table.Columns, "name")
}
func testSchemaEventsFunction(t *testing.T, session *Session, listener *schemaChangesTestListener) {
functionName := randomNameWithPrefix("gocql_function_events_")
functionBody1 := "return Integer.valueOf(input + 1);"
listener.clear()
err := session.Query(fmt.Sprintf(`CREATE OR REPLACE FUNCTION %s (input int)
RETURNS NULL ON NULL INPUT
RETURNS int
LANGUAGE java AS '%s'`, functionName, functionBody1)).Exec()
require.NoError(t, err, "Expected no error creating function")
require.Eventually(t, func() bool {
return len(listener.FunctionCreatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected function created event to be received")
// Verify that the listener received the function created event
require.Equal(t, functionName, listener.FunctionCreatedEvents[0].Function.Name, "Expected function created event to have correct function name")
listener.clear()
functionBody2 := "return Integer.valueOf(input + 2);"
err = session.Query(fmt.Sprintf(`CREATE OR REPLACE FUNCTION %s (input int)
RETURNS NULL ON NULL INPUT
RETURNS int
LANGUAGE java AS '%s'`, functionName, functionBody2)).Exec()
require.NoError(t, err, "Expected no error creating function")
require.Eventually(t, func() bool {
return len(listener.FunctionUpdatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected function updated event to be received")
require.Equal(t, functionName, listener.FunctionUpdatedEvents[0].Old.Name)
require.Equal(t, functionName, listener.FunctionUpdatedEvents[0].New.Name)
listener.clear()
err = session.Query(fmt.Sprintf(`DROP FUNCTION %s (int)`, functionName)).Exec()
require.NoError(t, err, "Expected no error dropping function")
require.Eventually(t, func() bool {
return len(listener.FunctionDroppedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected function dropped event to be received")
require.Equal(t, functionName, listener.FunctionDroppedEvents[0].Function.Name)
}
func testSchemaEventsAggregate(t *testing.T, session *Session, listener *schemaChangesTestListener) {
stateFuncName := randomNameWithPrefix("gocql_agg_state_")
finalFuncName := randomNameWithPrefix("gocql_agg_final_")
aggName := randomNameWithPrefix("gocql_aggregate_events_")
listener.clear()
err := session.Query(fmt.Sprintf(`CREATE OR REPLACE FUNCTION gocql_test.%s ( state tuple<int,bigint>, val int )
CALLED ON NULL INPUT
RETURNS tuple<int,bigint>
LANGUAGE java AS
$$if (val !=null) {state.setInt(0, state.getInt(0)+1); state.setLong(1, state.getLong(1)+val.intValue());}return state;$$;`, stateFuncName)).Exec()
require.NoError(t, err, "Expected no error creating state function")
err = session.Query(fmt.Sprintf(`CREATE OR REPLACE FUNCTION gocql_test.%s ( state tuple<int,bigint> )
CALLED ON NULL INPUT
RETURNS double
LANGUAGE java AS
$$double r = 0; if (state.getInt(0) == 0) return null; r = state.getLong(1); r/= state.getInt(0); return Double.valueOf(r);$$`, finalFuncName)).Exec()
require.NoError(t, err, "Expected no error creating final function")
err = session.Query(fmt.Sprintf(`CREATE OR REPLACE AGGREGATE gocql_test.%s (int)
SFUNC %s
STYPE tuple<int,bigint>
FINALFUNC %s
INITCOND (0,0);`, aggName, stateFuncName, finalFuncName)).Exec()
require.NoError(t, err, "Expected no error creating aggregate")
require.Eventually(t, func() bool {
return len(listener.AggregateCreatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected aggregate created event to be received")
require.Equal(t, aggName, listener.AggregateCreatedEvents[0].Aggregate.Name, "Expected aggregate created event to have correct aggregate name")
listener.clear()
err = session.Query(fmt.Sprintf(`CREATE OR REPLACE AGGREGATE gocql_test.%s (int)
SFUNC %s
STYPE tuple<int,bigint>
FINALFUNC %s
INITCOND (1,1);`, aggName, stateFuncName, finalFuncName)).Exec()
require.NoError(t, err, "Expected no error updating aggregate")
require.Eventually(t, func() bool {
return len(listener.AggregateUpdatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected aggregate updated event to be received")
require.Equal(t, aggName, listener.AggregateUpdatedEvents[0].Old.Name, "Expected aggregate updated event to have correct old aggregate name")
require.Equal(t, aggName, listener.AggregateUpdatedEvents[0].New.Name, "Expected aggregate updated event to have correct new aggregate name")
listener.clear()
err = session.Query(fmt.Sprintf(`DROP AGGREGATE gocql_test.%s (int)`, aggName)).Exec()
require.NoError(t, err, "Expected no error dropping aggregate")
require.Eventually(t, func() bool {
return len(listener.AggregateDroppedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected aggregate dropped event to be received")
require.Equal(t, aggName, listener.AggregateDroppedEvents[0].Aggregate.Name, "Expected aggregate dropped event to have correct aggregate name")
}
func testSchemaEventsType(t *testing.T, session *Session, listener *schemaChangesTestListener) {
typeName := randomNameWithPrefix("gocql_type_events_")
listener.clear()
err := session.Query(fmt.Sprintf(`CREATE TYPE %s (id int, name text)`, typeName)).Exec()
require.NoError(t, err, "Expected no error creating type")
require.Eventually(t, func() bool {
return len(listener.TypeCreatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected type created event to be received")
require.Equal(t, typeName, listener.TypeCreatedEvents[0].UserType.Name)
require.Contains(t, listener.TypeCreatedEvents[0].UserType.FieldNames, "id")
require.Contains(t, listener.TypeCreatedEvents[0].UserType.FieldNames, "name")
listener.clear()
err = session.Query(fmt.Sprintf(`ALTER TYPE %s ADD age int`, typeName)).Exec()
require.NoError(t, err, "Expected no error updating type")
require.Eventually(t, func() bool {
return len(listener.TypeUpdatedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected type updated event to be received")
require.Equal(t, typeName, listener.TypeUpdatedEvents[0].Old.Name)
require.NotContains(t, listener.TypeUpdatedEvents[0].Old.FieldNames, "age")
require.Equal(t, typeName, listener.TypeUpdatedEvents[0].New.Name)
require.Contains(t, listener.TypeUpdatedEvents[0].New.FieldNames, "age")
err = session.Query(fmt.Sprintf(`DROP TYPE %s`, typeName)).Exec()
require.NoError(t, err, "Expected no error dropping type")
require.Eventually(t, func() bool {
return len(listener.TypeDroppedEvents) > 0
}, time.Second*5, time.Millisecond*100, "Expected type dropped event to be received")
require.Equal(t, typeName, listener.TypeDroppedEvents[0].UserType.Name)
}