github.com/koko1123/flow-go-1@v0.29.6/engine/unit_test.go (about)

     1  package engine_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/engine"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  )
    13  
    14  func TestReadyDone(t *testing.T) {
    15  	u := engine.NewUnit()
    16  	<-u.Ready()
    17  	<-u.Done()
    18  }
    19  
    20  // Test that if a function is run by LaunchPeriodically and
    21  // takes longer than the interval, the next call will be blocked
    22  func TestLaunchPeriod(t *testing.T) {
    23  	lock := sync.Mutex{}
    24  
    25  	u := engine.NewUnit()
    26  	unittest.RequireCloseBefore(t, u.Ready(), time.Second, "ready did not close")
    27  	logs := make([]string, 0)
    28  	u.LaunchPeriodically(func() {
    29  		lock.Lock()
    30  		logs = append(logs, "running")
    31  		lock.Unlock()
    32  
    33  		time.Sleep(100 * time.Millisecond)
    34  
    35  		lock.Lock()
    36  		logs = append(logs, "finish")
    37  		lock.Unlock()
    38  	}, 50*time.Millisecond, 0)
    39  
    40  	// 100 * 3 is to ensure enough time for 3 periodic function to finish
    41  	// adding another 30 as buffer to tolerate, and the buffer has to be
    42  	// smaller than 50 in order to avoid another trigger of the periodic function
    43  	<-time.After((100*3 + 30) * time.Millisecond)
    44  
    45  	// This can pass
    46  	lock.Lock()
    47  	require.Equal(t, []string{
    48  		"running", "finish",
    49  		"running", "finish",
    50  		"running",
    51  	}, logs)
    52  	lock.Unlock()
    53  	unittest.RequireCloseBefore(t, u.Done(), time.Second, "done did not close")
    54  
    55  	require.Equal(t, []string{
    56  		"running", "finish",
    57  		"running", "finish",
    58  		"running", "finish",
    59  	}, logs)
    60  }
    61  
    62  func TestLaunchPeriod_Delay(t *testing.T) {
    63  	u := engine.NewUnit()
    64  	unittest.RequireCloseBefore(t, u.Ready(), time.Second, "ready did not close")
    65  
    66  	// launch f with a large initial delay (30s)
    67  	// the function f should never be invoked, so we use t.Fail
    68  	u.LaunchPeriodically(t.Fail, time.Millisecond, time.Second*30)
    69  
    70  	// ensure we can stop the unit quickly (we should not need to wait for initial delay)
    71  	unittest.RequireCloseBefore(t, u.Done(), time.Second, "done did not close")
    72  }