@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"math/rand"
23
+ "sync"
23
24
"testing"
24
25
"time"
25
26
@@ -174,7 +175,7 @@ func TestReconcileElectionStep(t *testing.T) {
174
175
LeaseName : "component-A" ,
175
176
EmulationVersion : "1.20.0" ,
176
177
BinaryVersion : "1.20.0" ,
177
- PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now (). Add ( - 1 * time . Millisecond ) )),
178
+ PingTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
178
179
RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
179
180
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
180
181
},
@@ -439,8 +440,6 @@ func TestReconcileElectionStep(t *testing.T) {
439
440
}
440
441
441
442
func TestController (t * testing.T ) {
442
- fakeClock := testingclock .NewFakeClock (time .Now ())
443
-
444
443
cases := []struct {
445
444
name string
446
445
leases []* v1.Lease
@@ -461,7 +460,7 @@ func TestController(t *testing.T) {
461
460
LeaseName : "component-A" ,
462
461
EmulationVersion : "1.19.0" ,
463
462
BinaryVersion : "1.19.0" ,
464
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
463
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
465
464
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
466
465
},
467
466
},
@@ -490,7 +489,7 @@ func TestController(t *testing.T) {
490
489
LeaseName : "component-A" ,
491
490
EmulationVersion : "1.19.0" ,
492
491
BinaryVersion : "1.19.0" ,
493
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
492
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
494
493
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
495
494
},
496
495
},
@@ -503,7 +502,7 @@ func TestController(t *testing.T) {
503
502
LeaseName : "component-A" ,
504
503
EmulationVersion : "1.19.0" ,
505
504
BinaryVersion : "1.20.0" ,
506
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
505
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
507
506
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
508
507
},
509
508
},
@@ -516,7 +515,7 @@ func TestController(t *testing.T) {
516
515
LeaseName : "component-A" ,
517
516
EmulationVersion : "1.20.0" ,
518
517
BinaryVersion : "1.20.0" ,
519
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
518
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
520
519
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
521
520
},
522
521
},
@@ -543,7 +542,7 @@ func TestController(t *testing.T) {
543
542
},
544
543
Spec : v1.LeaseSpec {
545
544
HolderIdentity : ptr .To ("some-other-component" ),
546
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (time .Second ))),
545
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (time .Second ))),
547
546
LeaseDurationSeconds : ptr .To (int32 (10 )),
548
547
},
549
548
},
@@ -558,7 +557,7 @@ func TestController(t *testing.T) {
558
557
LeaseName : "component-A" ,
559
558
EmulationVersion : "1.19.0" ,
560
559
BinaryVersion : "1.19.0" ,
561
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
560
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
562
561
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
563
562
},
564
563
},
@@ -588,7 +587,7 @@ func TestController(t *testing.T) {
588
587
},
589
588
Spec : v1.LeaseSpec {
590
589
HolderIdentity : ptr .To ("some-other-component" ),
591
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (- 11 * time .Second ))),
590
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (- 11 * time .Second ))),
592
591
LeaseDurationSeconds : ptr .To (int32 (10 )),
593
592
Strategy : ptr.To [v1.CoordinatedLeaseStrategy ]("OldestEmulationVersion" ),
594
593
},
@@ -604,7 +603,7 @@ func TestController(t *testing.T) {
604
603
LeaseName : "component-A" ,
605
604
EmulationVersion : "1.19.0" ,
606
605
BinaryVersion : "1.19.0" ,
607
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
606
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
608
607
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
609
608
},
610
609
},
@@ -632,7 +631,7 @@ func TestController(t *testing.T) {
632
631
Spec : v1.LeaseSpec {
633
632
HolderIdentity : ptr .To ("component-identity-1" ),
634
633
Strategy : ptr.To [v1.CoordinatedLeaseStrategy ]("OldestEmulationVersion" ),
635
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ().Add (time .Second ))),
634
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ().Add (time .Second ))),
636
635
LeaseDurationSeconds : ptr .To (int32 (10 )),
637
636
},
638
637
},
@@ -647,7 +646,7 @@ func TestController(t *testing.T) {
647
646
LeaseName : "component-A" ,
648
647
EmulationVersion : "1.20.0" ,
649
648
BinaryVersion : "1.20.0" ,
650
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
649
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
651
650
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
652
651
},
653
652
},
@@ -662,7 +661,7 @@ func TestController(t *testing.T) {
662
661
LeaseName : "component-A" ,
663
662
EmulationVersion : "1.19.0" ,
664
663
BinaryVersion : "1.19.0" ,
665
- RenewTime : ptr .To (metav1 .NewMicroTime (fakeClock .Now ())),
664
+ RenewTime : ptr .To (metav1 .NewMicroTime (time .Now ())),
666
665
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
667
666
},
668
667
},
@@ -683,6 +682,10 @@ func TestController(t *testing.T) {
683
682
684
683
for _ , tc := range cases {
685
684
t .Run (tc .name , func (t * testing.T ) {
685
+ // collect go routines using t.logf
686
+ var wg sync.WaitGroup
687
+ defer wg .Wait ()
688
+
686
689
ctx , cancel := context .WithTimeout (context .Background (), time .Minute )
687
690
defer cancel ()
688
691
@@ -722,7 +725,9 @@ func TestController(t *testing.T) {
722
725
time .Sleep (time .Second )
723
726
}
724
727
728
+ wg .Add (1 )
725
729
go func () {
730
+ defer wg .Done ()
726
731
ticker := time .NewTicker (10 * time .Millisecond )
727
732
// Mock out the removal of preferredHolder leases.
728
733
// When controllers are running, they are expected to do this voluntarily
@@ -756,7 +761,9 @@ func TestController(t *testing.T) {
756
761
}
757
762
}()
758
763
764
+ wg .Add (1 )
759
765
go func () {
766
+ defer wg .Done ()
760
767
ticker := time .NewTicker (10 * time .Millisecond )
761
768
// Mock out leasecandidate ack.
762
769
// When controllers are running, they are expected to watch and ack
@@ -765,16 +772,18 @@ func TestController(t *testing.T) {
765
772
case <- ctx .Done ():
766
773
return
767
774
case <- ticker .C :
768
- for _ , lc := range tc .createAfterControllerStart {
769
- c , err := client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Get (ctx , lc .Name , metav1.GetOptions {})
770
- if err == nil {
771
- if c .Spec .PingTime != nil {
772
- t .Logf ("Answering ping for %s/%s" , c .Namespace , c .Name )
773
- c .Spec .RenewTime = & metav1.MicroTime {Time : fakeClock .Now ()}
774
- _ , err = client .CoordinationV1alpha1 ().LeaseCandidates (lc .Namespace ).Update (ctx , c , metav1.UpdateOptions {})
775
- if err != nil {
776
- t .Logf ("Error updating lease candidate %s/%s: %v" , c .Namespace , c .Name , err )
777
- }
775
+ cs , err := client .CoordinationV1alpha1 ().LeaseCandidates ("" ).List (ctx , metav1.ListOptions {})
776
+ if err != nil {
777
+ t .Logf ("Error listing lease candidates: %v" , err )
778
+ continue
779
+ }
780
+ for _ , c := range cs .Items {
781
+ if c .Spec .PingTime != nil && (c .Spec .RenewTime == nil || c .Spec .PingTime .Time .After (c .Spec .RenewTime .Time )) {
782
+ t .Logf ("Answering ping for %s/%s" , c .Namespace , c .Name )
783
+ c .Spec .RenewTime = & metav1.MicroTime {Time : time .Now ()}
784
+ _ , err = client .CoordinationV1alpha1 ().LeaseCandidates (c .Namespace ).Update (ctx , & c , metav1.UpdateOptions {})
785
+ if err != nil {
786
+ t .Logf ("Error updating lease candidate %s/%s: %v" , c .Namespace , c .Name , err )
778
787
}
779
788
}
780
789
}
@@ -815,10 +824,7 @@ func TestController(t *testing.T) {
815
824
if lease .Spec .HolderIdentity == nil {
816
825
return false , nil
817
826
}
818
- if * expectedLease .Spec .HolderIdentity != * lease .Spec .HolderIdentity {
819
- return false , nil
820
- }
821
- return true , nil
827
+ return * expectedLease .Spec .HolderIdentity == * lease .Spec .HolderIdentity , nil
822
828
})
823
829
if err != nil {
824
830
if lease == nil {
0 commit comments