Skip to content

Commit

Permalink
events: make "WithClock" methods allowed without "unit" build tag (#62)
Browse files Browse the repository at this point in the history
There are use cases where having those methods available even outside of a unit test is helpful, such as when the objects are instantiated with a clock that could be mocked in the unit test for the parent method.

Signed-off-by: ItalyPaleAle <[email protected]>
  • Loading branch information
ItalyPaleAle authored Sep 13, 2023
1 parent f08dc3f commit 50fac88
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 85 deletions.
5 changes: 5 additions & 0 deletions events/batcher/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func New[T key](interval time.Duration) *Batcher[T] {
}
}

// WithClock sets the clock used by the batcher. Used for testing.
func (b *Batcher[T]) WithClock(clock clock.WithDelayedExecution) {
b.clock = clock
}

// Subscribe adds a new event channel subscriber. If the batcher is closed, the
// subscriber is silently dropped.
func (b *Batcher[T]) Subscribe(eventCh ...chan<- struct{}) {
Expand Down
7 changes: 7 additions & 0 deletions events/batcher/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func TestNew(t *testing.T) {
assert.False(t, b.closed.Load())
}

func TestWithClock(t *testing.T) {
b := New[string](time.Millisecond * 10)
fakeClock := testingclock.NewFakeClock(time.Now())
b.WithClock(fakeClock)
assert.Equal(t, fakeClock, b.clock)
}

func TestSubscribe(t *testing.T) {
t.Parallel()

Expand Down
26 changes: 0 additions & 26 deletions events/batcher/batcher_unit.go

This file was deleted.

32 changes: 0 additions & 32 deletions events/batcher/batcher_unit_test.go

This file was deleted.

6 changes: 6 additions & 0 deletions events/queue/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func NewProcessor[T queueable](executeFn func(r T)) *Processor[T] {
}
}

// WithClock sets the clock used by the processor. Used for testing.
func (p *Processor[T]) WithClock(clock kclock.Clock) *Processor[T] {
p.clock = clock
return p
}

// Enqueue adds a new item to the queue.
// If a item with the same ID already exists, it'll be replaced.
func (p *Processor[T]) Enqueue(r T) error {
Expand Down
27 changes: 0 additions & 27 deletions events/queue/processor_unit.go

This file was deleted.

0 comments on commit 50fac88

Please sign in to comment.