diff --git a/common/signal/exec_test.go b/common/signal/exec_test.go index d6bac83b5..78116e32f 100644 --- a/common/signal/exec_test.go +++ b/common/signal/exec_test.go @@ -4,85 +4,40 @@ import ( "context" "errors" "testing" + "time" . "v2ray.com/core/common/signal" . "v2ray.com/ext/assert" ) -func TestErrorOrFinish2_Error(t *testing.T) { +func TestExecuteParallel(t *testing.T) { assert := With(t) - c1 := make(chan error, 1) - c2 := make(chan error, 2) - c := make(chan error, 1) + 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") + }) - go func() { - c <- ErrorOrFinish2(context.Background(), c1, c2) - }() - - c1 <- errors.New("test") - err := <-c assert(err.Error(), Equals, "test") } -func TestErrorOrFinish2_Error2(t *testing.T) { +func TestExecuteParallelContextCancel(t *testing.T) { assert := With(t) - c1 := make(chan error, 1) - c2 := make(chan error, 2) - c := make(chan error, 1) + 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 + }) - go func() { - c <- ErrorOrFinish2(context.Background(), c1, c2) - }() - - c2 <- errors.New("test") - err := <-c - assert(err.Error(), Equals, "test") -} - -func TestErrorOrFinish2_NoneError(t *testing.T) { - assert := With(t) - - c1 := make(chan error, 1) - c2 := make(chan error, 2) - c := make(chan error, 1) - - go func() { - c <- ErrorOrFinish2(context.Background(), c1, c2) - }() - - close(c1) - select { - case <-c: - t.Fail() - default: - } - - close(c2) - err := <-c - assert(err, IsNil) -} - -func TestErrorOrFinish2_NoneError2(t *testing.T) { - assert := With(t) - - c1 := make(chan error, 1) - c2 := make(chan error, 2) - c := make(chan error, 1) - - go func() { - c <- ErrorOrFinish2(context.Background(), c1, c2) - }() - - close(c2) - select { - case <-c: - t.Fail() - default: - } - - close(c1) - err := <-c - assert(err, IsNil) + assert(err.Error(), HasSubstring, "canceled") }