-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbyte_read_writer_test.go
56 lines (44 loc) · 1008 Bytes
/
byte_read_writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package metered
import (
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewReadWriter(t *testing.T) {
is := assert.New(t)
conn, err := net.Dial("tcp", "tcpbin.com:4242")
is.NotNil(conn)
is.Nil(err)
rw := NewReadWriter(conn)
is.NotNil(rw)
n, err := rw.Write([]byte("hello world\n"))
is.Equal(n, 12)
is.Nil(err)
is.Equal(rw.Rx(), uint64(0))
is.Equal(rw.Tx(), uint64(12))
resp := make([]byte, 1024)
n, err = rw.Read(resp)
is.Equal(n, 12)
is.Nil(err)
is.Equal(rw.Rx(), uint64(12))
is.Equal(rw.Tx(), uint64(12))
n, err = rw.Write([]byte("hello world\n"))
is.Equal(n, 12)
is.Nil(err)
is.Equal(rw.Rx(), uint64(12))
is.Equal(rw.Tx(), uint64(24))
resp = make([]byte, 1024)
n, err = rw.Read(resp)
is.Equal(n, 12)
is.Nil(err)
is.Equal(rw.Rx(), uint64(24))
is.Equal(rw.Tx(), uint64(24))
conn.Close()
n, err = rw.Write([]byte("hello world\n"))
is.Equal(n, 0)
is.Error(err)
resp = make([]byte, 1024)
n, err = rw.Read(resp)
is.Equal(n, 0)
is.Error(err)
}