forked from kataras/neffos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
208 lines (169 loc) · 4.98 KB
/
server_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
package neffos_test
import (
"bytes"
"context"
"fmt"
"net/http"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/kataras/neffos"
gobwas "github.com/kataras/neffos/gobwas"
gorilla "github.com/kataras/neffos/gorilla"
"golang.org/x/sync/errgroup"
)
func runTestServer(addr string, connHandler neffos.ConnHandler, configureServer ...func(*neffos.Server)) func() error {
gobwasServer := neffos.New(gobwas.DefaultUpgrader, connHandler)
gorillaServer := neffos.New(gorilla.DefaultUpgrader, connHandler)
for _, cfg := range configureServer {
cfg(gobwasServer)
cfg(gorillaServer)
}
mux := http.NewServeMux()
mux.Handle("/gobwas", gobwasServer)
mux.Handle("/gorilla", gorillaServer)
httpServer := http.Server{
Addr: addr,
Handler: mux,
}
go httpServer.ListenAndServe()
time.Sleep(200 * time.Millisecond)
// teardown.
return func() error {
gorillaServer.Close()
gobwasServer.Close()
return httpServer.Close()
}
}
func TestServerBroadcastTo(t *testing.T) {
// we fire up two connections, one with the "conn_ID" and other with the default uuid id generator,
// the message which the second client emits should only be sent to the connection with the ID of "conn_ID".
var (
wg sync.WaitGroup
namespace = "default"
body = []byte("data")
to = "conn_ID"
events = neffos.Namespaces{
namespace: neffos.Events{
"event": func(c *neffos.NSConn, msg neffos.Message) error {
if c.Conn.IsClient() {
if !bytes.Equal(msg.Body, body) {
t.Fatalf("expected event's incoming data to be: %s but got: %s", string(body), string(msg.Body))
}
if c.String() != to {
t.Fatalf("expected the message to be sent only to the connection with an ID of 'conn_ID'")
}
wg.Done()
} else {
msg.To = to
c.Conn.Server().Broadcast(c, msg)
}
return nil
},
},
}
)
teardownServer := runTestServer("localhost:8080", events, func(wsServer *neffos.Server) {
once := new(uint32)
wsServer.IDGenerator = func(w http.ResponseWriter, r *http.Request) string {
if atomic.CompareAndSwapUint32(once, 0, 1) {
return to // set the "to" only to the first conn for test.
}
return neffos.DefaultIDGenerator(w, r)
}
})
defer teardownServer()
wg.Add(2)
teardownClient1 := runTestClient("localhost:8080", events,
func(dialer string, client *neffos.Client) {
_, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
})
defer teardownClient1()
teardownClient2 := runTestClient("localhost:8080", events,
func(dialer string, client *neffos.Client) {
c, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
c.Emit("event", body)
})
defer teardownClient2()
wg.Wait()
}
func TestServerAsk(t *testing.T) {
// we fire up two connections, one with the "conn_ID" and other with the default uuid id generator,
// the message which the second client emits should only be sent to the connection with the ID of "conn_ID".
var (
wg sync.WaitGroup
namespace = "default"
body = []byte("data")
expectResponse = append(body, []byte("ok")...)
to = "conn_ID"
clientEvents = neffos.Namespaces{
namespace: neffos.Events{
"ask": func(c *neffos.NSConn, msg neffos.Message) error {
return neffos.Reply(expectResponse)
},
},
}
)
g := new(errgroup.Group)
teardownServer := runTestServer("localhost:8080", neffos.Namespaces{namespace: neffos.Events{}}, func(wsServer *neffos.Server) {
once := new(uint32)
wsServer.IDGenerator = func(w http.ResponseWriter, r *http.Request) string {
if atomic.CompareAndSwapUint32(once, 0, 1) {
return to // set the "to" only to the first conn for test.
}
return neffos.DefaultIDGenerator(w, r)
}
wgWaitToAllConnect := new(sync.WaitGroup)
wgWaitToAllConnect.Add(2)
wsServer.OnConnect = func(c *neffos.Conn) error {
wgWaitToAllConnect.Done()
return nil
}
worker := func() error {
wgWaitToAllConnect.Wait()
response, err := wsServer.Ask(context.TODO(), neffos.Message{
Namespace: "default",
Event: "ask",
To: to,
})
if err != nil {
return err
}
if !bytes.Equal(response.Body, expectResponse) {
return fmt.Errorf("expected response with body: %s but got: %s", string(expectResponse), string(response.Body))
}
wg.Done()
return nil
}
g.Go(worker)
})
defer teardownServer()
wg.Add(2) // two servers, a gorilla and gobwas.
teardownClient1 := runTestClient("localhost:8080", clientEvents,
func(dialer string, client *neffos.Client) {
_, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
})
defer teardownClient1()
teardownClient2 := runTestClient("localhost:8080", clientEvents,
func(dialer string, client *neffos.Client) {
_, err := client.Connect(context.TODO(), namespace)
if err != nil {
t.Fatal(err)
}
})
defer teardownClient2()
wg.Wait()
if err := g.Wait(); err != nil {
t.Fatal(err)
}
}