-
Notifications
You must be signed in to change notification settings - Fork 23
/
mux_test.go
84 lines (65 loc) · 1.84 KB
/
mux_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
package openflow
import (
"bytes"
"fmt"
"io"
"net"
"sync"
"testing"
)
func TestMultiMatcher(t *testing.T) {
txn := uint32(42)
// A function, that matches the type of the request.
mf1 := func(r *Request) bool {
return r.Header.Type == TypeHello
}
// A function, that matches transaction ID.
mf2 := func(r *Request) bool {
return r.Header.Transaction == txn
}
matcher := MultiMatcher(&MatcherFunc{mf1}, &MatcherFunc{mf2})
r := NewRequest(TypePacketIn, nil)
if matcher.Match(r) {
t.Errorf("Matched request with different type")
}
r = NewRequest(TypeHello, nil)
r.Header.Transaction = txn + 1
if matcher.Match(r) {
t.Errorf("Matched request with different transaction ID")
}
r.Header.Transaction = txn
if !matcher.Match(r) {
t.Errorf("Request supposed to match")
}
}
func TestTypeMux(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
mux := NewTypeMux()
mux.HandleFunc(TypeHello, func(rw ResponseWriter, r *Request) {
defer wg.Done()
wbuf := bytes.NewBuffer([]byte{0, 0, 0, 0})
rw.Write(r.Header.Copy(), wbuf)
})
mux.HandleFunc(TypeEchoRequest, func(rw ResponseWriter, r *Request) {
t.Errorf("This handler should never be called")
})
reader := bytes.NewBuffer(newHeader(TypeHello))
conn := &dummyConn{r: *reader}
s := Server{Addr: "0.0.0.0:6633", Handler: mux}
defer s.close()
err := s.Serve(&dummyListener{[]net.Conn{conn}})
// Serve function will treat the connection as a regular
// connection, thus will try to read the next message after
// the reading the first one. And as the buffer is empty
// it will return EOF, which will be used to identify the
// successful read of the message.
if err != io.EOF {
t.Errorf("Serve failed: %v", err)
}
wg.Wait()
returned := fmt.Sprintf("%x", conn.w.Bytes())
if returned != "0400000c0000000000000000" {
t.Errorf("Invalid data returned: %v", returned)
}
}