@@ -758,19 +758,29 @@ func TestSingleInstance(t *testing.T) {
758
758
// Create a counter to track how many times the task is called
759
759
counter := NewCounter ()
760
760
761
+ // Create a second counter to track number of executions
762
+ counter2 := NewCounter ()
763
+
761
764
// Create an error channel to signal failure
762
765
errCh := make (chan error )
763
766
764
767
// Add a task that will increment the counter
765
768
_ , err := scheduler .Add (& Task {
766
- Interval : time .Duration (1 * time .Second ),
769
+ Interval : time .Duration (500 * time .Millisecond ),
767
770
RunSingleInstance : true ,
768
771
TaskFunc : func () error {
772
+ // Increment Concurrent Counter
769
773
counter .Inc ()
770
774
if counter .Val () > 1 {
771
775
return fmt .Errorf ("Task ran more than once - count %d" , counter .Val ())
772
776
}
773
- <- time .After (10 * time .Second )
777
+ // Increment Execution Counter
778
+ counter2 .Inc ()
779
+
780
+ // Wait for 10 seconds
781
+ <- time .After (5 * time .Second )
782
+
783
+ // Decrement Concurrent Counter
774
784
counter .Dec ()
775
785
return nil
776
786
},
@@ -785,7 +795,9 @@ func TestSingleInstance(t *testing.T) {
785
795
// Wait for tasks to run and if no error, then we are good
786
796
select {
787
797
case <- time .After (30 * time .Second ):
788
- return
798
+ if counter2 .Val () < 4 {
799
+ t .Fatalf ("Task was not called more than once successfully - count %d" , counter2 .Val ())
800
+ }
789
801
case e := <- errCh :
790
802
t .Fatalf ("Error function was called - %s" , e )
791
803
}
0 commit comments