This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathspoe_test.go
149 lines (113 loc) · 2.95 KB
/
spoe_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package spoe
import (
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"
pool "github.com/libp2p/go-buffer-pool"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
func TestSPOE(t *testing.T) {
spoa := New(func(msgs *MessageIterator) ([]Action, error) {
return nil, nil
})
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
agentError := make(chan error)
go func() {
agentError <- spoa.Serve(lis)
}()
client, err := net.Dial("tcp", lis.Addr().String())
require.NoError(t, err)
cod := newCodec(client, defaultConfig)
// hello
helloReq := helloFrame(t)
require.NoError(t, cod.encodeFrame(helloReq))
helloRes := Frame{}
ok, err := cod.decodeFrame(&helloRes)
require.True(t, ok)
require.NoError(t, err)
require.Equal(t, helloReq.streamID, helloRes.streamID)
// notify
notifyReq := notifyFrame(t)
require.NoError(t, cod.encodeFrame(notifyReq))
notifyRes := Frame{}
ok, err = cod.decodeFrame(¬ifyRes)
require.True(t, ok)
require.NoError(t, err)
require.Equal(t, notifyReq.streamID, notifyRes.streamID)
}
func TestSPOEUnix(t *testing.T) {
spoa := New(func(msgs *MessageIterator) ([]Action, error) {
return nil, nil
})
name, err := ioutil.TempDir("/tmp", "http-mirror-test")
require.NoError(t, err)
defer os.RemoveAll(name)
sock := filepath.Join(name, "spoe.sock")
lis, err := net.Listen("unix", sock)
require.NoError(t, err)
defer lis.Close()
agentError := make(chan error)
go func() {
agentError <- spoa.Serve(lis)
}()
client, err := net.Dial("unix", sock)
require.NoError(t, err)
cod := newCodec(client, defaultConfig)
// hello
helloReq := helloFrame(t)
require.NoError(t, cod.encodeFrame(helloReq))
helloRes := Frame{}
ok, err := cod.decodeFrame(&helloRes)
require.True(t, ok)
require.NoError(t, err)
require.Equal(t, helloReq.streamID, helloRes.streamID)
// notify
notifyReq := notifyFrame(t)
require.NoError(t, cod.encodeFrame(notifyReq))
notifyRes := Frame{}
ok, err = cod.decodeFrame(¬ifyRes)
require.True(t, ok)
require.NoError(t, err)
require.Equal(t, notifyReq.streamID, notifyRes.streamID)
}
func BenchmarkSPOE(b *testing.B) {
log.SetLevel(log.FatalLevel)
spoa := New(func(msgs *MessageIterator) ([]Action, error) {
for msgs.Next() {
}
return nil, nil
})
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(b, err)
agentError := make(chan error)
go func() {
agentError <- spoa.Serve(lis)
}()
client, err := net.Dial("tcp", lis.Addr().String())
require.NoError(b, err)
cod := newCodec(client, defaultConfig)
// hello
helloReq := helloFrame(b)
require.NoError(b, cod.encodeFrame(helloReq))
notifyReq := notifyFrame(b)
res := Frame{}
ok, err := cod.decodeFrame(&res)
require.True(b, ok)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := cod.encodeFrame(notifyReq)
if err != nil {
b.Fatal(err)
}
_, err = cod.decodeFrame(&res)
if err != nil {
b.Error(err)
}
pool.Put(res.originalData)
}
}