Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bareminimum workqueue test #263

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions pkg/workqueue/workqueue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2025 NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package workqueue

import (
"context"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
)

func TestEnqueue(t *testing.T) {
// Create a WorkQueue using the default rate limiter.
defaultRateLimiter := DefaultControllerRateLimiter()
wq := New(defaultRateLimiter)

require.NotNil(t, wq)
require.NotNil(t, wq.queue)

// Create a context with timeout for processing.
// use DefaultTypedControllerRateLimiter Base delay: 5ms
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()

// Test EnqueueRaw
t.Run("EnqueueRaw", func(t *testing.T) {
var called int32
callback := func(ctx context.Context, obj any) error {
atomic.StoreInt32(&called, 1)
return nil
}
wq.EnqueueRaw("AnyObject", callback)
wq.processNextWorkItem(ctx)

if atomic.LoadInt32(&called) != 1 {
t.Error("EnqueueRaw callback was not invoked")
}
})
// Test Enqueue with valid and invalid runtime.Object and nil callback
// TODO: Implement a proper claim spec that needs to be processed
t.Run("EnqueueValid", func(t *testing.T) {
var called int32
callback := func(ctx context.Context, obj any) error {
_, ok := obj.(runtime.Object)
if !ok {
t.Errorf("Expected runtime.Object, got %T", obj)
}
atomic.StoreInt32(&called, 1)
return nil
}
validObj := &runtime.Unknown{}
wq.Enqueue(validObj, callback)
wq.processNextWorkItem(ctx)

if atomic.LoadInt32(&called) != 1 {
t.Error("Enqueue callback was not invoked")
}
Comment on lines +72 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you comment on why it's required to use atomics in the context of the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read that this is a standard go practice to use atomics to test callback functions

})

t.Run("EnqueueInvalid", func(t *testing.T) {
callback := func(ctx context.Context, obj any) error { return nil }
wq.Enqueue("NotRuntimeObject", callback)
})

t.Run("NilCallback", func(t *testing.T) {
validObj := &runtime.Unknown{}
wq.Enqueue(validObj, nil)
wq.processNextWorkItem(ctx)
})
Comment on lines +30 to +86
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Create a WorkQueue using the default rate limiter.
defaultRateLimiter := DefaultControllerRateLimiter()
wq := New(defaultRateLimiter)
require.NotNil(t, wq)
require.NotNil(t, wq.queue)
// Create a context with timeout for processing.
// use DefaultTypedControllerRateLimiter Base delay: 5ms
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
// Test EnqueueRaw
t.Run("EnqueueRaw", func(t *testing.T) {
var called int32
callback := func(ctx context.Context, obj any) error {
atomic.StoreInt32(&called, 1)
return nil
}
wq.EnqueueRaw("AnyObject", callback)
wq.processNextWorkItem(ctx)
if atomic.LoadInt32(&called) != 1 {
t.Error("EnqueueRaw callback was not invoked")
}
})
// Test Enqueue with valid and invalid runtime.Object and nil callback
// TODO: Implement a proper claim spec that needs to be processed
t.Run("EnqueueValid", func(t *testing.T) {
var called int32
callback := func(ctx context.Context, obj any) error {
_, ok := obj.(runtime.Object)
if !ok {
t.Errorf("Expected runtime.Object, got %T", obj)
}
atomic.StoreInt32(&called, 1)
return nil
}
validObj := &runtime.Unknown{}
wq.Enqueue(validObj, callback)
wq.processNextWorkItem(ctx)
if atomic.LoadInt32(&called) != 1 {
t.Error("Enqueue callback was not invoked")
}
})
t.Run("EnqueueInvalid", func(t *testing.T) {
callback := func(ctx context.Context, obj any) error { return nil }
wq.Enqueue("NotRuntimeObject", callback)
})
t.Run("NilCallback", func(t *testing.T) {
validObj := &runtime.Unknown{}
wq.Enqueue(validObj, nil)
wq.processNextWorkItem(ctx)
})
wq := New(DefaultControllerRateLimiter())
require.NotNil(t, wq)
require.NotNil(t, wq.queue)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
tests := []struct {
name string
obj any
callback func(context.Context, any) error
validate func(*testing.T, *int32)
processNextWorkItem bool
}{
{
name: "EnqueueRaw",
obj: "AnyObject",
callback: func(ctx context.Context, obj any) error {
atomic.StoreInt32(obj.(*int32), 1)
return nil
},
validate: func(t *testing.T, called *int32) {
if atomic.LoadInt32(called) != 1 {
t.Error("EnqueueRaw callback was not invoked")
}
},
processNextWorkItem: true,
},
{
name: "EnqueueValid",
obj: &runtime.Unknown{},
callback: func(ctx context.Context, obj any) error {
if _, ok := obj.(runtime.Object); !ok {
t.Errorf("Expected runtime.Object, got %T", obj)
}
atomic.StoreInt32(new(int32), 1)
return nil
},
validate: func(t *testing.T, called *int32) {
// No validation needed for runtime.Object type
},
processNextWorkItem: true,
},
{
name: "EnqueueInvalid",
obj: "NotRuntimeObject",
callback: func(ctx context.Context, obj any) error { return nil },
validate: nil,
processNextWorkItem: false,
},
{
name: "NilCallback",
obj: &runtime.Unknown{},
callback: nil,
validate: nil,
processNextWorkItem: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var called int32
if tt.name == "EnqueueRaw" {
wq.EnqueueRaw(&called, tt.callback)
} else {
wq.Enqueue(tt.obj, tt.callback)
}
if tt.processNextWorkItem {
wq.processNextWorkItem(ctx)
if tt.validate != nil {
tt.validate(t, &called)
}
}
})
}

I have tested this suggestion it works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it may be a bit of an overkill to add the extra validate func and bool just to make the test compact. If its not strongly desired, I would like to stick to the current format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArangoGutierrez how do we move forward with this? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ArangoGutierrez how do we move forward with this? :)

}