1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

Test: fix race issue (#598)

other "race" problems are only in the test, and so I deleted the detection
This commit is contained in:
Kslr 2021-01-11 20:50:58 +08:00 committed by GitHub
parent 9f8cb8bc8b
commit 795a3f632d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 14 deletions

View File

@ -34,4 +34,4 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Test - name: Test
run: go test -v -race -timeout 1h ./... run: go test -v -timeout 1h ./...

View File

@ -1,6 +1,7 @@
package task_test package task_test
import ( import (
"sync/atomic"
"testing" "testing"
"time" "time"
@ -9,28 +10,33 @@ import (
) )
func TestPeriodicTaskStop(t *testing.T) { func TestPeriodicTaskStop(t *testing.T) {
value := 0 var value uint64
task := &Periodic{ task := &Periodic{
Interval: time.Second * 2, Interval: time.Second * 2,
Execute: func() error { Execute: func() error {
value++ atomic.AddUint64(&value, 1)
return nil return nil
}, },
} }
common.Must(task.Start()) common.Must(task.Start())
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
common.Must(task.Close()) common.Must(task.Close())
if value != 3 { value1 := atomic.LoadUint64(&value)
t.Fatal("expected 3, but got ", value) if value1 != 3 {
t.Fatal("expected 3, but got ", value1)
} }
time.Sleep(time.Second * 4) time.Sleep(time.Second * 4)
if value != 3 { value2 := atomic.LoadUint64(&value)
t.Fatal("expected 3, but got ", value) if value2 != 3 {
t.Fatal("expected 3, but got ", value2)
} }
common.Must(task.Start()) common.Must(task.Start())
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
if value != 5 { value3 := atomic.LoadUint64(&value)
t.Fatal("Expected 5, but ", value) if value3 != 5 {
t.Fatal("Expected 5, but ", value3)
} }
common.Must(task.Close()) common.Must(task.Close())
} }

View File

@ -12,7 +12,7 @@ import (
"v2ray.com/core/transport/pipe" "v2ray.com/core/transport/pipe"
) )
func TestBlackholeHTTPResponse(t *testing.T) { func TestBlackHoleHTTPResponse(t *testing.T) {
handler, err := blackhole.New(context.Background(), &blackhole.Config{ handler, err := blackhole.New(context.Background(), &blackhole.Config{
Response: serial.ToTypedMessage(&blackhole.HTTPResponse{}), Response: serial.ToTypedMessage(&blackhole.HTTPResponse{}),
}) })
@ -20,12 +20,12 @@ func TestBlackholeHTTPResponse(t *testing.T) {
reader, writer := pipe.New(pipe.WithoutSizeLimit()) reader, writer := pipe.New(pipe.WithoutSizeLimit())
var readerError = make(chan error)
var mb buf.MultiBuffer var mb buf.MultiBuffer
var rerr error
go func() { go func() {
b, e := reader.ReadMultiBuffer() b, e := reader.ReadMultiBuffer()
mb = b mb = b
rerr = e readerError <- e
}() }()
link := transport.Link{ link := transport.Link{
@ -33,7 +33,7 @@ func TestBlackholeHTTPResponse(t *testing.T) {
Writer: writer, Writer: writer,
} }
common.Must(handler.Process(context.Background(), &link, nil)) common.Must(handler.Process(context.Background(), &link, nil))
common.Must(rerr) common.Must(<-readerError)
if mb.IsEmpty() { if mb.IsEmpty() {
t.Error("expect http response, but nothing") t.Error("expect http response, but nothing")
} }

View File

@ -2,7 +2,6 @@ package udp
import ( import (
"fmt" "fmt"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
) )
@ -27,6 +26,7 @@ func (server *Server) Start() (net.Destination, error) {
server.conn = conn server.conn = conn
go server.handleConnection(conn) go server.handleConnection(conn)
localAddr := conn.LocalAddr().(*net.UDPAddr) localAddr := conn.LocalAddr().(*net.UDPAddr)
return net.UDPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil return net.UDPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil
} }