@@ -469,6 +469,39 @@ func TestExpectationsOnRecreate(t *testing.T) {
469
469
t .Fatal (err )
470
470
}
471
471
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
+
472
505
fakeRecorder := record .NewFakeRecorder (100 )
473
506
dsc .eventRecorder = fakeRecorder
474
507
@@ -499,24 +532,16 @@ func TestExpectationsOnRecreate(t *testing.T) {
499
532
t .Fatal ("caches failed to sync" )
500
533
}
501
534
502
- if dsc .queue .Len () != 0 {
503
- t .Fatal ("Unexpected item in the queue" )
504
- }
535
+ expectStableQueueLength (0 )
505
536
506
537
oldDS := newDaemonSet ("test" )
507
538
oldDS , err = client .AppsV1 ().DaemonSets (oldDS .Namespace ).Create (context .Background (), oldDS , metav1.CreateOptions {})
508
539
if err != nil {
509
540
t .Fatal (err )
510
541
}
511
542
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" )
520
545
ok = dsc .processNextWorkItem ()
521
546
if ! ok {
522
547
t .Fatal ("queue is shutting down" )
@@ -538,28 +563,28 @@ func TestExpectationsOnRecreate(t *testing.T) {
538
563
t .Fatal (err )
539
564
}
540
565
if ! exists {
541
- t .Errorf ("No expectations found for DaemonSet %q" , oldDSKey )
566
+ t .Fatalf ("No expectations found for DaemonSet %q" , oldDSKey )
542
567
}
543
568
if dsExp .Fulfilled () {
544
569
t .Errorf ("There should be unfulfiled expectation for creating new pods for DaemonSet %q" , oldDSKey )
545
570
}
546
571
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" )
549
577
}
550
578
579
+ // process does not re-update the DS
580
+ expectStableQueueLength (0 )
581
+
551
582
err = client .AppsV1 ().DaemonSets (oldDS .Namespace ).Delete (context .Background (), oldDS .Name , metav1.DeleteOptions {})
552
583
if err != nil {
553
584
t .Fatal (err )
554
585
}
555
586
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" )
563
588
564
589
_ , exists , err = dsc .expectations .GetExpectations (oldDSKey )
565
590
if err != nil {
@@ -579,9 +604,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
579
604
t .Fatal ("Keys should be equal!" )
580
605
}
581
606
582
- if dsc .queue .Len () != 0 {
583
- t .Fatal ("Unexpected item in the queue" )
584
- }
607
+ expectStableQueueLength (0 )
585
608
586
609
newDS := oldDS .DeepCopy ()
587
610
newDS .UID = uuid .NewUUID ()
@@ -595,14 +618,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
595
618
t .Fatal ("New DS has the same UID as the old one!" )
596
619
}
597
620
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" )
606
622
ok = dsc .processNextWorkItem ()
607
623
if ! ok {
608
624
t .Fatal ("Queue is shutting down!" )
@@ -617,7 +633,7 @@ func TestExpectationsOnRecreate(t *testing.T) {
617
633
t .Fatal (err )
618
634
}
619
635
if ! exists {
620
- t .Errorf ("No expectations found for DaemonSet %q" , oldDSKey )
636
+ t .Fatalf ("No expectations found for DaemonSet %q" , oldDSKey )
621
637
}
622
638
if dsExp .Fulfilled () {
623
639
t .Errorf ("There should be unfulfiled expectation for creating new pods for DaemonSet %q" , oldDSKey )
0 commit comments