From 50fac882cfe1df4f93193871750c65169e294a44 Mon Sep 17 00:00:00 2001 From: "Alessandro (Ale) Segala" <43508+ItalyPaleAle@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:59:34 -0700 Subject: [PATCH] events: make "WithClock" methods allowed without "unit" build tag (#62) 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 <43508+ItalyPaleAle@users.noreply.github.com> --- events/batcher/batcher.go | 5 +++++ events/batcher/batcher_test.go | 7 +++++++ events/batcher/batcher_unit.go | 26 ----------------------- events/batcher/batcher_unit_test.go | 32 ----------------------------- events/queue/processor.go | 6 ++++++ events/queue/processor_unit.go | 27 ------------------------ 6 files changed, 18 insertions(+), 85 deletions(-) delete mode 100644 events/batcher/batcher_unit.go delete mode 100644 events/batcher/batcher_unit_test.go delete mode 100644 events/queue/processor_unit.go diff --git a/events/batcher/batcher.go b/events/batcher/batcher.go index b5734fa..e6f58e9 100644 --- a/events/batcher/batcher.go +++ b/events/batcher/batcher.go @@ -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{}) { diff --git a/events/batcher/batcher_test.go b/events/batcher/batcher_test.go index 0d264c1..5deaec9 100644 --- a/events/batcher/batcher_test.go +++ b/events/batcher/batcher_test.go @@ -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() diff --git a/events/batcher/batcher_unit.go b/events/batcher/batcher_unit.go deleted file mode 100644 index 51dd412..0000000 --- a/events/batcher/batcher_unit.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build unit -// +build unit - -/* -Copyright 2023 The Dapr Authors -Licensed 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 batcher - -import ( - "k8s.io/utils/clock" -) - -// WithClock sets the clock used by the batcher. Used for testing. -func (b *Batcher[T]) WithClock(clock clock.WithDelayedExecution) { - b.clock = clock -} diff --git a/events/batcher/batcher_unit_test.go b/events/batcher/batcher_unit_test.go deleted file mode 100644 index af5e38d..0000000 --- a/events/batcher/batcher_unit_test.go +++ /dev/null @@ -1,32 +0,0 @@ -//go:build unit -// +build unit - -/* -Copyright 2023 The Dapr Authors -Licensed 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 batcher - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" - testingclock "k8s.io/utils/clock/testing" -) - -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) -} diff --git a/events/queue/processor.go b/events/queue/processor.go index b3bc1ac..b7661b5 100644 --- a/events/queue/processor.go +++ b/events/queue/processor.go @@ -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 { diff --git a/events/queue/processor_unit.go b/events/queue/processor_unit.go deleted file mode 100644 index 8726e07..0000000 --- a/events/queue/processor_unit.go +++ /dev/null @@ -1,27 +0,0 @@ -//go:build unit -// +build unit - -/* -Copyright 2023 The Dapr Authors -Licensed 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 queue - -import ( - kclock "k8s.io/utils/clock" -) - -// 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 -}