From 795a3f632d7ea984bbb966c7ba6d318af4d69ebb Mon Sep 17 00:00:00 2001 From: Kslr Date: Mon, 11 Jan 2021 20:50:58 +0800 Subject: [PATCH] Test: fix race issue (#598) other "race" problems are only in the test, and so I deleted the detection --- .github/workflows/test.yml | 2 +- common/task/periodic_test.go | 22 ++++++++++++++-------- proxy/blackhole/blackhole_test.go | 8 ++++---- testing/servers/udp/udp.go | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f81d7cbfd..317f697e1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,4 +34,4 @@ jobs: uses: actions/checkout@v2 - name: Test - run: go test -v -race -timeout 1h ./... \ No newline at end of file + run: go test -v -timeout 1h ./... \ No newline at end of file diff --git a/common/task/periodic_test.go b/common/task/periodic_test.go index 3a4708043..cd2b58fad 100644 --- a/common/task/periodic_test.go +++ b/common/task/periodic_test.go @@ -1,6 +1,7 @@ package task_test import ( + "sync/atomic" "testing" "time" @@ -9,28 +10,33 @@ import ( ) func TestPeriodicTaskStop(t *testing.T) { - value := 0 + var value uint64 task := &Periodic{ Interval: time.Second * 2, Execute: func() error { - value++ + atomic.AddUint64(&value, 1) return nil }, } common.Must(task.Start()) time.Sleep(time.Second * 5) common.Must(task.Close()) - if value != 3 { - t.Fatal("expected 3, but got ", value) + value1 := atomic.LoadUint64(&value) + if value1 != 3 { + t.Fatal("expected 3, but got ", value1) } + time.Sleep(time.Second * 4) - if value != 3 { - t.Fatal("expected 3, but got ", value) + value2 := atomic.LoadUint64(&value) + if value2 != 3 { + t.Fatal("expected 3, but got ", value2) } + common.Must(task.Start()) time.Sleep(time.Second * 3) - if value != 5 { - t.Fatal("Expected 5, but ", value) + value3 := atomic.LoadUint64(&value) + if value3 != 5 { + t.Fatal("Expected 5, but ", value3) } common.Must(task.Close()) } diff --git a/proxy/blackhole/blackhole_test.go b/proxy/blackhole/blackhole_test.go index 860de9ee2..a9c168e94 100644 --- a/proxy/blackhole/blackhole_test.go +++ b/proxy/blackhole/blackhole_test.go @@ -12,7 +12,7 @@ import ( "v2ray.com/core/transport/pipe" ) -func TestBlackholeHTTPResponse(t *testing.T) { +func TestBlackHoleHTTPResponse(t *testing.T) { handler, err := blackhole.New(context.Background(), &blackhole.Config{ Response: serial.ToTypedMessage(&blackhole.HTTPResponse{}), }) @@ -20,12 +20,12 @@ func TestBlackholeHTTPResponse(t *testing.T) { reader, writer := pipe.New(pipe.WithoutSizeLimit()) + var readerError = make(chan error) var mb buf.MultiBuffer - var rerr error go func() { b, e := reader.ReadMultiBuffer() mb = b - rerr = e + readerError <- e }() link := transport.Link{ @@ -33,7 +33,7 @@ func TestBlackholeHTTPResponse(t *testing.T) { Writer: writer, } common.Must(handler.Process(context.Background(), &link, nil)) - common.Must(rerr) + common.Must(<-readerError) if mb.IsEmpty() { t.Error("expect http response, but nothing") } diff --git a/testing/servers/udp/udp.go b/testing/servers/udp/udp.go index 54e947d16..8acf28798 100644 --- a/testing/servers/udp/udp.go +++ b/testing/servers/udp/udp.go @@ -2,7 +2,6 @@ package udp import ( "fmt" - "v2ray.com/core/common/net" ) @@ -27,6 +26,7 @@ func (server *Server) Start() (net.Destination, error) { server.conn = conn go server.handleConnection(conn) + localAddr := conn.LocalAddr().(*net.UDPAddr) return net.UDPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil }