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"
"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) {
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) {
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)
}()
close(c2)
select {
case <-c:
t.Fail()
default:
}
close(c1)
err := <-c
assert(err, IsNil)
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")
}

Loading…
Cancel
Save