go-tarantool icon indicating copy to clipboard operation
go-tarantool copied to clipboard

v3: create separate mock for Watcher interface

Open bigbes opened this issue 1 month ago • 0 comments

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)

  1. 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
    
  2. 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
    
  3. 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.

bigbes avatar Oct 30 '25 09:10 bigbes