2016-12-29 18:32:20 -05:00
|
|
|
package signal_test
|
|
|
|
|
|
|
|
import (
|
2017-01-28 15:24:46 -05:00
|
|
|
"context"
|
2016-12-29 18:32:20 -05:00
|
|
|
"errors"
|
|
|
|
"testing"
|
2018-04-11 10:52:51 -04:00
|
|
|
"time"
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
. "v2ray.com/core/common/signal"
|
2017-10-24 10:15:35 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-12-29 18:32:20 -05:00
|
|
|
)
|
|
|
|
|
2018-04-11 10:52:51 -04:00
|
|
|
func TestExecuteParallel(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2018-04-11 10:52:51 -04:00
|
|
|
err := ExecuteParallel(context.Background(), func() error {
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
return errors.New("test")
|
|
|
|
}, func() error {
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
return errors.New("test2")
|
|
|
|
})
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err.Error(), Equals, "test")
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
|
|
|
|
2018-04-11 10:52:51 -04:00
|
|
|
func TestExecuteParallelContextCancel(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
2018-04-11 10:52:51 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
err := ExecuteParallel(ctx, func() error {
|
|
|
|
time.Sleep(time.Millisecond * 2000)
|
|
|
|
return errors.New("test")
|
|
|
|
}, func() error {
|
|
|
|
time.Sleep(time.Millisecond * 5000)
|
|
|
|
return errors.New("test2")
|
|
|
|
}, func() error {
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
assert(err.Error(), HasSubstring, "canceled")
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|