This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_test.go
More file actions
136 lines (107 loc) · 2.18 KB
/
setup_test.go
File metadata and controls
136 lines (107 loc) · 2.18 KB
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
package synapse
import (
"fmt"
"io"
"log"
"net"
"os"
"testing"
"time"
"github.com/tinylib/msgp/msgp"
)
var (
// both the TCP and unix socket
// client serve the same handler,
// so in every case we expect
// precisely the same behavior.
// tcp client for testing
tcpClient *Client
// unix socket clinet for testing
unxClient *Client
// only global so that we
// can attach handlers to it
// during tests
rt *RouteTable
ct testing.T
)
type testData []byte
func (s *testData) MarshalMsg(b []byte) ([]byte, error) {
return msgp.AppendBytes(b, []byte(*s)), nil
}
func (s *testData) UnmarshalMsg(b []byte) (o []byte, err error) {
var t []byte
t, o, err = msgp.ReadBytesBytes(b, []byte(*s))
*s = testData(t)
return
}
type EchoHandler struct{}
func (e EchoHandler) ServeCall(req Request, res ResponseWriter) {
var s msgp.Raw
err := req.Decode(&s)
if err != nil {
panic(err)
}
res.Send(&s)
}
type NopHandler struct{}
func (n NopHandler) ServeCall(req Request, res ResponseWriter) {
err := req.Decode(nil)
if err != nil {
panic(err)
}
res.Send(nil)
}
func finish(c io.Closer) {
err := c.Close()
if err != nil {
fmt.Println("warning:", err)
}
time.Sleep(1 * time.Millisecond)
}
const (
Echo Method = iota
Nop
DebugEcho
)
func TestMain(m *testing.M) {
RegisterName(Echo, "echo")
RegisterName(Nop, "nop")
RegisterName(DebugEcho, "debug-echo")
rt = &RouteTable{
Echo: EchoHandler{},
Nop: NopHandler{},
DebugEcho: Debug(EchoHandler{}, log.New(os.Stderr, "debug-echo :: ", log.LstdFlags)),
}
l, err := net.Listen("tcp", ":7070")
if err != nil {
panic(err)
}
go Serve(l, rt)
ul, err := net.Listen("unix", "synapse")
if err != nil {
panic(err)
}
go Serve(ul, rt)
tcpClient, err = Dial("tcp", ":7070", 5*time.Millisecond)
if err != nil {
panic(err)
}
unxClient, err = Dial("unix", "synapse", 5*time.Millisecond)
if err != nil {
panic(err)
}
ret := m.Run()
// note: the unix socket
// won't get cleaned up
// if the client is
// closed *after* the
// listener, which will
// cause subsequent tests
// to fail to bind: "address
// already in use"
finish(tcpClient)
finish(unxClient)
finish(ul)
finish(l)
os.Exit(ret)
}