forked from contiv/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
103 lines (85 loc) · 1.83 KB
/
stream_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
package libOpenflow
import (
"io"
"net"
"runtime"
"testing"
"time"
"github.com/contiv/libOpenflow/common"
"github.com/contiv/libOpenflow/openflow13"
"github.com/contiv/libOpenflow/util"
"github.com/sirupsen/logrus"
)
var helloMessage *common.Hello
var binaryMessage []byte
type fakeConn struct {
count int
max int
}
func (f *fakeConn) Close() error {
return nil
}
func (f *fakeConn) Read(b []byte) (int, error) {
if f.count == f.max {
return 0, io.EOF
}
f.count++
copy(b, binaryMessage)
return len(binaryMessage), nil
}
func (f *fakeConn) Write(b []byte) (int, error) {
b, _ = helloMessage.MarshalBinary()
return len(b), nil
}
func (f *fakeConn) LocalAddr() net.Addr {
return nil
}
func (f *fakeConn) RemoteAddr() net.Addr {
return nil
}
func (f *fakeConn) SetDeadline(t time.Time) error {
return nil
}
func (f *fakeConn) SetReadDeadline(t time.Time) error {
return nil
}
func (f *fakeConn) SetWriteDeadline(t time.Time) error {
return nil
}
type parserIntf struct {
}
func (p parserIntf) Parse(b []byte) (message util.Message, err error) {
switch b[0] {
case openflow13.VERSION:
message, err = openflow13.Parse(b)
default:
}
return
}
func init() {
helloMessage, _ = common.NewHello(4)
binaryMessage, _ = helloMessage.MarshalBinary()
}
func TestMessageStream(t *testing.T) {
var (
c = &fakeConn{
max: 5000000,
}
p = parserIntf{}
goroutineCountStart = runtime.NumGoroutine()
goroutineCountEnd int
)
logrus.SetLevel(logrus.PanicLevel)
stream := util.NewMessageStream(c, p)
go func() {
_ = <-stream.Error
}()
for i := 0; i < 5000000; i++ {
<-stream.Inbound
}
time.Sleep(2 * time.Second)
goroutineCountEnd = runtime.NumGoroutine()
if goroutineCountEnd > goroutineCountStart {
t.Fatalf("found more goroutines: %v before, %v after", goroutineCountStart, goroutineCountEnd)
}
}