-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathemulation_integration_test.go
174 lines (145 loc) · 4.02 KB
/
emulation_integration_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
//go:build integration
package centrifuge
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/centrifugal/protocol"
"github.com/stretchr/testify/require"
)
func TestEmulation_DifferentNodes(t *testing.T) {
t.Parallel()
n1, _ := New(Config{
LogLevel: LogLevelDebug,
})
n1.OnConnecting(func(ctx context.Context, event ConnectEvent) (ConnectReply, error) {
return ConnectReply{Credentials: &Credentials{
UserID: "test",
}}, nil
})
n1.OnConnect(func(client *Client) {
client.OnRPC(func(event RPCEvent, callback RPCCallback) {
callback(RPCReply{
Data: event.Data,
}, nil)
})
})
redisConf := testSingleRedisConf(6379)
s, err := NewRedisShard(n1, redisConf)
require.NoError(t, err)
prefix := getUniquePrefix()
b1, _ := NewRedisBroker(n1, RedisBrokerConfig{
Prefix: prefix,
Shards: []*RedisShard{s},
})
defer stopRedisBroker(b1)
n1.SetBroker(b1)
require.NoError(t, n1.Run())
defer func() { _ = n1.Shutdown(context.Background()) }()
mux1 := http.NewServeMux()
mux1.Handle("/connection/http_stream", NewHTTPStreamHandler(n1, HTTPStreamConfig{}))
server1 := httptest.NewServer(mux1)
defer server1.Close()
n2, _ := New(Config{
LogLevel: LogLevelDebug,
})
s2, err := NewRedisShard(n2, redisConf)
require.NoError(t, err)
b2, _ := NewRedisBroker(n2, RedisBrokerConfig{
Prefix: prefix,
Shards: []*RedisShard{s2},
})
n2.SetBroker(b2)
require.NoError(t, n2.Run())
defer func() { _ = n2.Shutdown(context.Background()) }()
defer stopRedisBroker(b2)
mux2 := http.NewServeMux()
mux2.Handle("/emulation", NewEmulationHandler(n2, EmulationConfig{}))
server2 := httptest.NewServer(mux2)
defer server2.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
for {
info, err := n2.Info()
require.NoError(t, err)
if len(info.Nodes) == 2 {
break
}
select {
case <-ctx.Done():
t.Fatal("timeout waiting for two nodes running")
case <-time.After(time.Second):
}
}
client := &http.Client{Timeout: 5 * time.Second}
command := &protocol.Command{
Id: 1,
Connect: &protocol.ConnectRequest{},
}
jsonData, err := json.Marshal(command)
require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, server1.URL+"/connection/http_stream", bytes.NewBuffer(jsonData))
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
defer func() { _ = resp.Body.Close() }()
var node string
var session string
dec := newJSONStreamDecoder(resp.Body)
for {
msg, err := dec.decode()
require.NoError(t, err)
var reply protocol.Reply
err = json.Unmarshal(msg, &reply)
require.NoError(t, err)
require.NotNil(t, reply.Connect)
require.Equal(t, uint32(1), reply.Id)
require.NotZero(t, reply.Connect.Session)
require.NotZero(t, reply.Connect.Node)
node = reply.Connect.Node
session = reply.Connect.Session
break
}
// Send emulation request with RPC command.
command = &protocol.Command{
Id: 2,
Rpc: &protocol.RPCRequest{
Data: []byte(`{"data":"emulation_works_fine"}`),
},
}
jsonData, err = json.Marshal(command)
require.NoError(t, err)
jsonData2, err := json.Marshal(string(jsonData))
require.NoError(t, err)
emuRequest := &protocol.EmulationRequest{
Node: node,
Session: session,
Data: jsonData2,
}
jsonEmuRequest, err := json.Marshal(emuRequest)
require.NoError(t, err)
req, err = http.NewRequest(http.MethodPost, server2.URL+"/emulation", bytes.NewBuffer(jsonEmuRequest))
require.NoError(t, err)
resp2, err := client.Do(req)
require.NoError(t, err)
require.Equal(t, http.StatusNoContent, resp2.StatusCode)
defer func() { _ = resp2.Body.Close() }()
// Wait for RPC reply.
for {
msg, err := dec.decode()
require.NoError(t, err)
var reply protocol.Reply
err = json.Unmarshal(msg, &reply)
require.NoError(t, err)
if reply.Rpc != nil {
require.Equal(t, uint32(2), reply.Id)
require.Equal(t, []byte(`{"data":"emulation_works_fine"}`), []byte(reply.Rpc.Data))
break
}
}
}