-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_test.go
73 lines (59 loc) · 1.8 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package dataloader
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"context"
"time"
)
type MockBatch struct{}
func (*MockBatch) Full() <-chan struct{} {
return make(<-chan struct{})
}
func (*MockBatch) Dispatch() <-chan struct{} {
return make(<-chan struct{})
}
var _ = Describe("NewTimeWindowScheduler", func() {
It("should run after specified duration", func() {
scheduler := NewTimeWindowScheduler(200 * time.Millisecond)
runned := false
go scheduler(context.TODO(), &MockBatch{}, func() { runned = true })
Expect(runned).To(BeFalse())
<-time.After(200 * time.Millisecond)
Expect(runned).To(BeTrue())
})
It("can cancel", func() {
scheduler := NewTimeWindowScheduler(200 * time.Millisecond)
runned := false
ctx, cancel := context.WithCancel(context.TODO())
go scheduler(ctx, &MockBatch{}, func() { runned = true })
cancel()
<-time.After(200 * time.Millisecond)
Expect(runned).To(BeFalse())
})
})
var _ = Describe("NewMirrorCacheKey", func() {
It("uncomparable value", func() {
cacheKeyFn := NewMirrorCacheKey[[]string, string]()
val, err := cacheKeyFn(context.TODO(), []string{})
Expect(err).To(Equal(ErrUncomparableKey))
Expect(val).To(Equal(""))
})
It("unconvertible value", func() {
cacheKeyFn := NewMirrorCacheKey[struct{}, string]()
val, err := cacheKeyFn(context.TODO(), struct{}{})
Expect(err).To(Equal(ErrUnconvertibleKey))
Expect(val).To(Equal(""))
})
It("string to string", func() {
cacheKeyFn := NewMirrorCacheKey[string, string]()
val, err := cacheKeyFn(context.TODO(), "foo")
Expect(err).To(BeNil())
Expect(val).To(Equal("foo"))
})
It("int32 to int64", func() {
cacheKeyFn := NewMirrorCacheKey[int32, int64]()
val, err := cacheKeyFn(context.TODO(), int32(123))
Expect(err).To(BeNil())
Expect(val).To(Equal(int64(123)))
})
})