@@ -748,27 +748,25 @@ func TestWaitForWatcher(t *testing.T) {
748
748
wantErrWatcherNotStarted := func (t assert.TestingT , err error , i ... interface {}) bool {
749
749
return assert .ErrorIs (t , err , ErrWatcherNotStarted , i )
750
750
}
751
+
751
752
tests := []struct {
752
- name string
753
- states []details.State
754
- stateChangeInterval time.Duration
755
- timeout time.Duration
756
- expirationAfterLastState time.Duration
757
- wantErr assert.ErrorAssertionFunc
753
+ name string
754
+ states []details.State
755
+ stateChangeInterval time.Duration
756
+ cancelWaitContext bool
757
+ wantErr assert.ErrorAssertionFunc
758
758
}{
759
759
{
760
- name : "Happy path: watcher is watching already" ,
761
- states : []details.State {details .StateWatching },
762
- stateChangeInterval : 1 * time .Millisecond ,
763
- timeout : 500 * time .Millisecond ,
764
- expirationAfterLastState : 100 * time .Millisecond ,
765
- wantErr : assert .NoError ,
760
+ name : "Happy path: watcher is watching already" ,
761
+ states : []details.State {details .StateWatching },
762
+ stateChangeInterval : 1 * time .Millisecond ,
763
+ wantErr : assert .NoError ,
766
764
},
767
765
{
768
766
name : "Sad path: watcher is never starting" ,
769
767
states : []details.State {details .StateReplacing },
770
768
stateChangeInterval : 1 * time .Millisecond ,
771
- timeout : 50 * time . Millisecond ,
769
+ cancelWaitContext : true ,
772
770
wantErr : wantErrWatcherNotStarted ,
773
771
},
774
772
{
@@ -782,16 +780,14 @@ func TestWaitForWatcher(t *testing.T) {
782
780
details .StateRestarting ,
783
781
details .StateWatching ,
784
782
},
785
- stateChangeInterval : 1 * time .Millisecond ,
786
- timeout : 500 * time .Millisecond ,
787
- expirationAfterLastState : 10 * time .Millisecond ,
788
- wantErr : assert .NoError ,
783
+ stateChangeInterval : 1 * time .Millisecond ,
784
+ wantErr : assert .NoError ,
789
785
},
790
786
{
791
787
name : "Timeout: marker is never created" ,
792
788
states : nil ,
793
789
stateChangeInterval : 1 * time .Millisecond ,
794
- timeout : 50 * time . Millisecond ,
790
+ cancelWaitContext : true ,
795
791
wantErr : wantErrWatcherNotStarted ,
796
792
},
797
793
{
@@ -805,8 +801,8 @@ func TestWaitForWatcher(t *testing.T) {
805
801
details .StateRestarting ,
806
802
},
807
803
808
- stateChangeInterval : 5 * time .Millisecond ,
809
- timeout : 20 * time . Millisecond ,
804
+ stateChangeInterval : 1 * time .Millisecond ,
805
+ cancelWaitContext : true ,
810
806
wantErr : wantErrWatcherNotStarted ,
811
807
},
812
808
}
@@ -817,18 +813,22 @@ func TestWaitForWatcher(t *testing.T) {
817
813
if ! ok {
818
814
deadline = time .Now ().Add (5 * time .Second )
819
815
}
820
- testCtx , cancel := context .WithDeadline (context .TODO (), deadline )
821
- defer cancel ()
816
+ testCtx , testCancel := context .WithDeadline (context .Background (), deadline )
817
+ defer testCancel ()
822
818
823
819
tmpDir := t .TempDir ()
824
820
updMarkerFilePath := filepath .Join (tmpDir , markerFilename )
825
821
826
822
waitContext , waitCancel := context .WithCancel (testCtx )
823
+ defer waitCancel ()
824
+
825
+ fakeTimeout := 30 * time .Second
826
+
827
827
// in order to take timing out of the equation provide a context that we can cancel manually
828
828
// still assert that the parent context and timeout passed are correct
829
829
var createContextFunc createContextWithTimeout = func (ctx context.Context , timeout time.Duration ) (context.Context , context.CancelFunc ) {
830
830
assert .Same (t , testCtx , ctx , "parent context should be the same as the waitForWatcherCall" )
831
- assert .Equal (t , tt . timeout , timeout , "timeout used in new context should be the same as testcase" )
831
+ assert .Equal (t , fakeTimeout , timeout , "timeout used in new context should be the same as testcase" )
832
832
833
833
return waitContext , waitCancel
834
834
}
@@ -848,6 +848,8 @@ func TestWaitForWatcher(t *testing.T) {
848
848
849
849
wg .Add (1 )
850
850
851
+ // worker goroutine: writes out additional states while the test is blocked on waitOnWatcher() call and expires
852
+ // the wait context if cancelWaitContext is set to true. Timing of the goroutine is driven by stateChangeInterval.
851
853
go func () {
852
854
defer wg .Done ()
853
855
tick := time .NewTicker (tt .stateChangeInterval )
@@ -860,16 +862,15 @@ func TestWaitForWatcher(t *testing.T) {
860
862
writeState (t , updMarkerFilePath , state )
861
863
}
862
864
}
863
- <- time .After (tt .expirationAfterLastState )
864
- waitCancel ()
865
+ if tt .cancelWaitContext {
866
+ <- tick .C
867
+ waitCancel ()
868
+ }
865
869
}()
866
870
867
871
log , _ := logger .NewTesting (tt .name )
868
872
869
- tt .wantErr (t , waitForWatcherWithTimeoutCreationFunc (testCtx , log , updMarkerFilePath , tt .timeout , createContextFunc ), fmt .Sprintf ("waitForWatcher %s, %v, %s, %s)" , updMarkerFilePath , tt .states , tt .stateChangeInterval , tt .timeout ))
870
-
871
- // cancel context
872
- cancel ()
873
+ tt .wantErr (t , waitForWatcherWithTimeoutCreationFunc (testCtx , log , updMarkerFilePath , fakeTimeout , createContextFunc ), fmt .Sprintf ("waitForWatcher %s, %v, %s, %s)" , updMarkerFilePath , tt .states , tt .stateChangeInterval , fakeTimeout ))
873
874
874
875
// wait for goroutines to finish
875
876
wg .Wait ()
0 commit comments