mirror of https://github.com/v2ray/v2ray-core
fix test
parent
0caf92726b
commit
666a1a17f2
|
@ -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)
|
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")
|
||||||
|
}, func() error {
|
||||||
|
time.Sleep(time.Millisecond * 5000)
|
||||||
|
return errors.New("test2")
|
||||||
|
}, func() error {
|
||||||
|
cancel()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
go func() {
|
assert(err.Error(), HasSubstring, "canceled")
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue