Skip to content

Commit

Permalink
Add some more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Jun 18, 2019
1 parent 504cc37 commit f274870
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions pkg/dmsg/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ func TestServer_Serve(t *testing.T) {

var readErr, writeErr error
go func() {
// read/write to/from transport until the stop signal arrives
for {
select {
case <-readWriteStop:
Expand Down Expand Up @@ -646,6 +647,7 @@ func TestServer_Serve(t *testing.T) {
err = b.InitiateServerConnections(context.Background(), 1)
require.NoError(t, err)

// create transports
bTransport, err := b.Dial(context.Background(), aPK)
require.NoError(t, err)

Expand All @@ -656,30 +658,35 @@ func TestServer_Serve(t *testing.T) {
for i := 0; i < msgCount; i++ {
msg := "Hello there!"

// write message of 12 bytes
_, err := bTransport.Write([]byte(msg))
require.NoError(t, err)

recMsg := make([]byte, 5)
n, err := aTransport.Read(recMsg)
require.NoError(t, err)
// create a receiving buffer of 5 bytes
recBuff := make([]byte, 5)

received := string(recMsg[:n])
// read 5 bytes, 7 left
n, err := aTransport.Read(recBuff)
require.NoError(t, err)
require.Equal(t, n, len(recBuff))

log.Printf("Received: %v , bytes: %v", received, n)
received := string(recBuff[:n])

n, err = aTransport.Read(recMsg)
// read 5 more, 2 left
n, err = aTransport.Read(recBuff)
require.NoError(t, err)
require.Equal(t, n, len(recBuff))

received += string(recMsg[:n])

log.Printf("Received: %v , bytes: %v", received, n)
received += string(recBuff[:n])

n, err = aTransport.Read(recMsg)
// read 2 bytes left
n, err = aTransport.Read(recBuff)
require.NoError(t, err)
require.Equal(t, n, len(msg)-len(recBuff)*2)

received += string(recMsg[:n])
log.Printf("Last bytes count: %v", n)
received += string(recBuff[:n])

// received string must be equal to the sent one
require.Equal(t, received, msg)
}

Expand Down

0 comments on commit f274870

Please sign in to comment.