github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/executor/executor_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPeriodic(t *testing.T) {
    12  	/*
    13  		This test is very simple - it has hardcoded time.Sleep. Running it at machine with high load may cause the test fail.
    14  		The best way is to modify executor_test (and adapt executor) to not rely on time.Sleep.
    15  	*/
    16  
    17  	// GIVEN
    18  	called := 0
    19  	worker := func(ctx context.Context) {
    20  		called = called + 1
    21  	}
    22  	ctx, cancel := context.WithCancel(context.TODO())
    23  	svc := NewPeriodic(50*time.Millisecond, worker)
    24  
    25  	// WHEN
    26  	svc.Run(ctx)
    27  	time.Sleep(120 * time.Millisecond)
    28  	cancel()
    29  
    30  	// THEN
    31  	// expecting 3 calls, first at after 0ms, second - after 10ms, third after 20ms
    32  	assert.Equal(t, 3, called)
    33  }