Fix multiplex test as test payload length may be randomised to 0

This commit is contained in:
Andy Wang 2020-12-22 12:39:39 +00:00
parent c9ac93b0b9
commit 3633c9a03c
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
1 changed files with 8 additions and 15 deletions

View File

@ -10,7 +10,6 @@ import (
"net" "net"
"sync" "sync"
"testing" "testing"
"time"
) )
func serveEcho(l net.Listener) { func serveEcho(l net.Listener) {
@ -64,21 +63,20 @@ func makeSessionPair(numConn int) (*Session, *Session, []*connPair) {
return clientSession, serverSession, paris return clientSession, serverSession, paris
} }
func runEchoTest(t *testing.T, conns []net.Conn, maxMsgLen int) { func runEchoTest(t *testing.T, conns []net.Conn, msgLen int) {
var wg sync.WaitGroup var wg sync.WaitGroup
for _, conn := range conns { for _, conn := range conns {
wg.Add(1) wg.Add(1)
go func(conn net.Conn) { go func(conn net.Conn) {
testDataLen := rand.Intn(maxMsgLen) testData := make([]byte, msgLen)
testData := make([]byte, testDataLen)
rand.Read(testData) rand.Read(testData)
n, err := conn.Write(testData) n, err := conn.Write(testData)
if n != testDataLen { if n != msgLen {
t.Fatalf("written only %v, err %v", n, err) t.Fatalf("written only %v, err %v", n, err)
} }
recvBuf := make([]byte, testDataLen) recvBuf := make([]byte, msgLen)
_, err = io.ReadFull(conn, recvBuf) _, err = io.ReadFull(conn, recvBuf)
if err != nil { if err != nil {
t.Fatalf("failed to read back: %v", err) t.Fatalf("failed to read back: %v", err)
@ -96,7 +94,7 @@ func runEchoTest(t *testing.T, conns []net.Conn, maxMsgLen int) {
func TestMultiplex(t *testing.T) { func TestMultiplex(t *testing.T) {
const numStreams = 2000 // -race option limits the number of goroutines to 8192 const numStreams = 2000 // -race option limits the number of goroutines to 8192
const numConns = 4 const numConns = 4
const maxMsgLen = 16384 const msgLen = 16384
clientSession, serverSession, _ := makeSessionPair(numConns) clientSession, serverSession, _ := makeSessionPair(numConns)
go serveEcho(serverSession) go serveEcho(serverSession)
@ -111,15 +109,10 @@ func TestMultiplex(t *testing.T) {
} }
//test echo //test echo
runEchoTest(t, streams, maxMsgLen) runEchoTest(t, streams, msgLen)
assert.Eventuallyf(t, func() bool { assert.EqualValues(t, numStreams, clientSession.streamCount(), "client stream count is wrong")
return clientSession.streamCount() == numStreams assert.EqualValues(t, numStreams, serverSession.streamCount(), "server stream count is wrong")
}, time.Second, 10*time.Millisecond, "client stream count is wrong: %v", clientSession.streamCount())
assert.Eventuallyf(t, func() bool {
return serverSession.streamCount() == numStreams
}, time.Second, 10*time.Millisecond, "server stream count is wrong: %v", serverSession.streamCount())
// close one stream // close one stream
closing, streams := streams[0], streams[1:] closing, streams := streams[0], streams[1:]