pull/1028/merge
Darien Raymond 7 years ago
parent 0caf92726b
commit 666a1a17f2
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -4,85 +4,40 @@ import (
"context" "context"
"errors" "errors"
"testing" "testing"
"time"
. "v2ray.com/core/common/signal" . "v2ray.com/core/common/signal"
. "v2ray.com/ext/assert" . "v2ray.com/ext/assert"
) )
func TestErrorOrFinish2_Error(t *testing.T) { func TestExecuteParallel(t *testing.T) {
assert := With(t) assert := With(t)
c1 := make(chan error, 1) err := ExecuteParallel(context.Background(), func() error {
c2 := make(chan error, 2) time.Sleep(time.Millisecond * 200)
c := make(chan error, 1) 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") 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)
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) assert := With(t)
c1 := make(chan error, 1) ctx, cancel := context.WithCancel(context.Background())
c2 := make(chan error, 2) err := ExecuteParallel(ctx, func() error {
c := make(chan error, 1) time.Sleep(time.Millisecond * 2000)
return errors.New("test")
go func() { }, func() error {
c <- ErrorOrFinish2(context.Background(), c1, c2) time.Sleep(time.Millisecond * 5000)
}() return errors.New("test2")
}, func() error {
close(c2) cancel()
select { return nil
case <-c: })
t.Fail()
default: assert(err.Error(), HasSubstring, "canceled")
}
close(c1)
err := <-c
assert(err, IsNil)
} }

Loading…
Cancel
Save