-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
ws_flag_change_test.go
130 lines (125 loc) · 3.58 KB
/
ws_flag_change_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
package controller_test
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/service"
"github.com/thomaspoignant/go-feature-flag/internal/flag"
"github.com/thomaspoignant/go-feature-flag/notifier"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"go.uber.org/zap"
)
func Test_websocket_flag_change(t *testing.T) {
tests := []struct {
name string
flagChange notifier.DiffCache
}{
{
name: "Update 1 flag",
flagChange: notifier.DiffCache{
Deleted: nil,
Added: nil,
Updated: map[string]notifier.DiffUpdated{
"my-flag": {
Before: &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("A"),
},
},
After: &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("B"),
},
},
},
},
},
},
{
name: "Update remove and add flag at the same time",
flagChange: notifier.DiffCache{
Deleted: map[string]flag.Flag{
"flag-1": &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("A"),
},
},
},
Added: map[string]flag.Flag{
"flag-2": &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("A"),
},
},
},
Updated: map[string]notifier.DiffUpdated{
"my-flag": {
Before: &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("A"),
},
},
After: &flag.InternalFlag{
Variations: &map[string]*interface{}{
"A": testconvert.Interface(true),
"B": testconvert.Interface(false),
},
DefaultRule: &flag.Rule{
VariationResult: testconvert.String("B"),
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
websocketService := service.NewWebsocketService()
defer websocketService.Close()
log := zap.L()
ctrl := controller.NewWsFlagChange(websocketService, log)
e := echo.New()
e.GET("/ws/v1/flag/change", ctrl.Handler)
testServer := httptest.NewServer(e)
defer testServer.Close()
url := "ws" + strings.TrimPrefix(testServer.URL, "http") + "/ws/v1/flag/change"
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
t.Fatalf("Failed to connect to WebSocket: %v", err)
}
defer func() { _ = ws.Close() }()
websocketService.BroadcastFlagChanges(tt.flagChange)
_, receivedMessage, err := ws.ReadMessage()
assert.NoError(t, err)
expectedMessage, err := json.Marshal(tt.flagChange)
assert.NoError(t, err)
assert.JSONEq(t, string(expectedMessage), string(receivedMessage))
})
}
}