-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathchanges_test.go
394 lines (318 loc) · 15 KB
/
changes_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReadChangesOptionsFromJSON(t *testing.T) {
ctx := base.TestCtx(t)
h := &handler{}
h.server = NewServerContext(ctx, &StartupConfig{}, false)
defer h.server.Close(ctx)
// Basic case, no heartbeat, no timeout
optStr := `{"feed":"longpoll", "since": "123456:78", "limit":123, "style": "all_docs",
"include_docs": true, "filter": "Melitta", "channels": "ABC,BBC"}`
feed, options, filter, channelsArray, _, _, err := h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, "longpoll", feed)
assert.Equal(t, uint64(78), options.Since.Seq)
assert.Equal(t, uint64(123456), options.Since.TriggeredBy)
assert.Equal(t, 123, options.Limit)
assert.Equal(t, true, options.Conflicts)
assert.Equal(t, true, options.IncludeDocs)
assert.Equal(t, uint64(kDefaultHeartbeatMS), options.HeartbeatMs)
assert.Equal(t, uint64(kDefaultTimeoutMS), options.TimeoutMs)
assert.Equal(t, "Melitta", filter)
assert.Equal(t, []string{"ABC", "BBC"}, channelsArray)
// Attempt to set heartbeat, timeout to valid values
optStr = `{"feed":"longpoll", "since": "1", "heartbeat":30000, "timeout":60000}`
_, options, _, _, _, _, err = h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, uint64(30000), options.HeartbeatMs)
assert.Equal(t, uint64(60000), options.TimeoutMs)
// Attempt to set valid timeout, no heartbeat
optStr = `{"feed":"longpoll", "since": "1", "timeout":2000}`
_, options, _, _, _, _, err = h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, uint64(2000), options.TimeoutMs)
// Disable heartbeat, timeout by explicitly setting to zero
optStr = `{"feed":"longpoll", "since": "1", "heartbeat":0, "timeout":0}`
_, options, _, _, _, _, err = h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, uint64(0), options.HeartbeatMs)
assert.Equal(t, uint64(0), options.TimeoutMs)
// Attempt to set heartbeat less than minimum heartbeat, timeout greater than max timeout
optStr = `{"feed":"longpoll", "since": "1", "heartbeat":1000, "timeout":1000000}`
_, options, _, _, _, _, err = h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, uint64(kMinHeartbeatMS), options.HeartbeatMs)
assert.Equal(t, uint64(kMaxTimeoutMS), options.TimeoutMs)
// Set max heartbeat in server context, attempt to set heartbeat greater than max
h.server.Config.Replicator.MaxHeartbeat = base.NewConfigDuration(time.Minute)
optStr = `{"feed":"longpoll", "since": "1", "heartbeat":90000}`
_, options, _, _, _, _, err = h.readChangesOptionsFromJSON([]byte(optStr))
assert.NoError(t, err)
assert.Equal(t, uint64(60000), options.HeartbeatMs)
}
// Test for wrong _changes entries for user joining a populated channel
func TestUserJoiningPopulatedChannel(t *testing.T) {
base.SetUpTestLogging(t, base.LevelInfo, base.KeyCache, base.KeyAccess, base.KeyCRUD, base.KeyChanges)
rtConfig := RestTesterConfig{
SyncFn: `function(doc) {channel(doc.channels)}`,
}
rt := NewRestTester(t, &rtConfig)
defer rt.Close()
ctx := rt.Context()
a := rt.ServerContext().Database(ctx, "db").Authenticator(ctx)
guest, err := a.GetUser("")
assert.NoError(t, err)
guest.SetDisabled(false)
assert.NoError(t, a.Save(guest))
rt.CreateUser("user1", []string{"alpha"})
// Create 100 docs
for i := 0; i < 100; i++ {
docpath := fmt.Sprintf("/{{.keyspace}}/doc%d", i)
RequireStatus(t, rt.SendRequest("PUT", docpath, `{"foo": "bar", "channels":["alpha"]}`), 201)
}
limit := 50
changesResults := rt.WaitForChanges(50, fmt.Sprintf("/{{.keyspace}}/_changes?limit=%d", limit), "user1", false)
since := changesResults.Results[49].Seq
assert.Equal(t, "doc48", changesResults.Results[49].ID)
// // Check the _changes feed with since and limit, to get second half of feed
changesResults = rt.WaitForChanges(50, fmt.Sprintf("/{{.keyspace}}/_changes?since=\"%s\"&limit=%d", since, limit), "user1", false)
assert.Equal(t, "doc98", changesResults.Results[49].ID)
rt.CreateUser("user2", []string{"alpha"})
// Retrieve all changes for user2 with no limits
changesResults = rt.WaitForChanges(101, fmt.Sprintf("/{{.keyspace}}/_changes"), "user2", false)
assert.Equal(t, "doc99", changesResults.Results[99].ID)
rt.CreateUser("user3", []string{"alpha"})
getUserResponse := rt.SendAdminRequest("GET", "/db/_user/user3", "")
RequireStatus(t, getUserResponse, 200)
log.Printf("create user response: %s", getUserResponse.Body.Bytes())
// Get the sequence from the user doc to validate against the triggered by value in the changes results
user3, _ := rt.GetDatabase().Authenticator(base.TestCtx(t)).GetUser("user3")
userSequence := user3.Sequence()
// Get first 50 document changes.
changesResults = rt.WaitForChanges(50, fmt.Sprintf("/{{.keyspace}}/_changes?limit=%d", limit), "user3", false)
since = changesResults.Results[49].Seq
assert.Equal(t, "doc49", changesResults.Results[49].ID)
assert.Equal(t, userSequence, since.TriggeredBy)
// // Get remainder of changes i.e. no limit parameter
changesResults = rt.WaitForChanges(51, fmt.Sprintf("/{{.keyspace}}/_changes?since=\"%s\"", since), "user3", false)
assert.Equal(t, "doc99", changesResults.Results[49].ID)
rt.CreateUser("user4", []string{"alpha"})
// Get the sequence from the user doc to validate against the triggered by value in the changes results
user4, err := rt.GetDatabase().Authenticator(base.TestCtx(t)).GetUser("user4")
require.NoError(t, err)
user4Sequence := user4.Sequence()
changesResults = rt.WaitForChanges(50, fmt.Sprintf("/{{.keyspace}}/_changes?limit=%d", limit), "user4", false)
since = changesResults.Results[49].Seq
assert.Equal(t, "doc49", changesResults.Results[49].ID)
assert.Equal(t, user4Sequence, since.TriggeredBy)
// // Check the _changes feed with since and limit, to get second half of feed
changesResults = rt.WaitForChanges(50, fmt.Sprintf("/{{.keyspace}}/_changes?since=%s&limit=%d", since, limit), "user4", false)
assert.Equal(t, "doc99", changesResults.Results[49].ID)
}
// TestWebhookWinningRevChangedEvent ensures the winning_rev_changed event is only fired for a winning revision change, and checks that document_changed is always fired.
func TestWebhookWinningRevChangedEvent(t *testing.T) {
base.SetUpTestLogging(t, base.LevelDebug, base.KeyHTTP, base.KeyEvents)
wg := sync.WaitGroup{}
var WinningRevChangedCount uint32
var DocumentChangedCount uint32
handler := func(w http.ResponseWriter, r *http.Request) {
var body db.Body
d := base.JSONDecoder(r.Body)
require.NoError(t, d.Decode(&body))
require.Contains(t, body, db.BodyId)
require.Contains(t, body, db.BodyRev)
event := r.URL.Query().Get("event")
switch event {
case "WinningRevChanged":
atomic.AddUint32(&WinningRevChangedCount, 1)
case "DocumentChanged":
atomic.AddUint32(&DocumentChangedCount, 1)
default:
t.Fatalf("unknown event type: %s", event)
}
wg.Done()
}
s := httptest.NewServer(http.HandlerFunc(handler))
defer s.Close()
rtConfig := &RestTesterConfig{
DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{
EventHandlers: &EventHandlerConfig{
DocumentChanged: []*EventConfig{
{Url: s.URL + "?event=DocumentChanged", Filter: "function(doc){return true;}", HandlerType: "webhook"},
{Url: s.URL + "?event=WinningRevChanged", Filter: "function(doc){return true;}", HandlerType: "webhook",
Options: map[string]interface{}{db.EventOptionDocumentChangedWinningRevOnly: true},
},
},
},
AllowConflicts: base.Ptr(true),
},
}}
rt := NewRestTester(t, rtConfig)
defer rt.Close()
wg.Add(2)
const docID = "doc1"
version1 := rt.PutDoc(docID, `{"foo":"bar"}`)
// push winning branch
wg.Add(2)
res := rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc1?new_edits=false", `{"foo":"buzz","_revisions":{"start":3,"ids":["buzz","bar","`+version1.RevID+`"]}}`)
RequireStatus(t, res, http.StatusCreated)
winningVersion := DocVersionFromPutResponse(t, res)
// push non-winning branch
wg.Add(1)
_ = rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("2-buzzzzz"), &version1, `{"foo":"buzzzzz"}`)
RequireStatus(t, res, http.StatusCreated)
wg.Wait()
assert.Equal(t, 2, int(atomic.LoadUint32(&WinningRevChangedCount)))
assert.Equal(t, 3, int(atomic.LoadUint32(&DocumentChangedCount)))
// tombstone the winning branch and ensure we get a rev changed message for the promoted branch
wg.Add(2)
rt.DeleteDoc(docID, winningVersion)
wg.Wait()
assert.Equal(t, 3, int(atomic.LoadUint32(&WinningRevChangedCount)))
assert.Equal(t, 4, int(atomic.LoadUint32(&DocumentChangedCount)))
// push a separate winning branch
wg.Add(2)
res = rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc1?new_edits=false", `{"foo":"quux","_revisions":{"start":4,"ids":["quux", "buzz","bar","`+version1.RevID+`"]}}`)
RequireStatus(t, res, http.StatusCreated)
newWinningVersion := DocVersionFromPutResponse(t, res)
// tombstone the winning branch, we should get a second webhook fired for rev 2-buzzzzz now it's been resurrected
wg.Add(2)
rt.DeleteDoc(docID, newWinningVersion)
wg.Wait()
assert.Equal(t, 5, int(atomic.LoadUint32(&WinningRevChangedCount)))
assert.Equal(t, 6, int(atomic.LoadUint32(&DocumentChangedCount)))
}
// TestJumpInSequencesAtAllocatorSkippedSequenceFill:
// - High level test
// - Add a doc through Sync Gateway
// - Alter that allocated sequence to be higher value. Mocking this document arriving from different env
// (e.g. via XDCR)
// - Wait for this sequence to arrive over cache feed and wait for skipped sequences to subsequently fill
// - Update this doc again, triggering unused sequence range release
// - Write another doc and assert that the changes feed returns all expected docs
func TestJumpInSequencesAtAllocatorSkippedSequenceFill(t *testing.T) {
if !base.TestUseXattrs() {
t.Skip("This test requires xattrs because it writes directly to the xattr")
}
base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
rt := NewRestTester(t, &RestTesterConfig{
DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{
AutoImport: false,
CacheConfig: &CacheConfig{
ChannelCacheConfig: &ChannelCacheConfig{
MaxWaitPending: base.Ptr(uint32(10)),
},
},
}},
})
defer rt.Close()
ctx := base.TestCtx(t)
vrs := rt.PutDoc("doc", `{"prop":true}`)
resp := rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/_changes", "")
RequireStatus(t, resp, http.StatusOK)
ds := rt.GetSingleDataStore()
xattrs, cas, err := ds.GetXattrs(ctx, "doc", []string{base.SyncXattrName})
require.NoError(t, err)
var retrievedXattr map[string]interface{}
require.NoError(t, base.JSONUnmarshal(xattrs[base.SyncXattrName], &retrievedXattr))
retrievedXattr["sequence"] = uint64(20)
newXattrVal := map[string][]byte{
base.SyncXattrName: base.MustJSONMarshal(t, retrievedXattr),
}
_, err = ds.UpdateXattrs(ctx, "doc", 0, cas, newXattrVal, nil)
require.NoError(t, err)
// wait for value to move from pending to cache and skipped list to fill
require.EventuallyWithT(t, func(c *assert.CollectT) {
rt.GetDatabase().UpdateCalculatedStats(ctx)
assert.Equal(c, int64(1), rt.GetDatabase().DbStats.CacheStats.SkippedSeqLen.Value())
}, time.Second*10, time.Millisecond*100)
docVrs := rt.UpdateDoc("doc", vrs, `{"prob": "lol"}`)
// wait skipped list to be emptied by release of sequence range
require.EventuallyWithT(t, func(c *assert.CollectT) {
rt.GetDatabase().UpdateCalculatedStats(ctx)
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.PendingSeqLen.Value())
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.NumCurrentSeqsSkipped.Value())
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.SkippedSeqLen.Value())
}, time.Second*10, time.Millisecond*100)
doc1Vrs := rt.PutDoc("doc1", `{"prop":true}`)
changes := rt.WaitForChanges(2, "/{{.keyspace}}/_changes", "", true)
changes.RequireDocIDs(t, []string{"doc1", "doc"})
changes.RequireRevID(t, []string{docVrs.RevID, doc1Vrs.RevID})
}
// TestJumpInSequencesAtAllocatorRangeInPending:
// - High level test
// - Add a doc through Sync Gateway
// - Alter that allocated sequence to be higher value. Mocking this document arriving from different env
// (e.g. via XDCR)
// - Wait for this sequence to arrive over cache feed and subsequently pushed to pending
// - Update this doc again, triggering unused sequence range release
// - Write another doc and assert that the changes feed returns all expected docs
func TestJumpInSequencesAtAllocatorRangeInPending(t *testing.T) {
if !base.TestUseXattrs() {
t.Skip("This test requires xattrs because it writes directly to the xattr")
}
base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
rt := NewRestTester(t, &RestTesterConfig{
DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{
AutoImport: false,
CacheConfig: &CacheConfig{
ChannelCacheConfig: &ChannelCacheConfig{
MaxWaitPending: base.Ptr(uint32(1500)),
},
},
}},
})
defer rt.Close()
ctx := base.TestCtx(t)
vrs := rt.PutDoc("doc", `{"prop":true}`)
resp := rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/_changes", "")
RequireStatus(t, resp, http.StatusOK)
ds := rt.GetSingleDataStore()
xattrs, cas, err := ds.GetXattrs(ctx, "doc", []string{base.SyncXattrName})
require.NoError(t, err)
var retrievedXattr map[string]interface{}
require.NoError(t, base.JSONUnmarshal(xattrs[base.SyncXattrName], &retrievedXattr))
retrievedXattr["sequence"] = uint64(20)
newXattrVal := map[string][]byte{
base.SyncXattrName: base.MustJSONMarshal(t, retrievedXattr),
}
_, err = ds.UpdateXattrs(ctx, "doc", 0, cas, newXattrVal, nil)
require.NoError(t, err)
// wait for value top be added to pending
require.EventuallyWithT(t, func(c *assert.CollectT) {
rt.GetDatabase().UpdateCalculatedStats(ctx)
assert.Equal(c, int64(1), rt.GetDatabase().DbStats.CacheStats.PendingSeqLen.Value())
}, time.Second*10, time.Millisecond*100)
docVrs := rt.UpdateDoc("doc", vrs, `{"prob": "lol"}`)
// assert that nothing has been pushed to skipped
require.EventuallyWithT(t, func(c *assert.CollectT) {
rt.GetDatabase().UpdateCalculatedStats(ctx)
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.SkippedSeqCap.Value())
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.NumCurrentSeqsSkipped.Value())
assert.Equal(c, int64(0), rt.GetDatabase().DbStats.CacheStats.SkippedSeqLen.Value())
}, time.Second*10, time.Millisecond*100)
doc1Vrs := rt.PutDoc("doc1", `{"prop":true}`)
changes := rt.WaitForChanges(2, "/{{.keyspace}}/_changes", "", true)
changes.RequireDocIDs(t, []string{"doc1", "doc"})
changes.RequireRevID(t, []string{docVrs.RevID, doc1Vrs.RevID})
}