2016-12-29 23:32:20 +00:00
|
|
|
package signal_test
|
|
|
|
|
|
|
|
import (
|
2017-01-28 20:24:46 +00:00
|
|
|
"context"
|
2016-12-29 23:32:20 +00:00
|
|
|
"errors"
|
|
|
|
"testing"
|
2018-04-11 14:52:51 +00:00
|
|
|
"time"
|
2016-12-29 23:32:20 +00:00
|
|
|
|
|
|
|
. "v2ray.com/core/common/signal"
|
2017-10-24 14:15:35 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-12-29 23:32:20 +00:00
|
|
|
)
|
|
|
|
|
2018-04-11 14:52:51 +00:00
|
|
|
func TestExecuteParallel(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-12-29 23:32:20 +00:00
|
|
|
|
2018-04-11 14:52:51 +00: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 23:32:20 +00:00
|
|
|
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err.Error(), Equals, "test")
|
2016-12-29 23:32:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 14:52:51 +00:00
|
|
|
func TestExecuteParallelContextCancel(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-12-29 23:32:20 +00:00
|
|
|
|
2018-04-11 14:52:51 +00: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 23:32:20 +00:00
|
|
|
}
|