From 40ecf2e535bb2cc1f8f8d1fa2003ad12ba88f683 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Wed, 19 Jun 2019 21:53:34 +0300 Subject: [PATCH 1/3] Finish tests, do cleanup --- pkg/dmsg/server_test.go | 67 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/pkg/dmsg/server_test.go b/pkg/dmsg/server_test.go index c0ea5e7dd..4fd824e65 100644 --- a/pkg/dmsg/server_test.go +++ b/pkg/dmsg/server_test.go @@ -594,8 +594,7 @@ func TestServer_Serve(t *testing.T) { // continue creating transports until the error occurs for { - //ctx := context.Background() - ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) + ctx := context.Background() if _, err = a.Dial(ctx, bPK); err != nil { break } @@ -605,8 +604,7 @@ func TestServer_Serve(t *testing.T) { // the same as above, transport is created by another client for { - //ctx := context.Background() - ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) + ctx := context.Background() if _, err = b.Dial(ctx, aPK); err != nil { break } @@ -640,9 +638,7 @@ func TestServer_Serve(t *testing.T) { err = a.Close() require.NoError(t, err) - b.log.Println("BEFORE CLOSING") err = b.Close() - b.log.Println("AFTER CLOSING") require.NoError(t, err) }) @@ -720,7 +716,66 @@ func TestServer_Serve(t *testing.T) { }) t.Run("test capped_transport_buffer_should_not_result_in_hang", func(t *testing.T) { + // generate keys for both clients + aPK, aSK := cipher.GenerateKeyPair() + bPK, bSK := cipher.GenerateKeyPair() + + // create remote + a := NewClient(aPK, aSK, dc) + a.SetLogger(logging.MustGetLogger("A")) + err = a.InitiateServerConnections(context.Background(), 1) + require.NoError(t, err) + + // create initiator + b := NewClient(bPK, bSK, dc) + b.SetLogger(logging.MustGetLogger("B")) + err = b.InitiateServerConnections(context.Background(), 1) + require.NoError(t, err) + + // create transports + aWrTransport, err := a.Dial(context.Background(), bPK) + require.NoError(t, err) + + _, err = b.Accept(context.Background()) + require.NoError(t, err) + + msg := []byte("Hello there!") + // exact iterations to fill the receiving buffer and hang `Write` + iterationsToDo := tpBufCap/len(msg) + 1 + + // fill the buffer, but no block yet + for i := 0; i < iterationsToDo-1; i++ { + _, err = aWrTransport.Write(msg) + require.NoError(t, err) + } + + // block on `Write` + go func() { + _, err = aWrTransport.Write(msg) + require.NoError(t, err) + }() + + // wait till it's definitely blocked + time.Sleep(1 * time.Second) + // create new transport from `B` to `A` + bWrTransport, err := b.Dial(context.Background(), aPK) + require.NoError(t, err) + + aRdTransport, err := a.Accept(context.Background()) + require.NoError(t, err) + + // try to write/read message via the new transports + for i := 0; i < 100; i++ { + _, err := bWrTransport.Write(msg) + require.NoError(t, err) + + recBuff := make([]byte, len(msg)) + _, err = aRdTransport.Read(recBuff) + require.NoError(t, err) + + require.Equal(t, recBuff, msg) + } }) } From 682d729e1ed05ed5fd757605d701cece0ff8df36 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Thu, 20 Jun 2019 20:29:48 +0300 Subject: [PATCH 2/3] Fix `SetLogger` call --- pkg/dmsg/server_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/dmsg/server_test.go b/pkg/dmsg/server_test.go index 744181fed..2f687aaac 100644 --- a/pkg/dmsg/server_test.go +++ b/pkg/dmsg/server_test.go @@ -713,14 +713,12 @@ func TestServer_Serve(t *testing.T) { bPK, bSK := cipher.GenerateKeyPair() // create remote - a := NewClient(aPK, aSK, dc) - a.SetLogger(logging.MustGetLogger("A")) + a := NewClient(aPK, aSK, dc, SetLogger(logging.MustGetLogger("A"))) err = a.InitiateServerConnections(context.Background(), 1) require.NoError(t, err) // create initiator - b := NewClient(bPK, bSK, dc) - b.SetLogger(logging.MustGetLogger("B")) + b := NewClient(bPK, bSK, dc, SetLogger(logging.MustGetLogger("B"))) err = b.InitiateServerConnections(context.Background(), 1) require.NoError(t, err) From a45cc9149b9efaba8dca27925e129d3716dc6b58 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Thu, 20 Jun 2019 20:32:11 +0300 Subject: [PATCH 3/3] Rename some tests --- pkg/dmsg/server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/dmsg/server_test.go b/pkg/dmsg/server_test.go index 2f687aaac..8b74d4fce 100644 --- a/pkg/dmsg/server_test.go +++ b/pkg/dmsg/server_test.go @@ -540,7 +540,7 @@ func TestServer_Serve(t *testing.T) { } }) - t.Run("test failed accept not hanging already established transport", func(t *testing.T) { + t.Run("failed_accepts_should_not_result_in_hang", func(t *testing.T) { // generate keys for both clients aPK, aSK := cipher.GenerateKeyPair() bPK, bSK := cipher.GenerateKeyPair() @@ -707,7 +707,7 @@ func TestServer_Serve(t *testing.T) { require.NoError(t, err) }) - t.Run("test capped_transport_buffer_should_not_result_in_hang", func(t *testing.T) { + t.Run("capped_transport_buffer_should_not_result_in_hang", func(t *testing.T) { // generate keys for both clients aPK, aSK := cipher.GenerateKeyPair() bPK, bSK := cipher.GenerateKeyPair()