Skip to content

Commit 934619e

Browse files
committed
Move WithClock to unit build tagged file
Signed-off-by: joshvanl <[email protected]>
1 parent 0e1fd37 commit 934619e

File tree

7 files changed

+121
-22
lines changed

7 files changed

+121
-22
lines changed

events/batcher/batcher.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ func New[T key](interval time.Duration) *Batcher[T] {
5252
}
5353
}
5454

55-
// WithClock sets the clock used by the batcher. Used for testing.
56-
func (b *Batcher[T]) WithClock(clock clock.WithDelayedExecution) {
57-
b.clock = clock
58-
}
59-
6055
// Subscribe adds a new event channel subscriber. If the batcher is closed, the
6156
// subscriber is silently dropped.
6257
func (b *Batcher[T]) Subscribe(eventCh ...chan<- struct{}) {

events/batcher/batcher_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ func TestNew(t *testing.T) {
3030
assert.False(t, b.closed.Load())
3131
}
3232

33-
func TestWithClock(t *testing.T) {
34-
b := New[string](time.Millisecond * 10)
35-
fakeClock := testingclock.NewFakeClock(time.Now())
36-
b.WithClock(fakeClock)
37-
assert.Equal(t, fakeClock, b.clock)
38-
}
39-
4033
func TestSubscribe(t *testing.T) {
4134
t.Parallel()
4235

events/batcher/batcher_unit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build unit
2+
// +build unit
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package batcher
18+
19+
import (
20+
"k8s.io/utils/clock"
21+
)
22+
23+
// WithClock sets the clock used by the batcher. Used for testing.
24+
func (b *Batcher[T]) WithClock(clock clock.WithDelayedExecution) {
25+
b.clock = clock
26+
}

events/batcher/batcher_unit_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build unit
2+
// +build unit
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package batcher
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/stretchr/testify/assert"
24+
clocktesting "k8s.io/utils/clock/testing"
25+
)
26+
27+
func TestWithClock(t *testing.T) {
28+
b := New[string](time.Millisecond * 10)
29+
fakeClock := clocktesting.NewFakeClock(time.Now())
30+
b.WithClock(fakeClock)
31+
assert.Equal(t, fakeClock, b.clock)
32+
}

events/queue/processor.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"sync/atomic"
2020
"time"
2121

22-
kclock "k8s.io/utils/clock"
22+
"k8s.io/utils/clock"
2323
)
2424

2525
// ErrProcessorStopped is returned when the processor is not running.
@@ -29,7 +29,7 @@ var ErrProcessorStopped = errors.New("processor is stopped")
2929
type Processor[T queueable] struct {
3030
executeFn func(r T)
3131
queue queue[T]
32-
clock kclock.Clock
32+
clock clock.Clock
3333
lock sync.Mutex
3434
wg sync.WaitGroup
3535
processorRunningCh chan struct{}
@@ -47,16 +47,10 @@ func NewProcessor[T queueable](executeFn func(r T)) *Processor[T] {
4747
processorRunningCh: make(chan struct{}, 1),
4848
stopCh: make(chan struct{}),
4949
resetCh: make(chan struct{}, 1),
50-
clock: kclock.RealClock{},
50+
clock: clock.RealClock{},
5151
}
5252
}
5353

54-
// WithClock sets the clock used by the processor. Used for testing.
55-
func (p *Processor[T]) WithClock(clock kclock.Clock) *Processor[T] {
56-
p.clock = clock
57-
return p
58-
}
59-
6054
// Enqueue adds a new item to the queue.
6155
// If a item with the same ID already exists, it'll be replaced.
6256
func (p *Processor[T]) Enqueue(r T) error {
@@ -149,7 +143,7 @@ func (p *Processor[T]) processLoop() {
149143
var (
150144
r T
151145
ok bool
152-
t kclock.Timer
146+
t clock.Timer
153147
scheduledTime time.Time
154148
deadline time.Duration
155149
)

events/queue/processor_unit.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build unit
2+
// +build unit
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package queue
18+
19+
import (
20+
"k8s.io/utils/clock"
21+
)
22+
23+
// WithClock sets the clock used by the processor. Used for testing.
24+
func (p *Processor[T]) WithClock(clock clock.Clock) *Processor[T] {
25+
p.clock = clock
26+
return p
27+
}

events/queue/processor_unit_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build unit
2+
// +build unit
3+
4+
/*
5+
Copyright 2023 The Dapr Authors
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package queue
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/stretchr/testify/assert"
24+
clocktesting "k8s.io/utils/clock/testing"
25+
)
26+
27+
func TestWithClock(t *testing.T) {
28+
p := NewProcessor(func(*queueableItem) {})
29+
fakeClock := clocktesting.NewFakeClock(time.Now())
30+
p.WithClock(fakeClock)
31+
assert.Equal(t, fakeClock, p.clock)
32+
}

0 commit comments

Comments
 (0)