github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/routine/delaytask_test.go (about) 1 // Copyright (c) 2018 IoTeX 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package routine_test 7 8 import ( 9 "context" 10 "testing" 11 "time" 12 13 "github.com/facebookgo/clock" 14 "github.com/stretchr/testify/assert" 15 16 "github.com/iotexproject/iotex-core/pkg/routine" 17 ) 18 19 func TestDelayTaskTimeout(t *testing.T) { 20 c := make(chan bool) 21 ctx := context.Background() 22 ck := clock.NewMock() 23 task := routine.NewDelayTask(func() { c <- true }, 100*time.Millisecond, routine.WithClock(ck)) 24 task.Start(ctx) 25 defer func() { 26 task.Stop(ctx) 27 }() 28 29 ck.Add(1 * time.Second) 30 assert.True(t, <-c, "Do executed") 31 } 32 33 func TestDelayTaskStop(t *testing.T) { 34 c := make(chan bool) 35 ctx := context.Background() 36 task := routine.NewDelayTask(func() { c <- true }, 100*time.Millisecond) 37 task.Start(ctx) 38 task.Stop(ctx) 39 40 select { 41 case <-c: 42 t.Fail() 43 case <-time.After(600 * time.Millisecond): 44 } 45 }