Skip to content

Commit

Permalink
Add self-dial test
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 20, 2019
1 parent 8daa622 commit 272618b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,52 @@ func TestServer_Serve(t *testing.T) {
require.NoError(t, err)
})

t.Run("test client's self-dialing", func(t *testing.T) {
// generate keys for the client
aPK, aSK := cipher.GenerateKeyPair()

// create client
a := NewClient(aPK, aSK, dc, SetLogger(logging.MustGetLogger("A")))
err = a.InitiateServerConnections(context.Background(), 1)
require.NoError(t, err)

// self-dial
selfWrTp, err := a.Dial(context.Background(), aPK)
require.NoError(t, err)

// self-accept
selfRdTp, err := a.Accept(context.Background())
require.NoError(t, err)

// try to write/read message to/from self
msgCount := 100
for i := 0; i < msgCount; i++ {
msg := []byte("Hello there!")

_, err := selfWrTp.Write(msg)
require.NoError(t, err)

recBuf := make([]byte, 5)

_, err = selfRdTp.Read(recBuf)
require.NoError(t, err)

_, err = selfRdTp.Read(recBuf)
require.NoError(t, err)

_, err = selfRdTp.Read(recBuf)
require.NoError(t, err)
}

err = selfRdTp.Close()
require.NoError(t, err)

err = selfWrTp.Close()
require.NoError(t, err)

err = a.Close()
require.NoError(t, err)
})
}

// Given two client instances (a & b) and a server instance (s),
Expand Down

0 comments on commit 272618b

Please sign in to comment.