Skip to content

Commit 8e6a866

Browse files
committed
Deflake TestExpectationsOnRecreate
1 parent 6324c13 commit 8e6a866

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

pkg/controller/daemon/daemon_controller_test.go

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,39 @@ func TestExpectationsOnRecreate(t *testing.T) {
469469
t.Fatal(err)
470470
}
471471

472+
expectStableQueueLength := func(expected int) {
473+
t.Helper()
474+
for i := 0; i < 5; i++ {
475+
if actual := dsc.queue.Len(); actual != expected {
476+
t.Fatalf("expected queue len to remain at %d, got %d", expected, actual)
477+
}
478+
time.Sleep(10 * time.Millisecond)
479+
}
480+
}
481+
waitForQueueLength := func(expected int, msg string) {
482+
t.Helper()
483+
i := 0
484+
err = wait.PollImmediate(100*time.Millisecond, informerSyncTimeout, func() (bool, error) {
485+
current := dsc.queue.Len()
486+
switch {
487+
case current == expected:
488+
return true, nil
489+
case current > expected:
490+
return false, fmt.Errorf("queue length %d exceeded expected length %d", current, expected)
491+
default:
492+
i++
493+
if i > 1 {
494+
t.Logf("Waiting for queue to have %d item, currently has: %d", expected, current)
495+
}
496+
return false, nil
497+
}
498+
})
499+
if err != nil {
500+
t.Fatalf("%s: %v", msg, err)
501+
}
502+
expectStableQueueLength(expected)
503+
}
504+
472505
fakeRecorder := record.NewFakeRecorder(100)
473506
dsc.eventRecorder = fakeRecorder
474507

@@ -499,24 +532,16 @@ func TestExpectationsOnRecreate(t *testing.T) {
499532
t.Fatal("caches failed to sync")
500533
}
501534

502-
if dsc.queue.Len() != 0 {
503-
t.Fatal("Unexpected item in the queue")
504-
}
535+
expectStableQueueLength(0)
505536

506537
oldDS := newDaemonSet("test")
507538
oldDS, err = client.AppsV1().DaemonSets(oldDS.Namespace).Create(context.Background(), oldDS, metav1.CreateOptions{})
508539
if err != nil {
509540
t.Fatal(err)
510541
}
511542

512-
err = wait.PollImmediate(100*time.Millisecond, informerSyncTimeout, func() (bool, error) {
513-
klog.V(8).Infof("Waiting for queue to have 1 item, currently has: %d", dsc.queue.Len())
514-
return dsc.queue.Len() == 1, nil
515-
})
516-
if err != nil {
517-
t.Fatalf("initial DS didn't result in new item in the queue: %v", err)
518-
}
519-
543+
// create of DS adds to queue, processes
544+
waitForQueueLength(1, "created DS")
520545
ok = dsc.processNextWorkItem()
521546
if !ok {
522547
t.Fatal("queue is shutting down")
@@ -538,28 +563,28 @@ func TestExpectationsOnRecreate(t *testing.T) {
538563
t.Fatal(err)
539564
}
540565
if !exists {
541-
t.Errorf("No expectations found for DaemonSet %q", oldDSKey)
566+
t.Fatalf("No expectations found for DaemonSet %q", oldDSKey)
542567
}
543568
if dsExp.Fulfilled() {
544569
t.Errorf("There should be unfulfiled expectation for creating new pods for DaemonSet %q", oldDSKey)
545570
}
546571

547-
if dsc.queue.Len() != 0 {
548-
t.Fatal("Unexpected item in the queue")
572+
// process updates DS, update adds to queue
573+
waitForQueueLength(1, "updated DS")
574+
ok = dsc.processNextWorkItem()
575+
if !ok {
576+
t.Fatal("queue is shutting down")
549577
}
550578

579+
// process does not re-update the DS
580+
expectStableQueueLength(0)
581+
551582
err = client.AppsV1().DaemonSets(oldDS.Namespace).Delete(context.Background(), oldDS.Name, metav1.DeleteOptions{})
552583
if err != nil {
553584
t.Fatal(err)
554585
}
555586

556-
err = wait.PollImmediate(100*time.Millisecond, informerSyncTimeout, func() (bool, error) {
557-
klog.V(8).Infof("Waiting for queue to have 1 item, currently has: %d", dsc.queue.Len())
558-
return dsc.queue.Len() == 1, nil
559-
})
560-
if err != nil {
561-
t.Fatalf("Deleting DS didn't result in new item in the queue: %v", err)
562-
}
587+
waitForQueueLength(1, "deleted DS")
563588

564589
_, exists, err = dsc.expectations.GetExpectations(oldDSKey)
565590
if err != nil {
@@ -579,9 +604,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
579604
t.Fatal("Keys should be equal!")
580605
}
581606

582-
if dsc.queue.Len() != 0 {
583-
t.Fatal("Unexpected item in the queue")
584-
}
607+
expectStableQueueLength(0)
585608

586609
newDS := oldDS.DeepCopy()
587610
newDS.UID = uuid.NewUUID()
@@ -595,14 +618,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
595618
t.Fatal("New DS has the same UID as the old one!")
596619
}
597620

598-
err = wait.PollImmediate(100*time.Millisecond, informerSyncTimeout, func() (bool, error) {
599-
klog.V(8).Infof("Waiting for queue to have 1 item, currently has: %d", dsc.queue.Len())
600-
return dsc.queue.Len() == 1, nil
601-
})
602-
if err != nil {
603-
t.Fatalf("Re-creating DS didn't result in new item in the queue: %v", err)
604-
}
605-
621+
waitForQueueLength(1, "recreated DS")
606622
ok = dsc.processNextWorkItem()
607623
if !ok {
608624
t.Fatal("Queue is shutting down!")
@@ -617,7 +633,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
617633
t.Fatal(err)
618634
}
619635
if !exists {
620-
t.Errorf("No expectations found for DaemonSet %q", oldDSKey)
636+
t.Fatalf("No expectations found for DaemonSet %q", oldDSKey)
621637
}
622638
if dsExp.Fulfilled() {
623639
t.Errorf("There should be unfulfiled expectation for creating new pods for DaemonSet %q", oldDSKey)

0 commit comments

Comments
 (0)