-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
174 lines (154 loc) · 4 KB
/
server.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
package test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
rpc2 "github.com/cisco-pxgrid/cloud-sdk-go/internal/rpc"
"github.com/cisco-pxgrid/websocket"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
type Config struct {
PubSubPath string
SubscriptionsPath string
RejectConn bool
PublishError bool
ConsumeError bool
ConsumeDrop bool
}
type sub struct {
stream string
id string
params []rpc2.PublishParams
}
func (s *sub) String() string {
return fmt.Sprintf("sub{stream:%s, id:%s, params:%+v}", s.stream, s.id, s.params)
}
var subs = map[string]*sub{}
var subsMu = sync.Mutex{}
// NewRPCServer creates and starts a test HTTP server that talks RPC
func NewRPCServer(t *testing.T, cfg Config) *httptest.Server {
r := chi.NewRouter()
// pubsub
r.Get(cfg.PubSubPath, func(w http.ResponseWriter, r *http.Request) {
if cfg.RejectConn {
w.WriteHeader(http.StatusInternalServerError)
return
}
c, err := websocket.Accept(w, r, nil)
assert.NoError(t, err)
defer c.Close(websocket.StatusNormalClosure, "")
ctx := context.Background()
for {
mt, payload, err := c.Read(ctx)
if err != nil {
if websocket.CloseStatus(err) != websocket.StatusNormalClosure {
t.Errorf("Read error: %v", err)
}
break
}
req, err := rpc2.NewRequestFromBytes(payload)
assert.NoError(t, err)
var resp *rpc2.Response
switch req.Method {
case rpc2.MethodOpen, rpc2.MethodClose:
resp = rpc2.NewControlResponse(req.ID, true, rpc2.Error{})
case rpc2.MethodPublish:
params, _ := req.PublishParams()
if cfg.PublishError {
resp = rpc2.NewErrorResponse(req.ID, fmt.Errorf("Publish Error"))
} else {
for _, p := range params {
subsMu.Lock()
s := subs[p.Stream]
subsMu.Unlock()
if s != nil {
s.params = append(s.params, *p)
}
}
resp = rpc2.NewPublishResponse(req.ID, params[0].MsgID, nil)
}
case rpc2.MethodConsume:
params, _ := req.ConsumeParams()
if cfg.ConsumeError {
resp = rpc2.NewErrorResponse(req.ID, fmt.Errorf("Consume Error"))
} else if cfg.ConsumeDrop {
resp = nil
} else {
subsMu.Lock()
for stream, sub := range subs {
if sub.id != params.SubscriptionID {
continue
}
msgs := make([]rpc2.ConsumeMessage, 0)
for _, p := range sub.params {
if p.MsgID == "" {
continue
}
msgs = append(msgs, rpc2.ConsumeMessage{
MsgID: p.MsgID,
Payload: p.Payload,
Headers: p.Headers,
})
}
// reset the params slice
sub.params = make([]rpc2.PublishParams, 0)
resp = rpc2.NewConsumeResponse(req.ID, "", sub.id, stream, msgs)
}
subsMu.Unlock()
}
}
if resp != nil {
err = c.Write(ctx, mt, resp.Bytes())
assert.NoError(t, err)
}
}
})
// subscriptions
r.Route(cfg.SubscriptionsPath, func(r chi.Router) {
// new subscription
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
body, err := io.ReadAll(r.Body)
assert.NoError(t, err)
var req struct {
GroupID string `json:"group_id"`
Streams []string `json:"streams"`
}
_ = json.Unmarshal(body, &req)
t.Logf("Received new subscription request: %+v", req)
id := uuid.NewString()
subsMu.Lock()
subs[req.Streams[0]] = &sub{
stream: req.Streams[0],
id: id,
}
subsMu.Unlock()
resp := struct {
ID string `json:"_id"`
}{ID: id}
err = json.NewEncoder(w).Encode(resp)
assert.NoError(t, err)
})
// delete subscription
r.Delete("/{id}", func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
t.Logf("Got delete subscription request: %v", id)
subsMu.Lock()
for stream, s := range subs {
if id == s.id {
delete(subs, stream)
}
}
subsMu.Unlock()
w.WriteHeader(http.StatusNoContent)
})
})
return httptest.NewTLSServer(r)
}