2019-04-27 18:20:03 +02:00
|
|
|
package tcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTCP(t *testing.T) {
|
2019-09-08 15:12:16 +02:00
|
|
|
timeToRoomDeletion = 100 * time.Millisecond
|
2019-09-07 19:06:41 +02:00
|
|
|
go Run("debug", "8281", "8282")
|
2019-04-27 18:20:03 +02:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2019-09-08 15:12:16 +02:00
|
|
|
c1, banner, _, err := ConnectToTCPServer("localhost:8281", "testRoom", 1*time.Minute)
|
2019-09-07 19:06:41 +02:00
|
|
|
assert.Equal(t, banner, "8282")
|
2019-04-27 18:20:03 +02:00
|
|
|
assert.Nil(t, err)
|
2019-09-07 19:06:41 +02:00
|
|
|
c2, _, _, err := ConnectToTCPServer("localhost:8281", "testRoom")
|
2019-04-27 18:20:03 +02:00
|
|
|
assert.Nil(t, err)
|
2019-09-07 19:06:41 +02:00
|
|
|
_, _, _, err = ConnectToTCPServer("localhost:8281", "testRoom")
|
2019-04-27 18:20:03 +02:00
|
|
|
assert.NotNil(t, err)
|
2019-09-08 15:12:16 +02:00
|
|
|
_, _, _, err = ConnectToTCPServer("localhost:8281", "testRoom", 1*time.Nanosecond)
|
|
|
|
assert.NotNil(t, err)
|
2019-04-27 18:20:03 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|