@@ -22,66 +22,75 @@ import (
2222 "testing"
2323 "time"
2424
25+ "github.com/stretchr/testify/require"
2526 "k8s.io/apimachinery/pkg/runtime"
2627)
2728
2829func TestEnqueue (t * testing.T ) {
2930 // Create a WorkQueue using the default rate limiter.
3031 defaultRateLimiter := DefaultControllerRateLimiter ()
3132 wq := New (defaultRateLimiter )
32- if wq == nil || wq .queue == nil {
33- t .Fatal ("Failed to create valid WorkQueue" )
33+ require .NotNil (t , wq )
34+ require .NotNil (t , wq .queue )
35+
36+ tests := []struct {
37+ name string
38+ obj any
39+ called * int32
40+ }{
41+ {
42+ name : "EnqueueRaw" ,
43+ obj : "AnyObject" ,
44+ called : new (int32 ),
45+ },
46+ {
47+ name : "EnqueueValid" ,
48+ obj : & runtime.Unknown {},
49+ called : new (int32 ),
50+ },
51+ {
52+ name : "EnqueueInvalid" ,
53+ obj : "NotRuntimeObject" ,
54+ },
55+ {
56+ name : "NilCallback" ,
57+ obj : & runtime.Unknown {},
58+ },
3459 }
3560
3661 // Create a context with timeout for processing.
3762 // use DefaultTypedControllerRateLimiter Base delay: 5ms
3863 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Millisecond )
3964 defer cancel ()
4065
41- // Test EnqueueRaw
42- t .Run ("EnqueueRaw" , func (t * testing.T ) {
43- var called int32
44- callback := func (ctx context.Context , obj any ) error {
45- atomic .StoreInt32 (& called , 1 )
46- return nil
47- }
48- wq .EnqueueRaw ("AnyObject" , callback )
49- wq .processNextWorkItem (ctx )
66+ for _ , tt := range tests {
67+ t .Run (tt .name , func (t * testing.T ) {
68+ var callback func (context.Context , any ) error
69+ switch tt .name {
70+ case "EnqueueRaw" :
71+ callback = func (ctx context.Context , obj any ) error {
72+ atomic .StoreInt32 (tt .called , 1 )
73+ return nil
74+ }
75+ case "EnqueueValid" :
76+ callback = func (ctx context.Context , obj any ) error {
77+ if _ , ok := obj .(runtime.Object ); ! ok {
78+ t .Errorf ("Expected runtime.Object, got %T" , obj )
79+ }
80+ atomic .StoreInt32 (tt .called , 1 )
81+ return nil
82+ }
83+ default :
84+ callback = nil
5085
51- if atomic .LoadInt32 (& called ) != 1 {
52- t .Error ("EnqueueRaw callback was not invoked" )
53- }
54- })
55- // Test Enqueue with valid and invalid runtime.Object and nill callback
56- // TODO: Implement a proper claim spec that needs to be processed
57- t .Run ("EnqueueValid" , func (t * testing.T ) {
58- var called int32
59- callback := func (ctx context.Context , obj any ) error {
60- _ , ok := obj .(runtime.Object )
61- if ! ok {
62- t .Errorf ("Expected runtime.Object, got %T" , obj )
86+ wq .Enqueue (tt .obj , callback )
87+ if tt .name != "EnqueueInvalid" {
88+ wq .processNextWorkItem (ctx )
89+ }
90+ if tt .called != nil && atomic .LoadInt32 (tt .called ) != 1 {
91+ t .Error ("Callback was not invoked" )
92+ }
6393 }
64- atomic .StoreInt32 (& called , 1 )
65- return nil
66- }
67- validObj := & runtime.Unknown {}
68- wq .Enqueue (validObj , callback )
69- wq .processNextWorkItem (ctx )
70-
71- if atomic .LoadInt32 (& called ) != 1 {
72- t .Error ("Enqueue callback was not invoked" )
73- }
74- })
75-
76- t .Run ("EnqueueInvalid" , func (t * testing.T ) {
77- callback := func (ctx context.Context , obj any ) error { return nil }
78- wq .Enqueue ("NotRuntimeObject" , callback )
79- })
80-
81- t .Run ("NilCallback" , func (t * testing.T ) {
82- validObj := & runtime.Unknown {}
83- wq .Enqueue (validObj , nil )
84- wq .processNextWorkItem (ctx )
85- })
86-
94+ })
95+ }
8796}
0 commit comments