-
Notifications
You must be signed in to change notification settings - Fork 23
/
responsewrite_test.go
113 lines (87 loc) · 2.54 KB
/
responsewrite_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
// +build integration
package openflow_test
import (
"fmt"
"log"
"net"
"testing"
"time"
of "github.com/netrack/openflow"
"github.com/netrack/openflow/ofp"
"github.com/netrack/openflow/ofptest"
"github.com/netrack/openflow/ofputil"
)
func waitAll(ch chan struct{}, n int, t time.Duration) error {
timer := time.NewTimer(t)
defer timer.Stop()
for i := 0; i < n; i++ {
select {
case <-ch:
case <-timer.C:
text := "no response within the time interval"
return fmt.Errorf(text)
}
}
return nil
}
func TestResponseWrite(t *testing.T) {
// Wait for 3 successful echo-request replies and a
// reply on the multipart aggregated statistics.
nreqs := 4
ch := make(chan struct{}, nreqs)
mux := of.NewServeMux()
statsHandler := func(rw of.ResponseWriter, r *of.Request) {
ch <- struct{}{}
}
featuresHandler := func(rw of.ResponseWriter, r *of.Request) {
var resp ofp.MultipartReply
var features ofp.TableFeatures
resp.ReadFrom(r.Body)
features.ReadFrom(r.Body)
// Read only the first table feature.
log.Println(features)
}
helloHandler := func(rw of.ResponseWriter, r *of.Request) {
rw.Write(r.Header.Copy(), nil)
// Send a multipart request to retrieve statistics from
// the connected switch.
body := &ofp.AggregateStatsRequest{
OutPort: ofp.PortAny,
OutGroup: ofp.GroupAny,
Match: ofputil.ExtendedMatch(
ofputil.MatchInPort(1),
),
}
req := ofp.NewMultipartRequest(
ofp.MultipartTypeAggregate, body)
header := &of.Header{Type: of.TypeMultipartRequest}
pattern := of.TransactionMatcher(header)
// Create a matcher based on the header transaction. It
// will be used to handle the multipart response.
mux.HandleOnce(pattern, of.HandlerFunc(statsHandler))
rw.Write(header, req)
req = ofp.NewMultipartRequest(
ofp.MultipartTypeTableFeatures, nil)
header = &of.Header{Type: of.TypeMultipartRequest}
pattern = of.TransactionMatcher(header)
mux.Handle(pattern, of.HandlerFunc(featuresHandler))
rw.Write(header, req)
}
echoHandler := func(rw of.ResponseWriter, r *of.Request) {
var req ofp.EchoRequest
req.ReadFrom(r.Body)
header := r.Header.Copy()
header.Type = of.TypeEchoReply
rw.Write(header, &ofp.EchoReply{req.Data})
ch <- struct{}{}
}
mux.HandleFunc(of.TypeMatcher(of.TypeHello), helloHandler)
mux.HandleFunc(of.TypeMatcher(of.TypeEchoRequest), echoHandler)
ln, _ := net.Listen("tcp", "127.0.0.1:6633")
s := ofptest.NewUnstartedServer(mux, ln)
s.Start()
defer s.Close()
if err := waitAll(ch, nreqs, 20*time.Second); err != nil {
t.Fatal(err.Error())
}
}