2016-12-29 18:32:20 -05:00
|
|
|
package signal_test
|
|
|
|
|
|
|
|
import (
|
2017-01-28 15:24:46 -05:00
|
|
|
"context"
|
2016-12-29 18:32:20 -05:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "v2ray.com/core/common/signal"
|
2017-10-24 10:15:35 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-12-29 18:32:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestErrorOrFinish2_Error(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
c2 := make(chan error, 2)
|
|
|
|
c := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2017-01-28 15:24:46 -05:00
|
|
|
c <- ErrorOrFinish2(context.Background(), c1, c2)
|
2016-12-29 18:32:20 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
c1 <- errors.New("test")
|
|
|
|
err := <-c
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err.Error(), Equals, "test")
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorOrFinish2_Error2(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
c2 := make(chan error, 2)
|
|
|
|
c := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2017-01-28 15:24:46 -05:00
|
|
|
c <- ErrorOrFinish2(context.Background(), c1, c2)
|
2016-12-29 18:32:20 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
c2 <- errors.New("test")
|
|
|
|
err := <-c
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err.Error(), Equals, "test")
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorOrFinish2_NoneError(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
c2 := make(chan error, 2)
|
|
|
|
c := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2017-01-28 15:24:46 -05:00
|
|
|
c <- ErrorOrFinish2(context.Background(), c1, c2)
|
2016-12-29 18:32:20 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
close(c1)
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
t.Fail()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
close(c2)
|
|
|
|
err := <-c
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrorOrFinish2_NoneError2(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
c1 := make(chan error, 1)
|
|
|
|
c2 := make(chan error, 2)
|
|
|
|
c := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2017-01-28 15:24:46 -05:00
|
|
|
c <- ErrorOrFinish2(context.Background(), c1, c2)
|
2016-12-29 18:32:20 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
close(c2)
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
t.Fail()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
close(c1)
|
|
|
|
err := <-c
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|