-
Notifications
You must be signed in to change notification settings - Fork 34
/
tcpconn_test.go
128 lines (114 loc) · 2.99 KB
/
tcpconn_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package buffstreams
import (
"log"
"os"
"strconv"
"testing"
"time"
"github.com/StabbyCutyou/buffstreams/test/message"
"github.com/golang/protobuf/proto"
)
func exampleCallback(bts []byte) error {
msg := &message.Note{}
err := proto.Unmarshal(bts, msg)
return err
}
var (
buffWriteConfig = TCPConnConfig{
MaxMessageSize: 2048,
Address: FormatAddress("127.0.0.1", strconv.Itoa(5034)),
}
buffWriteConfig2 = TCPConnConfig{
MaxMessageSize: 2048,
Address: FormatAddress("127.0.0.1", strconv.Itoa(5035)),
}
listenConfig = TCPListenerConfig{
MaxMessageSize: 2048,
EnableLogging: true,
Address: FormatAddress("", strconv.Itoa(5033)),
Callback: exampleCallback,
}
listenConfig2 = TCPListenerConfig{
MaxMessageSize: 2048,
EnableLogging: true,
Address: FormatAddress("", strconv.Itoa(5034)),
Callback: exampleCallback,
}
listenConfig3 = TCPListenerConfig{
MaxMessageSize: 2048,
EnableLogging: true,
Address: FormatAddress("", strconv.Itoa(5035)),
Callback: exampleCallback,
}
btl = &TCPListener{}
btl2 = &TCPListener{}
btl3 = &TCPListener{}
btc = &TCPConn{}
btc2 = &TCPConn{}
name = "Stabby"
date = time.Now().UnixNano()
data = "This is an intenntionally long and rambling sentence to pad out the size of the message."
msg = &message.Note{Name: &name, Date: &date, Comment: &data}
msgBytes = func(*message.Note) []byte { b, _ := proto.Marshal(msg); return b }(msg)
)
func TestMain(m *testing.M) {
btl, err := ListenTCP(listenConfig)
if err != nil {
log.Fatal(err)
}
btl.StartListeningAsync()
btl2, err = ListenTCP(listenConfig2)
if err != nil {
log.Fatal(err)
}
btl2.StartListeningAsync()
btl3, err = ListenTCP(listenConfig3)
if err != nil {
log.Fatal(err)
}
btl3.StartListeningAsync()
btc, err = DialTCP(&buffWriteConfig)
if err != nil {
log.Fatal(err)
}
btc2, err = DialTCP(&buffWriteConfig2)
if err != nil {
log.Fatal(err)
}
os.Exit(m.Run())
}
func TestDialBuffTCPUsesDefaultMessageSize(t *testing.T) {
cfg := TCPConnConfig{
Address: buffWriteConfig.Address,
}
buffM, err := DialTCP(&cfg)
if err != nil {
t.Errorf("Failed to open connection to %d: %s", cfg.Address, err)
}
if buffM.maxMessageSize != DefaultMaxMessageSize {
t.Errorf("Expected Max Message Size to be %d, actually got %d", DefaultMaxMessageSize, buffM.maxMessageSize)
}
}
func TestDialBuffTCPUsesSpecifiedMaxMessageSize(t *testing.T) {
cfg := TCPConnConfig{
Address: buffWriteConfig.Address,
MaxMessageSize: 8196,
}
conn, err := DialTCP(&cfg)
if err != nil {
t.Errorf("Failed to open connection to %d: %s", cfg.Address, err)
}
if conn.maxMessageSize != cfg.MaxMessageSize {
t.Errorf("Expected Max Message Size to be %d, actually got %d", cfg.MaxMessageSize, conn.maxMessageSize)
}
}
func BenchmarkWrite(b *testing.B) {
for n := 0; n < b.N; n++ {
btc.Write(msgBytes)
}
}
func BenchmarkWrite2(b *testing.B) {
for n := 0; n < b.N; n++ {
btc2.Write(msgBytes)
}
}