joao-r-reis commented on code in PR #1929: URL: https://github.com/apache/cassandra-gocql-driver/pull/1929#discussion_r2874085374
########## schema_events_test.go: ########## @@ -0,0 +1,442 @@ +//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 []KeyspaceCreatedEvent + KeyspaceUpdatedEvents []KeyspaceUpdatedEvent + KeyspaceDroppedEvents []KeyspaceDroppedEvent + + TableCreatedEvents []TableCreatedEvent + TableUpdatedEvents []TableUpdatedEvent + TableDroppedEvents []TableDroppedEvent + + FunctionCreatedEvents []FunctionCreatedEvent + FunctionUpdatedEvents []FunctionUpdatedEvent + FunctionDroppedEvents []FunctionDroppedEvent + + AggregateCreatedEvents []AggregateCreatedEvent + AggregateUpdatedEvents []AggregateUpdatedEvent + AggregateDroppedEvents []AggregateDroppedEvent + + TypeCreatedEvents []UserTypeCreatedEvent + TypeUpdatedEvents []UserTypeUpdatedEvent + TypeDroppedEvents []UserTypeDroppedEvent +} + +// OnAggregateCreated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnAggregateCreated(event AggregateCreatedEvent) { + (*s).AggregateCreatedEvents = append(s.AggregateCreatedEvents, event) +} + +// OnAggregateDropped implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnAggregateDropped(event AggregateDroppedEvent) { + (*s).AggregateDroppedEvents = append(s.AggregateDroppedEvents, event) +} + +// OnAggregateUpdated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnAggregateUpdated(event AggregateUpdatedEvent) { + (*s).AggregateUpdatedEvents = append(s.AggregateUpdatedEvents, event) +} + +// OnFunctionCreated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnFunctionCreated(event FunctionCreatedEvent) { + (*s).FunctionCreatedEvents = append(s.FunctionCreatedEvents, event) +} + +// OnFunctionDropped implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnFunctionDropped(event FunctionDroppedEvent) { + (*s).FunctionDroppedEvents = append(s.FunctionDroppedEvents, event) +} + +// OnFunctionUpdated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnFunctionUpdated(event FunctionUpdatedEvent) { + (*s).FunctionUpdatedEvents = append(s.FunctionUpdatedEvents, event) +} + +// OnKeyspaceCreated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnKeyspaceCreated(event KeyspaceCreatedEvent) { + (*s).KeyspaceCreatedEvents = append(s.KeyspaceCreatedEvents, event) +} + +// OnKeyspaceDropped implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnKeyspaceDropped(event KeyspaceDroppedEvent) { + (*s).KeyspaceDroppedEvents = append(s.KeyspaceDroppedEvents, event) +} + +// OnKeyspaceUpdated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnKeyspaceUpdated(event KeyspaceUpdatedEvent) { + (*s).KeyspaceUpdatedEvents = append(s.KeyspaceUpdatedEvents, event) +} + +// OnTableCreated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnTableCreated(event TableCreatedEvent) { + (*s).TableCreatedEvents = append(s.TableCreatedEvents, event) +} + +// OnTableDropped implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnTableDropped(event TableDroppedEvent) { + (*s).TableDroppedEvents = append(s.TableDroppedEvents, event) +} + +// OnTableUpdated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnTableUpdated(event TableUpdatedEvent) { + (*s).TableUpdatedEvents = append(s.TableUpdatedEvents, event) +} + +// OnTypeCreated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnUserTypeCreated(event UserTypeCreatedEvent) { + (*s).TypeCreatedEvents = append(s.TypeCreatedEvents, event) +} + +// OnTypeDropped implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnUserTypeDropped(event UserTypeDroppedEvent) { + (*s).TypeDroppedEvents = append(s.TypeDroppedEvents, event) +} + +// OnTypeUpdated implements [SchemaChangeListener]. +func (s *schemaChangesTestListener) OnUserTypeUpdated(event UserTypeUpdatedEvent) { + (*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.SchemaListeners.KeyspaceChangeListener = listener + config.Metadata.SchemaListeners.TableChangeListener = listener + config.Metadata.SchemaListeners.UserTypeChangeListener = listener + config.Metadata.SchemaListeners.FunctionChangeListener = listener + config.Metadata.SchemaListeners.AggregateChangeListener = listener Review Comment: might be cleaner to show this as the following for a less verbose example ``` config.Metadata.SchemaListeners = SchemaListeners{ KeyspaceChangeListener: listener, TableChangeListener: listener, UserTypeChangeListener: listener, FunctionChangeListener: listener, AggregateChangeListener: listener, } ``` I'm still not 100% happy with this and I'm still doing some thinking to see if I can come up with a design that would reduce the need to set the listeners separately (while keeping the good things about this design) even if the listener implements all the interfaces but let's go with this for now since I'm not sure if I'll be able to come up with something better -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

