|
| 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 | + "github.com/stretchr/testify/require" |
| 26 | + "k8s.io/apimachinery/pkg/runtime" |
| 27 | +) |
| 28 | + |
| 29 | +func TestEnqueue(t *testing.T) { |
| 30 | + // Create a WorkQueue using the default rate limiter. |
| 31 | + defaultRateLimiter := DefaultControllerRateLimiter() |
| 32 | + wq := New(defaultRateLimiter) |
| 33 | + |
| 34 | + require.NotNil(t, wq) |
| 35 | + require.NotNil(t, wq.queue) |
| 36 | + |
| 37 | + // Create a context with timeout for processing. |
| 38 | + // use DefaultTypedControllerRateLimiter Base delay: 5ms |
| 39 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond) |
| 40 | + defer cancel() |
| 41 | + |
| 42 | + // Test EnqueueRaw |
| 43 | + t.Run("EnqueueRaw", func(t *testing.T) { |
| 44 | + var called int32 |
| 45 | + callback := func(ctx context.Context, obj any) error { |
| 46 | + atomic.StoreInt32(&called, 1) |
| 47 | + return nil |
| 48 | + } |
| 49 | + wq.EnqueueRaw("AnyObject", callback) |
| 50 | + wq.processNextWorkItem(ctx) |
| 51 | + |
| 52 | + if atomic.LoadInt32(&called) != 1 { |
| 53 | + t.Error("EnqueueRaw callback was not invoked") |
| 54 | + } |
| 55 | + }) |
| 56 | + // Test Enqueue with valid and invalid runtime.Object and nil callback |
| 57 | + // TODO: Implement a proper claim spec that needs to be processed |
| 58 | + t.Run("EnqueueValid", func(t *testing.T) { |
| 59 | + var called int32 |
| 60 | + callback := func(ctx context.Context, obj any) error { |
| 61 | + _, ok := obj.(runtime.Object) |
| 62 | + if !ok { |
| 63 | + t.Errorf("Expected runtime.Object, got %T", obj) |
| 64 | + } |
| 65 | + atomic.StoreInt32(&called, 1) |
| 66 | + return nil |
| 67 | + } |
| 68 | + validObj := &runtime.Unknown{} |
| 69 | + wq.Enqueue(validObj, callback) |
| 70 | + wq.processNextWorkItem(ctx) |
| 71 | + |
| 72 | + if atomic.LoadInt32(&called) != 1 { |
| 73 | + t.Error("Enqueue callback was not invoked") |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("EnqueueInvalid", func(t *testing.T) { |
| 78 | + callback := func(ctx context.Context, obj any) error { return nil } |
| 79 | + wq.Enqueue("NotRuntimeObject", callback) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("NilCallback", func(t *testing.T) { |
| 83 | + validObj := &runtime.Unknown{} |
| 84 | + wq.Enqueue(validObj, nil) |
| 85 | + wq.processNextWorkItem(ctx) |
| 86 | + }) |
| 87 | +} |
0 commit comments