forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveroptions_test.go
216 lines (202 loc) · 6.13 KB
/
serveroptions_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package signalr
import (
"errors"
"fmt"
"github.com/go-kit/kit/log"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
)
type singleHub struct {
Hub
id string
}
func (s *singleHub) OnConnected(string) {
s.initUUID()
singleHubMsg <- s.id
}
func (s *singleHub) GetUUID() string {
s.initUUID()
singleHubMsg <- s.id
return s.id
}
func (s *singleHub) initUUID() {
if s.id == "" {
s.id = uuid.New().String()
}
}
var singleHubMsg = make(chan string, 100)
var _ = Describe("Server options", func() {
Describe("UseHub option", func() {
Context("When the UseHub option is used", func() {
It("should use the same hub instance on all invocations", func() {
server, err := NewServer(UseHub(&singleHub{}))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn1 := newTestingConnection()
Expect(conn1).NotTo(BeNil())
go server.Run(conn1)
<-singleHubMsg
conn2 := newTestingConnection()
Expect(conn2).NotTo(BeNil())
go server.Run(conn2)
<-singleHubMsg
// Call GetId twice. If different instances used the results are different
var uuid string
conn1.ClientSend(`{"type":1,"invocationId": "123","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn1.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
uuid = fmt.Sprint(message.(completionMessage).Result)
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
conn1.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn1.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
Expect(fmt.Sprint(message.(completionMessage).Result)).To(Equal(uuid))
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
// Even on another connection, the uuid should be the same
conn2.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
<-singleHubMsg
select {
case message := <-conn2.received:
Expect(message).To(BeAssignableToTypeOf(completionMessage{}))
Expect(fmt.Sprint(message.(completionMessage).Result)).To(Equal(uuid))
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
})
})
})
Describe("SimpleHubFactory option", func() {
Context("When the SimpleHubFactory option is used", func() {
It("should call the hubfactory on each hub method invocation", func() {
server, err := NewServer(SimpleHubFactory(&singleHub{}))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnection()
Expect(conn).NotTo(BeNil())
go server.Run(conn)
uuids := make(map[string]interface{})
uuid := <-singleHubMsg
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
conn.ClientSend(`{"type":1,"invocationId": "123","target":"getuuid"}`)
select {
case uuid = <-singleHubMsg:
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
conn.ClientSend(`{"type":1,"invocationId": "456","target":"getuuid"}`)
select {
case uuid = <-singleHubMsg:
if _, ok := uuids[uuid]; ok {
Fail("same hub called twice")
} else {
uuids[uuid] = nil
}
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
})
})
})
Describe("Logger option", func() {
Context("When the Logger option with debug false is used", func() {
It("calling a method correctly should log no events", func() {
cw := newChannelWriter()
server, err := NewServer(UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), false))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnection()
Expect(conn).NotTo(BeNil())
go server.Run(conn)
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
<-invocationQueue
select {
case logEntry := <-cw.Chan():
Fail(fmt.Sprintf("log entry written: %v", string(logEntry)))
case <-time.After(1000 * time.Millisecond):
break
}
})
})
Context("When the Logger option with debug true is used", func() {
It("calling a method correctly should log events", func() {
cw := newChannelWriter()
server, err := NewServer(UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), true))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnection()
Expect(conn).NotTo(BeNil())
go server.Run(conn)
conn.ClientSend(`{"type":1,"invocationId": "123","target":"simple"}`)
<-invocationQueue
select {
case <-cw.Chan():
break
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("When the Logger option with debug false is used", func() {
It("calling a method incorrectly should log events", func() {
cw := newChannelWriter()
server, err := NewServer(UseHub(&invocationHub{}), Logger(log.NewLogfmtLogger(cw), false))
Expect(server).NotTo(BeNil())
Expect(err).To(BeNil())
conn := newTestingConnection()
Expect(conn).NotTo(BeNil())
go server.Run(conn)
conn.ClientSend(`{"type":1,"invocationId": "123","target":"sumple is simple with typo"}`)
select {
case <-cw.Chan():
break
case <-time.After(1000 * time.Millisecond):
Fail("timed out")
}
})
})
Context("When no option which sets the hub type is used, NewServer", func() {
It("should return an error", func() {
_, err := NewServer()
Expect(err).NotTo(BeNil())
})
})
Context("When an option returns an error, NewServer", func() {
It("should return an error", func() {
_, err := NewServer(func(*Server) error { return errors.New("bad option") })
Expect(err).NotTo(BeNil())
})
})
})
})
type channelWriter struct {
channel chan []byte
}
func (c *channelWriter) Write(p []byte) (n int, err error) {
c.channel <- p
return len(p), nil
}
func (c *channelWriter) Chan() chan []byte {
return c.channel
}
func newChannelWriter() *channelWriter {
return &channelWriter{make(chan []byte, 100)}
}