-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathworkqueue_test.go
70 lines (64 loc) · 2.13 KB
/
workqueue_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
package kubehandler_test
import (
"context"
"testing"
"time"
kubehandlerv2 "github.com/gojektech/kubehandler/v2"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
)
func TestShouldEnqueueIntoTheUnderlyingWorkQueue(t *testing.T) {
workQueue := kubehandlerv2.NewWorkQueue("WorkqueueTest")
workQueue.EnqueueAdd("someKind", &appsv1.Deployment{})
timeCompleted := make(chan string, 1)
go func() {
time.Sleep(1 * time.Second)
timeCompleted <- "done"
}()
select {
case <-timeCompleted:
assert.Equal(t, 1, workQueue.Length())
case <-time.After(2 * time.Second):
assert.Fail(t, "Nothing in the work queue after timeout")
}
}
func TestShouldCallRegisteredAddFuncWhenAddEventIsReceived(t *testing.T) {
workQueue := kubehandlerv2.NewWorkQueue("WorkqueueTest2")
kind := "Foo"
addHandlerCalled := make(chan bool, 1)
workQueue.RegisterAddHandler(kind, func(ctx context.Context, namespace, name string) error {
addHandlerCalled <- true
return nil
})
workQueue.EnqueueAdd(kind, &appsv1.Deployment{})
go workQueue.Run(context.TODO(), 1)
assert.True(t, <-addHandlerCalled)
}
func TestShouldCallRegisteredUpdateFuncWhenUpdateEventIsReceived(t *testing.T) {
workQueue := kubehandlerv2.NewWorkQueue("WorkqueueTest3")
kind := "Foo"
updateHandlerCalled := make(chan bool, 1)
ctx, cancelFunc := context.WithCancel(context.Background())
workQueue.RegisterUpdateHandler(kind, func(ctx context.Context, namespace, name string) error {
updateHandlerCalled <- true
return nil
})
workQueue.EnqueueUpdate(kind, &appsv1.Deployment{})
go workQueue.Run(ctx, 1)
assert.True(t, <-updateHandlerCalled)
cancelFunc()
}
func TestShouldCallRegisteredDeleteFuncWhenDeleteEventIsReceived(t *testing.T) {
workQueue := kubehandlerv2.NewWorkQueue("WorkqueueTest4")
kind := "Foo"
deleteHandlerCalled := make(chan bool, 1)
workQueue.RegisterDeleteHandler(kind, func(ctx context.Context, namespace, name string) error {
deleteHandlerCalled <- true
return nil
})
ctx, cancelFunc := context.WithCancel(context.Background())
workQueue.EnqueueDelete(kind, &appsv1.Deployment{})
go workQueue.Run(ctx, 1)
assert.True(t, <-deleteHandlerCalled)
cancelFunc()
}