Copyright 2023 The Kubernetes Authors.
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 queue
import (
"context"
"errors"
"fmt"
"testing"
"time"
fakeclock "k8s.io/utils/clock/testing"
"sigs.k8s.io/kwok/pkg/utils/wait"
)
func TestSimpleAddAfter(t *testing.T) {
fakeClock := fakeclock.NewFakeClock(time.Now())
dq := NewDelayingQueue[string](fakeClock)
first := "foo"
dq.AddAfter(first, 50*time.Millisecond)
fakeClock.Step(10 * time.Millisecond)
err := checkIncreased(dq)
if err != nil {
t.Fatal(err)
}
fakeClock.Step(100 * time.Millisecond)
err = checkLength(dq, 1)
if err != nil {
t.Fatal(err)
}
}
func TestCancel(t *testing.T) {
fakeClock := fakeclock.NewFakeClock(time.Now())
dq := NewDelayingQueue[string](fakeClock)
first := "foo"
dq.AddAfter(first, 500*time.Millisecond)
fakeClock.Step(100 * time.Millisecond)
dq.Cancel(first)
fakeClock.Step(1 * time.Second)
err := checkIncreased(dq)
if err != nil {
t.Fatal(err)
}
}
func TestAddTwo(t *testing.T) {
fakeClock := fakeclock.NewFakeClock(time.Now())
dq := NewDelayingQueue[string](fakeClock)
first := "foo"
second := "bar"
dq.AddAfter(first, 1*time.Second)
dq.AddAfter(second, 500*time.Millisecond)
fakeClock.Step(600 * time.Millisecond)
err := checkLength(dq, 1)
if err != nil {
t.Fatal(err)
}
x, ok := dq.Get()
if !ok || x != second {
t.Fatalf("expected %s, got %s", second, x)
}
fakeClock.Step(1 * time.Second)
err = checkLength(dq, 1)
if err != nil {
t.Fatal(err)
}
x, ok = dq.Get()
if !ok || x != first {
t.Fatalf("expected %s, got %s", first, x)
}
}
type length interface {
Len() int
}
func checkLength(q length, l int) error {
return wait.Poll(context.TODO(), func(ctx context.Context) (done bool, err error) {
if q.Len() == l {
return true, nil
}
return false, nil
}, wait.WithInterval(1*time.Millisecond), wait.WithTimeout(100*time.Millisecond))
}
func checkIncreased(q length) error {
curLen := q.Len()
err := wait.Poll(context.TODO(), func(ctx context.Context) (done bool, err error) {
if q.Len()-curLen > 0 {
return true, fmt.Errorf("the queue should not increase")
}
return false, nil
}, wait.WithInterval(1*time.Millisecond), wait.WithTimeout(100*time.Millisecond))
if errors.Is(err, context.DeadlineExceeded) {
return nil
}
return err
}