|
|
@ -4,6 +4,7 @@ import ( |
|
|
|
"context" |
|
|
|
"encoding/hex" |
|
|
|
"net" |
|
|
|
"sync" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
|
|
|
@ -14,6 +15,7 @@ import ( |
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/internal/libs/protoio" |
|
|
|
"github.com/tendermint/tendermint/libs/log" |
|
|
|
"github.com/tendermint/tendermint/libs/service" |
|
|
|
tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p" |
|
|
|
"github.com/tendermint/tendermint/proto/tendermint/types" |
|
|
|
) |
|
|
@ -54,7 +56,7 @@ func TestMConnectionSendFlushStop(t *testing.T) { |
|
|
|
clientConn := createTestMConnection(log.TestingLogger(), client) |
|
|
|
err := clientConn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, clientConn)) |
|
|
|
t.Cleanup(waitAll(clientConn)) |
|
|
|
|
|
|
|
msg := []byte("abc") |
|
|
|
assert.True(t, clientConn.Send(0x01, msg)) |
|
|
@ -91,7 +93,7 @@ func TestMConnectionSend(t *testing.T) { |
|
|
|
mconn := createTestMConnection(log.TestingLogger(), client) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
msg := []byte("Ant-Man") |
|
|
|
assert.True(t, mconn.Send(0x01, msg)) |
|
|
@ -132,12 +134,12 @@ func TestMConnectionReceive(t *testing.T) { |
|
|
|
mconn1 := createMConnectionWithCallbacks(logger, client, onReceive, onError) |
|
|
|
err := mconn1.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn1)) |
|
|
|
t.Cleanup(waitAll(mconn1)) |
|
|
|
|
|
|
|
mconn2 := createTestMConnection(logger, server) |
|
|
|
err = mconn2.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn2)) |
|
|
|
t.Cleanup(waitAll(mconn2)) |
|
|
|
|
|
|
|
msg := []byte("Cyclops") |
|
|
|
assert.True(t, mconn2.Send(0x01, msg)) |
|
|
@ -171,7 +173,7 @@ func TestMConnectionPongTimeoutResultsInError(t *testing.T) { |
|
|
|
mconn := createMConnectionWithCallbacks(log.TestingLogger(), client, onReceive, onError) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
serverGotPing := make(chan struct{}) |
|
|
|
go func() { |
|
|
@ -212,7 +214,7 @@ func TestMConnectionMultiplePongsInTheBeginning(t *testing.T) { |
|
|
|
mconn := createMConnectionWithCallbacks(log.TestingLogger(), client, onReceive, onError) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
// sending 3 pongs in a row (abuse)
|
|
|
|
protoWriter := protoio.NewDelimitedWriter(server) |
|
|
@ -269,7 +271,7 @@ func TestMConnectionMultiplePings(t *testing.T) { |
|
|
|
mconn := createMConnectionWithCallbacks(log.TestingLogger(), client, onReceive, onError) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
// sending 3 pings in a row (abuse)
|
|
|
|
// see https://github.com/tendermint/tendermint/issues/1190
|
|
|
@ -320,7 +322,7 @@ func TestMConnectionPingPongs(t *testing.T) { |
|
|
|
mconn := createMConnectionWithCallbacks(log.TestingLogger(), client, onReceive, onError) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
serverGotPing := make(chan struct{}) |
|
|
|
go func() { |
|
|
@ -380,7 +382,7 @@ func TestMConnectionStopsAndReturnsError(t *testing.T) { |
|
|
|
mconn := createMConnectionWithCallbacks(log.TestingLogger(), client, onReceive, onError) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
if err := client.Close(); err != nil { |
|
|
|
t.Error(err) |
|
|
@ -454,7 +456,7 @@ func TestMConnectionReadErrorBadEncoding(t *testing.T) { |
|
|
|
_, err := client.Write([]byte{1, 2, 3, 4, 5}) |
|
|
|
require.NoError(t, err) |
|
|
|
assert.True(t, expectSend(chOnErr), "badly encoded msgPacket") |
|
|
|
t.Cleanup(stopAll(t, mconnClient, mconnServer)) |
|
|
|
t.Cleanup(waitAll(mconnClient, mconnServer)) |
|
|
|
} |
|
|
|
|
|
|
|
func TestMConnectionReadErrorUnknownChannel(t *testing.T) { |
|
|
@ -473,7 +475,7 @@ func TestMConnectionReadErrorUnknownChannel(t *testing.T) { |
|
|
|
// should cause an error
|
|
|
|
assert.True(t, mconnClient.Send(0x02, msg)) |
|
|
|
assert.True(t, expectSend(chOnErr), "unknown channel") |
|
|
|
t.Cleanup(stopAll(t, mconnClient, mconnServer)) |
|
|
|
t.Cleanup(waitAll(mconnClient, mconnServer)) |
|
|
|
} |
|
|
|
|
|
|
|
func TestMConnectionReadErrorLongMessage(t *testing.T) { |
|
|
@ -484,7 +486,7 @@ func TestMConnectionReadErrorLongMessage(t *testing.T) { |
|
|
|
defer cancel() |
|
|
|
|
|
|
|
mconnClient, mconnServer := newClientAndServerConnsForReadErrors(ctx, t, chOnErr) |
|
|
|
t.Cleanup(stopAll(t, mconnClient, mconnServer)) |
|
|
|
t.Cleanup(waitAll(mconnClient, mconnServer)) |
|
|
|
|
|
|
|
mconnServer.onReceive = func(chID ChannelID, msgBytes []byte) { |
|
|
|
chOnRcv <- struct{}{} |
|
|
@ -522,7 +524,7 @@ func TestMConnectionReadErrorUnknownMsgType(t *testing.T) { |
|
|
|
|
|
|
|
chOnErr := make(chan struct{}) |
|
|
|
mconnClient, mconnServer := newClientAndServerConnsForReadErrors(ctx, t, chOnErr) |
|
|
|
t.Cleanup(stopAll(t, mconnClient, mconnServer)) |
|
|
|
t.Cleanup(waitAll(mconnClient, mconnServer)) |
|
|
|
|
|
|
|
// send msg with unknown msg type
|
|
|
|
_, err := protoio.NewDelimitedWriter(mconnClient.conn).WriteMsg(&types.Header{ChainID: "x"}) |
|
|
@ -539,7 +541,7 @@ func TestMConnectionTrySend(t *testing.T) { |
|
|
|
mconn := createTestMConnection(log.TestingLogger(), client) |
|
|
|
err := mconn.Start(ctx) |
|
|
|
require.Nil(t, err) |
|
|
|
t.Cleanup(stopAll(t, mconn)) |
|
|
|
t.Cleanup(waitAll(mconn)) |
|
|
|
|
|
|
|
msg := []byte("Semicolon-Woman") |
|
|
|
resultCh := make(chan string, 2) |
|
|
@ -586,7 +588,7 @@ func TestMConnectionChannelOverflow(t *testing.T) { |
|
|
|
defer cancel() |
|
|
|
|
|
|
|
mconnClient, mconnServer := newClientAndServerConnsForReadErrors(ctx, t, chOnErr) |
|
|
|
t.Cleanup(stopAll(t, mconnClient, mconnServer)) |
|
|
|
t.Cleanup(waitAll(mconnClient, mconnServer)) |
|
|
|
|
|
|
|
mconnServer.onReceive = func(chID ChannelID, msgBytes []byte) { |
|
|
|
chOnRcv <- struct{}{} |
|
|
@ -611,16 +613,26 @@ func TestMConnectionChannelOverflow(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
type stopper interface { |
|
|
|
Stop() error |
|
|
|
} |
|
|
|
|
|
|
|
func stopAll(t *testing.T, stoppers ...stopper) func() { |
|
|
|
func waitAll(waiters ...service.Service) func() { |
|
|
|
return func() { |
|
|
|
for _, s := range stoppers { |
|
|
|
if err := s.Stop(); err != nil { |
|
|
|
t.Log(err) |
|
|
|
switch len(waiters) { |
|
|
|
case 0: |
|
|
|
return |
|
|
|
case 1: |
|
|
|
waiters[0].Wait() |
|
|
|
return |
|
|
|
default: |
|
|
|
wg := &sync.WaitGroup{} |
|
|
|
|
|
|
|
for _, w := range waiters { |
|
|
|
wg.Add(1) |
|
|
|
go func(s service.Service) { |
|
|
|
defer wg.Done() |
|
|
|
s.Wait() |
|
|
|
}(w) |
|
|
|
} |
|
|
|
|
|
|
|
wg.Wait() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|