Fix timing sensitive tests

This commit is contained in:
Andy Wang 2020-12-19 19:30:53 +00:00
parent 275540a793
commit 005da456c0
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
7 changed files with 13 additions and 17 deletions

View File

@ -1,10 +0,0 @@
language: go
go:
- "1.15"
script:
- go test -race -coverprofile=coverage.txt -coverpkg=./... -covermode=atomic ./...
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -76,7 +76,7 @@ func TestDatagramBuffer_BlockingRead(t *testing.T) {
pipe := NewDatagramBufferedPipe() pipe := NewDatagramBufferedPipe()
b := []byte{0x01, 0x02, 0x03} b := []byte{0x01, 0x02, 0x03}
go func() { go func() {
time.Sleep(100 * time.Millisecond) time.Sleep(readBlockTime)
pipe.Write(Frame{Payload: b}) pipe.Write(Frame{Payload: b})
}() }()
b2 := make([]byte, len(b)) b2 := make([]byte, len(b))

View File

@ -12,6 +12,8 @@ import (
"time" "time"
) )
const eventualConsistencyTolerance = 500 * time.Millisecond
func serveEcho(l net.Listener) { func serveEcho(l net.Listener) {
for { for {
conn, err := l.Accept() conn, err := l.Accept()
@ -111,6 +113,8 @@ func TestMultiplex(t *testing.T) {
//test echo //test echo
runEchoTest(t, streams, maxMsgLen) runEchoTest(t, streams, maxMsgLen)
time.Sleep(eventualConsistencyTolerance)
if clientSession.streamCount() != numStreams { if clientSession.streamCount() != numStreams {
t.Errorf("client stream count is wrong: %v", clientSession.streamCount()) t.Errorf("client stream count is wrong: %v", clientSession.streamCount())
} }
@ -148,7 +152,7 @@ func TestMux_StreamClosing(t *testing.T) {
t.Errorf("can't write to stream: %v", err) t.Errorf("can't write to stream: %v", err)
} }
time.Sleep(500 * time.Millisecond) time.Sleep(eventualConsistencyTolerance)
_ = toBeClosed.Close() _ = toBeClosed.Close()
_, err = io.ReadFull(toBeClosed, recvBuf) _, err = io.ReadFull(toBeClosed, recvBuf)
if err != nil { if err != nil {

View File

@ -408,7 +408,7 @@ func TestSession_timeoutAfter(t *testing.T) {
seshConfigOrdered.Obfuscator = obfuscator seshConfigOrdered.Obfuscator = obfuscator
seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond
sesh := MakeSession(0, seshConfigOrdered) sesh := MakeSession(0, seshConfigOrdered)
time.Sleep(200 * time.Millisecond) time.Sleep(2 * seshConfigOrdered.InactivityTimeout)
if !sesh.IsClosed() { if !sesh.IsClosed() {
t.Error("session should have timed out") t.Error("session should have timed out")
} }

View File

@ -7,6 +7,8 @@ import (
"time" "time"
) )
const readBlockTime = 500 * time.Millisecond
func TestPipeRW(t *testing.T) { func TestPipeRW(t *testing.T) {
pipe := NewStreamBufferedPipe() pipe := NewStreamBufferedPipe()
b := []byte{0x01, 0x02, 0x03} b := []byte{0x01, 0x02, 0x03}
@ -60,7 +62,7 @@ func TestReadBlock(t *testing.T) {
pipe := NewStreamBufferedPipe() pipe := NewStreamBufferedPipe()
b := []byte{0x01, 0x02, 0x03} b := []byte{0x01, 0x02, 0x03}
go func() { go func() {
time.Sleep(100 * time.Millisecond) time.Sleep(readBlockTime)
pipe.Write(b) pipe.Write(b)
}() }()
b2 := make([]byte, len(b)) b2 := make([]byte, len(b))

View File

@ -230,7 +230,7 @@ func TestStream_Close(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("can't read residual data %v", err) t.Errorf("can't read residual data %v", err)
} }
time.Sleep(100 * time.Millisecond) time.Sleep(eventualConsistencyTolerance)
if sI, _ := sesh.streams.Load(stream.(*Stream).id); sI != nil { if sI, _ := sesh.streams.Load(stream.(*Stream).id); sI != nil {
t.Error("stream still exists") t.Error("stream still exists")
return return

View File

@ -145,7 +145,7 @@ func TestSwitchboard_CloseOnOneDisconn(t *testing.T) {
sesh.AddConnection(conn1client) sesh.AddConnection(conn1client)
conn0server.Close() conn0server.Close()
time.Sleep(500 * time.Millisecond) time.Sleep(eventualConsistencyTolerance)
if !sesh.IsClosed() { if !sesh.IsClosed() {
t.Error("session not closed after one conn is disconnected") t.Error("session not closed after one conn is disconnected")
return return
@ -178,7 +178,7 @@ func TestSwitchboard_ConnsCount(t *testing.T) {
sesh.sb.closeAll() sesh.sb.closeAll()
time.Sleep(500 * time.Millisecond) time.Sleep(eventualConsistencyTolerance)
if sesh.sb.connsCount() != 0 { if sesh.sb.connsCount() != 0 {
t.Error("connsCount incorrect") t.Error("connsCount incorrect")
} }