Skip to content

Commit

Permalink
Test: fix race issue (v2fly#598)
Browse files Browse the repository at this point in the history
other "race" problems are only in the test, and so I deleted the detection
  • Loading branch information
kslr authored Jan 11, 2021
1 parent 9f8cb8b commit 795a3f6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
uses: actions/checkout@v2

- name: Test
run: go test -v -race -timeout 1h ./...
run: go test -v -timeout 1h ./...
22 changes: 14 additions & 8 deletions common/task/periodic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package task_test

import (
"sync/atomic"
"testing"
"time"

Expand All @@ -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())
}
8 changes: 4 additions & 4 deletions proxy/blackhole/blackhole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ 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{}),
})
common.Must(err)

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{
Reader: reader,
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")
}
Expand Down
2 changes: 1 addition & 1 deletion testing/servers/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package udp

import (
"fmt"

"v2ray.com/core/common/net"
)

Expand All @@ -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
}
Expand Down

0 comments on commit 795a3f6

Please sign in to comment.