We need a dedicated, reusable mock implementation of the tarantool.Watcher
interface to enable reliable and isolated unit testing for components that
depend on watch events.
The real Watcher typically works by calling a user-provided callback
(WatchCallback) whenever a new WatchEvent is received from Tarantool.
In tests, we want to simulate this behavior without connecting to a real
instance.
Suggested Approaches (choose one or combine ideas)
-
Channel-based event source
The mock accepts a <-chan *tarantool.WatchEvent at creation.
Internally, it reads from this channel and invokes the callback
for each event. This mimics a live stream of updates.
ch := make(chan *tarantool.WatchEvent)
mockW := mock.NewWatcher(ch)
mockW.Watch(ctx, "mykey", func(ev *tarantool.WatchEvent) {
// handle event
})
ch <- &tarantool.WatchEvent{Key: "mykey", Data: []byte("updated")}
close(ch) // optional: signal end
-
Preloaded event list
The mock is initialized with a fixed slice of events. When Watch()
is called, it immediately (or after a trigger) replays all events
through the callback.
events := []*tarantool.WatchEvent{
{Key: "foo", Data: []byte("v1")},
{Key: "foo", Data: []byte("v2")},
}
mockW := mock.NewWatcherWithEvents(events...)
mockW.NewWatcher("foo", cb)
// all events are delivered synchronously or via a controlled tick
-
Manual event injection
Expose a method like Send(*WatchEvent) so tests can push events on demand:
mockW := mock.NewWatcher()
mockW.Watch(ctx, "bar", cb)
mockW.Send(&tarantool.WatchEvent{Key: "bar", Data: []byte("hello")})
Choose the design that offers the best balance of simplicity and flexibility.
If unsure, the manual Send() approach is often the most test-friendly and
easiest to reason about.
We need a dedicated, reusable mock implementation of the
tarantool.Watcherinterface to enable reliable and isolated unit testing for components that
depend on watch events.
The real
Watchertypically works by calling a user-provided callback(
WatchCallback) whenever a newWatchEventis received from Tarantool.In tests, we want to simulate this behavior without connecting to a real
instance.
Suggested Approaches (choose one or combine ideas)
Channel-based event source
The mock accepts a
<-chan *tarantool.WatchEventat creation.Internally, it reads from this channel and invokes the callback
for each event. This mimics a live stream of updates.
Preloaded event list
The mock is initialized with a fixed slice of events. When
Watch()is called, it immediately (or after a trigger) replays all events
through the callback.
Manual event injection
Expose a method like
Send(*WatchEvent)so tests can push events on demand:Choose the design that offers the best balance of simplicity and flexibility.
If unsure, the manual
Send()approach is often the most test-friendly andeasiest to reason about.