-
Notifications
You must be signed in to change notification settings - Fork 5
/
watcher_test.go
232 lines (188 loc) · 5.76 KB
/
watcher_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package watcher
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/casbin/casbin"
gnatsd "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
// Enable inmemory and NATS drivers
_ "github.com/rusenask/casbin-go-cloud-watcher/drivers/mempubsub"
_ "github.com/rusenask/casbin-go-cloud-watcher/drivers/natspubsub"
)
func TestNATSWatcher(t *testing.T) {
// Setup nats server
s := gnatsd.RunDefaultServer()
defer s.Shutdown()
natsEndpoint := fmt.Sprintf("nats://localhost:%d", nats.DefaultPort)
natsSubject := "nats://casbin-policy-updated-subject"
updaterCh := make(chan string, 1)
listenerCh := make(chan string, 1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// gocloud.dev connects to NATS server based on env variable
os.Setenv("NATS_SERVER_URL", natsEndpoint)
// updater represents the Casbin enforcer instance that changes the policy in DB
// Use the endpoint of nats as parameter.
updater, err := New(ctx, natsSubject)
if err != nil {
t.Fatalf("Failed to create updater, error: %s", err)
}
defer updater.Close()
updater.SetUpdateCallback(func(msg string) {
updaterCh <- "updater"
})
// listener represents any other Casbin enforcer instance that watches the change of policy in DB
listener, err := New(ctx, natsSubject)
if err != nil {
t.Fatalf("Failed to create second listener: %s", err)
}
defer listener.Close()
// listener should set a callback that gets called when policy changes
err = listener.SetUpdateCallback(func(msg string) {
listenerCh <- "listener"
})
if err != nil {
t.Fatalf("Failed to set listener callback: %s", err)
}
// updater changes the policy, and sends the notifications.
err = updater.Update()
if err != nil {
t.Fatalf("The updater failed to send Update: %s", err)
}
// Validate that listener received message
var updaterReceived bool
var listenerReceived bool
for {
select {
case res := <-listenerCh:
if res != "listener" {
t.Fatalf("Message from unknown source: %v", res)
break
}
listenerReceived = true
case res := <-updaterCh:
if res != "updater" {
t.Fatalf("Message from unknown source: %v", res)
break
}
updaterReceived = true
case <-time.After(time.Second * 10):
t.Fatal("Updater or listener didn't received message in time")
break
}
if updaterReceived && listenerReceived {
close(listenerCh)
close(updaterCh)
break
}
}
}
func TestWithEnforcerNATS(t *testing.T) {
// Setup nats server
s := gnatsd.RunDefaultServer()
defer s.Shutdown()
natsEndpoint := fmt.Sprintf("nats://localhost:%d", nats.DefaultPort)
natsSubject := "nats://casbin-policy-updated-subject"
cannel := make(chan string, 1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// gocloud.dev connects to NATS server based on env variable
os.Setenv("NATS_SERVER_URL", natsEndpoint)
w, err := New(ctx, natsSubject)
if err != nil {
t.Fatalf("Failed to create updater, error: %s", err)
}
defer w.Close()
// Initialize the enforcer.
e := casbin.NewEnforcer("./test_data/model.conf", "./test_data/policy.csv")
// Set the watcher for the enforcer.
e.SetWatcher(w)
// By default, the watcher's callback is automatically set to the
// enforcer's LoadPolicy() in the SetWatcher() call.
// We can change it by explicitly setting a callback.
w.SetUpdateCallback(func(msg string) {
cannel <- "enforcer"
})
// Update the policy to test the effect.
e.SavePolicy()
// Validate that listener received message
select {
case res := <-cannel:
if res != "enforcer" {
t.Fatalf("Got unexpected message :%v", res)
}
case <-time.After(time.Second * 10):
t.Fatal("The enforcer didn't send message in time")
}
close(cannel)
}
func TestWithEnforcerMemory(t *testing.T) {
endpointURL := "mem://topicA"
cannel := make(chan string, 1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
w, err := New(ctx, endpointURL)
if err != nil {
t.Fatalf("Failed to create updater, error: %s", err)
}
defer w.Close()
// Initialize the enforcer.
e := casbin.NewEnforcer("./test_data/model.conf", "./test_data/policy.csv")
// Set the watcher for the enforcer.
e.SetWatcher(w)
// By default, the watcher's callback is automatically set to the
// enforcer's LoadPolicy() in the SetWatcher() call.
// We can change it by explicitly setting a callback.
w.SetUpdateCallback(func(msg string) {
cannel <- "enforcer"
})
// Update the policy to test the effect.
e.SavePolicy()
// Validate that listener received message
select {
case res := <-cannel:
if res != "enforcer" {
t.Fatalf("Got unexpected message :%v", res)
}
case <-time.After(time.Second * 5):
t.Fatal("The enforcer didn't send message in time")
}
close(cannel)
}
// Ensure that we can still use the same topic name
func TestWithEnforcerMemoryB(t *testing.T) {
endpointURL := "mem://topicA"
cannel := make(chan string, 1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
w, err := New(ctx, endpointURL)
if err != nil {
t.Fatalf("Failed to create updater, error: %s", err)
}
defer w.Close()
// Initialize the enforcer.
e := casbin.NewEnforcer("./test_data/model.conf", "./test_data/policy.csv")
// Set the watcher for the enforcer.
e.SetWatcher(w)
// By default, the watcher's callback is automatically set to the
// enforcer's LoadPolicy() in the SetWatcher() call.
// We can change it by explicitly setting a callback.
w.SetUpdateCallback(func(msg string) {
cannel <- "enforcer"
})
// Update the policy to test the effect.
e.SavePolicy()
// Validate that listener received message
select {
case res := <-cannel:
if res != "enforcer" {
t.Fatalf("Got unexpected message :%v", res)
}
case <-time.After(time.Second * 5):
t.Fatal("The enforcer didn't send message in time")
}
close(cannel)
}