编写一个简单的单元测试:
package socks5
import (
"log"
"net"
"runtime"
"testing"
"time"
)
func TestServer(t *testing.T) {
runtime.GOMAXPROCS(1)
for i := 0; i < 5; i++ {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
addr := l.Addr().String()
_ = l.Close()
s, err := NewClassicServer(addr, "127.0.0.1", "", "", 0, 0)
if err != nil {
log.Fatal(err)
}
go func() { _ = s.ListenAndServe(&DefaultHandle{}) }()
// Wait until the TCP listener is up: the TCP runner has been added,
// but the UDP runner (server.go:213) may still be pending, so the
// concurrent Shutdown below races with that Add.
waitAcceptable(addr)
_ = s.Shutdown()
}
}
func waitAcceptable(addr string) {
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
c, err := net.DialTimeout("tcp", addr, 50*time.Millisecond)
if err == nil {
_ = c.Close()
return
}
time.Sleep(10 * time.Millisecond)
}
}
执行:go test -race -run TestServer -v .报错:
=== RUN TestServer
==================
WARNING: DATA RACE
Read at 0x00c0001126e0 by goroutine 7:
github.com/txthinking/runnergroup.(*RunnerGroup).Done()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:74 +0x2c
github.com/txthinking/socks5.(*Server).Shutdown()
/Users/nange/go/src/github.com/nange/socks5/server.go:248 +0x64
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:37 +0x54
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
Previous write at 0x00c0001126e0 by goroutine 8:
github.com/txthinking/runnergroup.(*RunnerGroup).Add()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:33 +0x630
github.com/txthinking/socks5.(*Server).ListenAndServe()
/Users/nange/go/src/github.com/nange/socks5/server.go:213 +0x420
github.com/txthinking/socks5.TestServer.func1()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x3c
Goroutine 7 (running) created at:
testing.(*T).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x6e0
testing.runTests.func1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2477 +0x74
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.runTests()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2475 +0x734
testing.(*M).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2337 +0xaf4
main.main()
_testmain.go:57 +0x100
Goroutine 8 (running) created at:
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x38
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
==================
==================
WARNING: DATA RACE
Write at 0x00c000112750 by goroutine 7:
??()
-:0 +0x102927688
sync/atomic.CompareAndSwapInt32()
<autogenerated>:1 +0x14
sync.(*Mutex).Lock()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/sync/mutex.go:46 +0x2c
github.com/txthinking/runnergroup.(*RunnerGroup).Done()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:80 +0x138
github.com/txthinking/socks5.(*Server).Shutdown()
/Users/nange/go/src/github.com/nange/socks5/server.go:248 +0x64
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:37 +0x54
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
Previous write at 0x00c000112750 by goroutine 8:
github.com/txthinking/socks5.(*Server).ListenAndServe()
/Users/nange/go/src/github.com/nange/socks5/server.go:174 +0x13c
github.com/txthinking/socks5.TestServer.func1()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x3c
Goroutine 7 (running) created at:
testing.(*T).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x6e0
testing.runTests.func1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2477 +0x74
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.runTests()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2475 +0x734
testing.(*M).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2337 +0xaf4
main.main()
_testmain.go:57 +0x100
Goroutine 8 (running) created at:
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x38
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
==================
==================
WARNING: DATA RACE
Read at 0x00c000112758 by goroutine 7:
github.com/txthinking/runnergroup.(*RunnerGroup).Done()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:81 +0x144
github.com/txthinking/socks5.(*Server).Shutdown()
/Users/nange/go/src/github.com/nange/socks5/server.go:248 +0x64
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:37 +0x54
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
Previous write at 0x00c000112758 by goroutine 8:
github.com/txthinking/socks5.(*Server).ListenAndServe()
/Users/nange/go/src/github.com/nange/socks5/server.go:174 +0x13c
github.com/txthinking/socks5.TestServer.func1()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x3c
Goroutine 7 (running) created at:
testing.(*T).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x6e0
testing.runTests.func1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2477 +0x74
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.runTests()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2475 +0x734
testing.(*M).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2337 +0xaf4
main.main()
_testmain.go:57 +0x100
Goroutine 8 (running) created at:
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x38
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
==================
==================
WARNING: DATA RACE
Read at 0x00c000112748 by goroutine 7:
github.com/txthinking/runnergroup.(*RunnerGroup).Done()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:86 +0x164
github.com/txthinking/socks5.(*Server).Shutdown()
/Users/nange/go/src/github.com/nange/socks5/server.go:248 +0x64
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:37 +0x54
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
Previous write at 0x00c000112748 by goroutine 8:
github.com/txthinking/socks5.(*Server).ListenAndServe()
/Users/nange/go/src/github.com/nange/socks5/server.go:174 +0x13c
github.com/txthinking/socks5.TestServer.func1()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x3c
Goroutine 7 (running) created at:
testing.(*T).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x6e0
testing.runTests.func1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2477 +0x74
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.runTests()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2475 +0x734
testing.(*M).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2337 +0xaf4
main.main()
_testmain.go:57 +0x100
Goroutine 8 (running) created at:
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x38
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
==================
==================
WARNING: DATA RACE
Read at 0x00c000137c80 by goroutine 7:
net.(*TCPListener).ok()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/net/tcpsock_posix.go:156 +0x2c
net.(*TCPListener).Close()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/net/tcpsock.go:390 +0x38
github.com/txthinking/socks5.(*Server).ListenAndServe.func2()
/Users/nange/go/src/github.com/nange/socks5/server.go:200 +0x2c
github.com/txthinking/runnergroup.(*RunnerGroup).Done()
/Users/nange/go/pkg/mod/github.com/txthinking/runnergroup@v0.0.0-20210608031112-152c7c4432bf/runnergroup.go:86 +0x174
github.com/txthinking/socks5.(*Server).Shutdown()
/Users/nange/go/src/github.com/nange/socks5/server.go:248 +0x64
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:37 +0x54
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
Previous write at 0x00c000137c80 by goroutine 8:
net.(*sysListener).listenTCPProto()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/net/tcpsock_posix.go:193 +0xe8
net.(*sysListener).listenTCP()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/net/tcpsock_posix.go:179 +0x1a8
net.ListenTCP()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/net/tcpsock.go:460 +0x188
github.com/txthinking/socks5.(*Server).ListenAndServe()
/Users/nange/go/src/github.com/nange/socks5/server.go:170 +0x100
github.com/txthinking/socks5.TestServer.func1()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x3c
Goroutine 7 (running) created at:
testing.(*T).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x6e0
testing.runTests.func1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2477 +0x74
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.runTests()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2475 +0x734
testing.(*M).Run()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:2337 +0xaf4
main.main()
_testmain.go:57 +0x100
Goroutine 8 (running) created at:
github.com/txthinking/socks5.TestServer()
/Users/nange/go/src/github.com/nange/socks5/server_test.go:31 +0x38
testing.tRunner()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1934 +0x164
testing.(*T).Run.gowrap1()
/opt/homebrew/Cellar/go/1.25.7_1/libexec/src/testing/testing.go:1997 +0x3c
==================
testing.go:1617: race detected during execution of test
--- FAIL: TestServer (1.57s)
FAIL
FAIL github.com/txthinking/socks5 2.112s
FAIL
编写一个简单的单元测试:
执行:
go test -race -run TestServer -v .报错: