2018-05-27 07:02:29 -04:00
|
|
|
package task_test
|
2018-02-08 10:19:43 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-05-27 07:02:29 -04:00
|
|
|
"v2ray.com/core/common"
|
2019-01-31 14:57:01 -05:00
|
|
|
. "v2ray.com/core/common/task"
|
2018-02-08 10:19:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPeriodicTaskStop(t *testing.T) {
|
|
|
|
value := 0
|
2018-05-27 07:02:29 -04:00
|
|
|
task := &Periodic{
|
2018-02-08 10:19:43 -05:00
|
|
|
Interval: time.Second * 2,
|
|
|
|
Execute: func() error {
|
|
|
|
value++
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
common.Must(task.Start())
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
common.Must(task.Close())
|
2019-01-31 14:57:01 -05:00
|
|
|
if value != 3 {
|
|
|
|
t.Fatal("expected 3, but got ", value)
|
|
|
|
}
|
2018-02-08 10:19:43 -05:00
|
|
|
time.Sleep(time.Second * 4)
|
2019-01-31 14:57:01 -05:00
|
|
|
if value != 3 {
|
|
|
|
t.Fatal("expected 3, but got ", value)
|
|
|
|
}
|
2018-08-29 17:00:01 -04:00
|
|
|
common.Must(task.Start())
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
if value != 5 {
|
|
|
|
t.Fatal("Expected 5, but ", value)
|
|
|
|
}
|
|
|
|
common.Must(task.Close())
|
2018-02-08 10:19:43 -05:00
|
|
|
}
|