From 666a1a17f21b6fff23f966026fec3c18bc4922b2 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 11 Apr 2018 16:52:51 +0200 Subject: [PATCH] fix test --- common/signal/exec_test.go | 89 ++++++++++---------------------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/common/signal/exec_test.go b/common/signal/exec_test.go index d6bac83b..78116e32 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") }