mirror of https://github.com/dapr/kit.git
events: make "WithClock" methods allowed without "unit" build tag
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>
This commit is contained in:
parent
f08dc3ff16
commit
e111e25161
|
|
@ -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
|
// Subscribe adds a new event channel subscriber. If the batcher is closed, the
|
||||||
// subscriber is silently dropped.
|
// subscriber is silently dropped.
|
||||||
func (b *Batcher[T]) Subscribe(eventCh ...chan<- struct{}) {
|
func (b *Batcher[T]) Subscribe(eventCh ...chan<- struct{}) {
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,13 @@ func TestNew(t *testing.T) {
|
||||||
assert.False(t, b.closed.Load())
|
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) {
|
func TestSubscribe(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
@ -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.
|
// Enqueue adds a new item to the queue.
|
||||||
// If a item with the same ID already exists, it'll be replaced.
|
// If a item with the same ID already exists, it'll be replaced.
|
||||||
func (p *Processor[T]) Enqueue(r T) error {
|
func (p *Processor[T]) Enqueue(r T) error {
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue