Skip to content

v3: create separate mock for Watcher interface #486

Description

@bigbes

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions