-
Notifications
You must be signed in to change notification settings - Fork 5
/
conn_test.go
69 lines (58 loc) · 1.29 KB
/
conn_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
57
58
59
60
61
62
63
64
65
66
67
68
69
package marionette_test
import (
"bytes"
"io"
"net"
"testing"
"github.com/redjack/marionette"
)
func TestBufferedConn(t *testing.T) {
// Generate 10MB of data.
data := make([]byte, 10*1024*1024)
for i := range data {
data[i] = byte(i % 100)
}
// Open random port.
ln, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
// Accept a connection in a separate goroutine and stream data to client.
go func() {
conn, err := ln.Accept()
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if _, err := io.Copy(conn, bytes.NewReader(data)); err != nil {
t.Fatal(err)
}
}()
// Connect to listener.
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
// Wrap in bufferred connection.
bufConn := marionette.NewBufferedConn(conn, marionette.MaxCellLength)
// Read all data.
var buf bytes.Buffer
for {
// Read 100b at a time.
b, err := bufConn.Peek(100, false)
if buf.Write(b); err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
}
if _, err := bufConn.Seek(int64(len(b)), io.SeekCurrent); err != nil {
t.Fatal(err)
}
}
// Verify correctness.
if b := buf.Bytes(); !bytes.Equal(b, data) {
t.Fatalf("incorrect bytes read: got=%d, exp=%d", len(b), len(data))
}
}