croc/src/tcp/tcp_test.go

35 lines
826 B
Go
Raw Normal View History

2019-04-27 18:20:03 +02:00
package tcp
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTCP(t *testing.T) {
2019-05-02 01:10:02 +02:00
go Run("debug", "8081", "8082")
2019-04-27 18:20:03 +02:00
time.Sleep(100 * time.Millisecond)
2019-05-03 05:57:55 +02:00
c1, banner, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
2019-05-02 01:10:02 +02:00
assert.Equal(t, banner, "8082")
2019-04-27 18:20:03 +02:00
assert.Nil(t, err)
2019-05-02 01:10:02 +02:00
c2, _, _, err := ConnectToTCPServer("localhost:8081", "testRoom")
2019-04-27 18:20:03 +02:00
assert.Nil(t, err)
2019-05-02 01:10:02 +02:00
_, _, _, err = ConnectToTCPServer("localhost:8081", "testRoom")
2019-04-27 18:20:03 +02:00
assert.NotNil(t, err)
// try sending data
assert.Nil(t, c1.Send([]byte("hello, c2")))
data, err := c2.Receive()
assert.Nil(t, err)
assert.Equal(t, []byte("hello, c2"), data)
assert.Nil(t, c2.Send([]byte("hello, c1")))
data, err = c1.Receive()
assert.Nil(t, err)
assert.Equal(t, []byte("hello, c1"), data)
c1.Close()
2019-04-30 19:14:21 +02:00
time.Sleep(300 * time.Millisecond)
2019-04-27 18:20:03 +02:00
}