github.com/xmplusdev/xray-core@v1.8.10/common/task/periodic_test.go (about)

     1  package task_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/xmplusdev/xray-core/common"
     8  	. "github.com/xmplusdev/xray-core/common/task"
     9  )
    10  
    11  func TestPeriodicTaskStop(t *testing.T) {
    12  	value := 0
    13  	task := &Periodic{
    14  		Interval: time.Second * 2,
    15  		Execute: func() error {
    16  			value++
    17  			return nil
    18  		},
    19  	}
    20  	common.Must(task.Start())
    21  	time.Sleep(time.Second * 5)
    22  	common.Must(task.Close())
    23  	if value != 3 {
    24  		t.Fatal("expected 3, but got ", value)
    25  	}
    26  	time.Sleep(time.Second * 4)
    27  	if value != 3 {
    28  		t.Fatal("expected 3, but got ", value)
    29  	}
    30  	common.Must(task.Start())
    31  	time.Sleep(time.Second * 3)
    32  	if value != 5 {
    33  		t.Fatal("Expected 5, but ", value)
    34  	}
    35  	common.Must(task.Close())
    36  }