-
Notifications
You must be signed in to change notification settings - Fork 8
/
monitor_test.go
117 lines (92 loc) · 2.64 KB
/
monitor_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package kcache
import (
"context"
"testing"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache/filter"
"github.com/boz/kcache/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestMonitor(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log := logutil.Default()
parent, cache, readych := testNewSubscription(t, log, filter.Null())
publisher := newPublisher(log, parent)
defer parent.Close()
icalled := make(chan bool)
ccalled := make(chan bool)
ucalled := make(chan bool)
dcalled := make(chan bool)
h := BuildHandler().OnInitialize(func(objs []metav1.Object) {
if assert.Len(t, objs, 1) {
assert.Equal(t, "a", objs[0].GetNamespace())
assert.Equal(t, "b", objs[0].GetName())
}
close(icalled)
}).OnCreate(func(obj metav1.Object) {
assert.Equal(t, "b", obj.GetNamespace())
assert.Equal(t, "b", obj.GetName())
close(ccalled)
}).OnUpdate(func(obj metav1.Object) {
assert.Equal(t, "b", obj.GetNamespace())
assert.Equal(t, "b", obj.GetName())
close(ucalled)
}).OnDelete(func(obj metav1.Object) {
assert.Equal(t, "b", obj.GetNamespace())
assert.Equal(t, "b", obj.GetName())
close(dcalled)
}).Create()
cache.sync([]metav1.Object{testGenPod("a", "b", "1")})
m, err := NewMonitor(publisher, h)
assert.NoError(t, err)
close(readych)
parent.send(testGenEvent(EventTypeCreate, "b", "b", "1"))
parent.send(testGenEvent(EventTypeUpdate, "b", "b", "2"))
parent.send(testGenEvent(EventTypeDelete, "b", "b", "3"))
select {
case <-icalled:
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, "initialize not called")
}
select {
case <-ccalled:
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, "create not called")
}
select {
case <-ucalled:
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, "update not called")
}
select {
case <-dcalled:
case <-testutil.AsyncWaitch(ctx):
assert.Fail(t, "delete not called")
}
m.Close()
testutil.AssertDone(t, "monitor", m)
}
func TestMonitor_lifecycle_close_early(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log := logutil.Default()
parent, _, readych := testNewSubscription(t, log, filter.Null())
publisher := newPublisher(log, parent)
calledch := make(chan bool)
h := BuildHandler().OnInitialize(func(_ []metav1.Object) {
close(calledch)
}).Create()
m, err := NewMonitor(publisher, h)
require.NoError(t, err)
publisher.Close()
testutil.AssertDone(t, "publisher", m)
close(readych)
select {
case <-calledch:
assert.Fail(t, "initialize called")
case <-testutil.AsyncWaitch(ctx):
}
}