Skip to content

Commit 4a37449

Browse files
committed
Replace all time-sensitive tests with assert.Eventually
1 parent 8d14658 commit 4a37449

File tree

7 files changed

+51
-64
lines changed

7 files changed

+51
-64
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/refraction-networking/utls v0.0.0-20190909200633-43c36d3c1f57
1616
github.com/sirupsen/logrus v1.5.0
1717
github.com/stephens2424/writerset v1.0.2 // indirect
18-
github.com/stretchr/testify v1.3.0
18+
github.com/stretchr/testify v1.6.1
1919
go.etcd.io/bbolt v1.3.4
2020
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
2121
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
4343
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
4444
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
4545
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
46+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
47+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4648
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
4749
go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg=
4850
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
@@ -76,5 +78,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
7678
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7779
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
7880
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
81+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7982
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
8083
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
84+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
85+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/multiplex/mux_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"github.com/cbeuw/Cloak/internal/common"
66
"github.com/cbeuw/connutil"
7+
"github.com/stretchr/testify/assert"
78
"io"
89
"math/rand"
910
"net"
@@ -12,8 +13,6 @@ import (
1213
"time"
1314
)
1415

15-
const eventualConsistencyTolerance = 500 * time.Millisecond
16-
1716
func serveEcho(l net.Listener) {
1817
for {
1918
conn, err := l.Accept()
@@ -114,13 +113,13 @@ func TestMultiplex(t *testing.T) {
114113
//test echo
115114
runEchoTest(t, streams, maxMsgLen)
116115

117-
time.Sleep(eventualConsistencyTolerance)
118-
if clientSession.streamCount() != numStreams {
119-
t.Errorf("client stream count is wrong: %v", clientSession.streamCount())
120-
}
121-
if serverSession.streamCount() != numStreams {
122-
t.Errorf("server stream count is wrong: %v", serverSession.streamCount())
123-
}
116+
assert.Eventuallyf(t, func() bool {
117+
return clientSession.streamCount() == numStreams
118+
}, time.Second, 10*time.Millisecond, "client stream count is wrong: %v", clientSession.streamCount())
119+
120+
assert.Eventuallyf(t, func() bool {
121+
return serverSession.streamCount() == numStreams
122+
}, time.Second, 10*time.Millisecond, "server stream count is wrong: %v", serverSession.streamCount())
124123

125124
// close one stream
126125
closing, streams := streams[0], streams[1:]
@@ -152,9 +151,12 @@ func TestMux_StreamClosing(t *testing.T) {
152151
t.Errorf("can't write to stream: %v", err)
153152
}
154153

155-
time.Sleep(eventualConsistencyTolerance)
154+
_, err = io.ReadFull(toBeClosed, recvBuf[:1])
155+
if err != nil {
156+
t.Errorf("can't read anything before stream closed: %v", err)
157+
}
156158
_ = toBeClosed.Close()
157-
_, err = io.ReadFull(toBeClosed, recvBuf)
159+
_, err = io.ReadFull(toBeClosed, recvBuf[1:])
158160
if err != nil {
159161
t.Errorf("can't read residual data on stream: %v", err)
160162
}

internal/multiplex/session_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package multiplex
33
import (
44
"bytes"
55
"github.com/cbeuw/connutil"
6+
"github.com/stretchr/testify/assert"
67
"math/rand"
78
"strconv"
89
"sync"
@@ -408,10 +409,10 @@ func TestSession_timeoutAfter(t *testing.T) {
408409
seshConfigOrdered.Obfuscator = obfuscator
409410
seshConfigOrdered.InactivityTimeout = 100 * time.Millisecond
410411
sesh := MakeSession(0, seshConfigOrdered)
411-
time.Sleep(5 * seshConfigOrdered.InactivityTimeout)
412-
if !sesh.IsClosed() {
413-
t.Error("session should have timed out")
414-
}
412+
413+
assert.Eventually(t, func() bool {
414+
return sesh.IsClosed()
415+
}, 5*seshConfigOrdered.InactivityTimeout, seshConfigOrdered.InactivityTimeout, "session should have timed out")
415416
}
416417

417418
func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) {

internal/multiplex/stream_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package multiplex
33
import (
44
"bytes"
55
"github.com/cbeuw/Cloak/internal/common"
6+
"github.com/stretchr/testify/assert"
67
"io"
78
"io/ioutil"
89
"math/rand"
@@ -230,11 +231,11 @@ func TestStream_Close(t *testing.T) {
230231
if err != nil {
231232
t.Errorf("can't read residual data %v", err)
232233
}
233-
time.Sleep(eventualConsistencyTolerance)
234-
if sI, _ := sesh.streams.Load(stream.(*Stream).id); sI != nil {
235-
t.Error("stream still exists")
236-
return
237-
}
234+
235+
assert.Eventually(t, func() bool {
236+
sI, _ := sesh.streams.Load(stream.(*Stream).id)
237+
return sI == nil
238+
}, time.Second, 10*time.Millisecond, "streams still exists")
238239

239240
})
240241
}

internal/multiplex/switchboard_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package multiplex
22

33
import (
44
"github.com/cbeuw/connutil"
5+
"github.com/stretchr/testify/assert"
56
"math/rand"
67
"sync"
78
"testing"
@@ -145,11 +146,11 @@ func TestSwitchboard_CloseOnOneDisconn(t *testing.T) {
145146
sesh.AddConnection(conn1client)
146147

147148
conn0server.Close()
148-
time.Sleep(eventualConsistencyTolerance)
149-
if !sesh.IsClosed() {
150-
t.Error("session not closed after one conn is disconnected")
151-
return
152-
}
149+
150+
assert.Eventually(t, func() bool {
151+
return sesh.IsClosed()
152+
}, time.Second, 10*time.Millisecond, "session not closed after one conn is disconnected")
153+
153154
if _, err := conn1client.Write([]byte{0x00}); err == nil {
154155
t.Error("the other conn is still connected")
155156
return
@@ -178,9 +179,7 @@ func TestSwitchboard_ConnsCount(t *testing.T) {
178179

179180
sesh.sb.closeAll()
180181

181-
time.Sleep(eventualConsistencyTolerance)
182-
if sesh.sb.connsCount() != 0 {
183-
t.Error("connsCount incorrect")
184-
}
185-
182+
assert.Eventuallyf(t, func() bool {
183+
return sesh.sb.connsCount() == 0
184+
}, time.Second, 10*time.Millisecond, "connsCount incorrect: %v", sesh.sb.connsCount())
186185
}

internal/test/integration_test.go

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
mux "github.com/cbeuw/Cloak/internal/multiplex"
1111
"github.com/cbeuw/Cloak/internal/server"
1212
"github.com/cbeuw/connutil"
13+
"github.com/stretchr/testify/assert"
1314
"io"
1415
"io/ioutil"
1516
"math/rand"
@@ -356,17 +357,9 @@ func TestTCPSingleplex(t *testing.T) {
356357

357358
proxyConn1.Close()
358359

359-
retries := 0
360-
retry:
361-
time.Sleep(delayBeforeTestingConnClose)
362-
if user.NumSession() != 1 {
363-
retries++
364-
if retries > connCloseRetries {
365-
t.Error("first session was not closed on connection close")
366-
} else {
367-
goto retry
368-
}
369-
}
360+
assert.Eventually(t, func() bool {
361+
return user.NumSession() == 1
362+
}, time.Second, 10*time.Millisecond, "first session was not closed on connection close")
370363

371364
// conn2 should still work
372365
runEchoTest(t, []net.Conn{proxyConn2}, 65536)
@@ -479,17 +472,10 @@ func TestClosingStreamsFromProxy(t *testing.T) {
479472
serverConn, _ := proxyFromCkServerL.Accept()
480473
serverConn.Close()
481474

482-
retries := 0
483-
retry:
484-
time.Sleep(delayBeforeTestingConnClose)
485-
if _, err := clientConn.Read(make([]byte, 16)); err == nil {
486-
retries++
487-
if retries > connCloseRetries {
488-
t.Errorf("closing stream on server side is not reflected to the client: %v", err)
489-
} else {
490-
goto retry
491-
}
492-
}
475+
assert.Eventually(t, func() bool {
476+
_, err := clientConn.Read(make([]byte, 16))
477+
return err != nil
478+
}, time.Second, 10*time.Millisecond, "closing stream on server side is not reflected to the client")
493479
})
494480

495481
t.Run("closing from client", func(t *testing.T) {
@@ -499,17 +485,10 @@ func TestClosingStreamsFromProxy(t *testing.T) {
499485
serverConn, _ := proxyFromCkServerL.Accept()
500486
clientConn.Close()
501487

502-
retries := 0
503-
retry:
504-
time.Sleep(delayBeforeTestingConnClose)
505-
if _, err := serverConn.Read(make([]byte, 16)); err == nil {
506-
retries++
507-
if retries > 3 {
508-
t.Errorf("closing stream on client side is not reflected to the server: %v", err)
509-
} else {
510-
goto retry
511-
}
512-
}
488+
assert.Eventually(t, func() bool {
489+
_, err := serverConn.Read(make([]byte, 16))
490+
return err != nil
491+
}, time.Second, 10*time.Millisecond, "closing stream on client side is not reflected to the server")
513492
})
514493

515494
t.Run("send then close", func(t *testing.T) {

0 commit comments

Comments
 (0)