Skip to content

Commit d16104a

Browse files
committed
adding minimum execution check
1 parent 9487524 commit d16104a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

tasks_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,19 +758,29 @@ func TestSingleInstance(t *testing.T) {
758758
// Create a counter to track how many times the task is called
759759
counter := NewCounter()
760760

761+
// Create a second counter to track number of executions
762+
counter2 := NewCounter()
763+
761764
// Create an error channel to signal failure
762765
errCh := make(chan error)
763766

764767
// Add a task that will increment the counter
765768
_, err := scheduler.Add(&Task{
766-
Interval: time.Duration(1 * time.Second),
769+
Interval: time.Duration(500 * time.Millisecond),
767770
RunSingleInstance: true,
768771
TaskFunc: func() error {
772+
// Increment Concurrent Counter
769773
counter.Inc()
770774
if counter.Val() > 1 {
771775
return fmt.Errorf("Task ran more than once - count %d", counter.Val())
772776
}
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
774784
counter.Dec()
775785
return nil
776786
},
@@ -785,7 +795,9 @@ func TestSingleInstance(t *testing.T) {
785795
// Wait for tasks to run and if no error, then we are good
786796
select {
787797
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+
}
789801
case e := <-errCh:
790802
t.Fatalf("Error function was called - %s", e)
791803
}

0 commit comments

Comments
 (0)