Skip to content

Commit a66fe85

Browse files
committed
Bareminimum workqueue test
Signed-off-by: Swati Gupta <[email protected]>
1 parent 0003aef commit a66fe85

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

pkg/workqueue/workqueue_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2025 NVIDIA CORPORATION. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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 workqueue
18+
19+
import (
20+
"context"
21+
"sync/atomic"
22+
"testing"
23+
"time"
24+
25+
"k8s.io/apimachinery/pkg/runtime"
26+
)
27+
28+
func TestEnqueue(t *testing.T) {
29+
// Create a WorkQueue using the default rate limiter.
30+
defaultRateLimiter := DefaultControllerRateLimiter()
31+
wq := New(defaultRateLimiter)
32+
if wq == nil || wq.queue == nil {
33+
t.Fatal("Failed to create valid WorkQueue")
34+
}
35+
36+
// Create a context with timeout for processing.
37+
// use DefaultTypedControllerRateLimiter Base delay: 5ms
38+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
39+
defer cancel()
40+
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)
50+
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)
63+
}
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+
87+
}

0 commit comments

Comments
 (0)