-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathutil_testing.go
700 lines (614 loc) · 23 KB
/
util_testing.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
Copyright 2018-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 db
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/couchbase/go-blip"
sgbucket "github.com/couchbase/sg-bucket"
"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/channels"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func (db *DatabaseContext) CacheCompactActive() bool {
channelCache := db.changeCache.getChannelCache()
compactingCache, ok := channelCache.(*channelCacheImpl)
if !ok {
return false
}
return compactingCache.isCompactActive()
}
func (db *DatabaseContext) WaitForCaughtUp(targetCount int64) error {
for i := 0; i < 100; i++ {
// caughtUpCount := base.ExpvarVar2Int(db.DbStats.StatsCblReplicationPull().Get(base.StatKeyPullReplicationsCaughtUp))
caughtUpCount := db.DbStats.CBLReplicationPull().NumPullReplCaughtUp.Value()
if caughtUpCount >= targetCount {
return nil
}
time.Sleep(100 * time.Millisecond)
}
return errors.New("WaitForCaughtUp didn't catch up")
}
func (db *DatabaseContext) WaitForTotalCaughtUp(targetCount int64) error {
for i := 0; i < 100; i++ {
caughtUpCount := db.DbStats.CBLReplicationPull().NumPullReplTotalCaughtUp.Value()
if caughtUpCount >= targetCount {
return nil
}
time.Sleep(100 * time.Millisecond)
}
return errors.New("WaitForCaughtUp didn't catch up")
}
type StatWaiter struct {
initCount int64 // Document cached count when NewStatWaiter is called
targetCount int64 // Target count used when Wait is called
stat *base.SgwIntStat // Stat to wait on
tb testing.TB // Raises tb.Fatalf on wait timeout
}
func (db *DatabaseContext) NewStatWaiter(stat *base.SgwIntStat, tb testing.TB) *StatWaiter {
return &StatWaiter{
initCount: stat.Value(),
targetCount: stat.Value(),
stat: stat,
tb: tb,
}
}
func (db *DatabaseContext) NewDCPCachingCountWaiter(tb testing.TB) *StatWaiter {
return db.NewStatWaiter(db.DbStats.Database().DCPCachingCount, tb)
}
func (db *DatabaseContext) NewPullReplicationCaughtUpWaiter(tb testing.TB) *StatWaiter {
return db.NewStatWaiter(db.DbStats.CBLReplicationPull().NumPullReplCaughtUp, tb)
}
func (db *DatabaseContext) NewCacheRevsActiveWaiter(tb testing.TB) *StatWaiter {
return db.NewStatWaiter(db.DbStats.Cache().ChannelCacheRevsActive, tb)
}
func (sw *StatWaiter) Add(count int) {
sw.targetCount += int64(count)
}
func (sw *StatWaiter) AddAndWait(count int) {
sw.targetCount += int64(count)
sw.Wait()
}
// Wait uses backoff retry for up to ~33s
func (sw *StatWaiter) Wait() {
actualCount := sw.stat.Value()
if actualCount >= sw.targetCount {
return
}
waitTime := 1 * time.Millisecond
for i := 0; i < 14; i++ {
waitTime *= 2
time.Sleep(waitTime)
actualCount = sw.stat.Value()
if actualCount >= sw.targetCount {
return
}
}
sw.tb.Fatalf("StatWaiter.Wait timed out waiting for stat to reach %d (actual: %d) %s", sw.targetCount, actualCount, base.GetCallersName(2, true))
}
func AssertEqualBodies(t *testing.T, expected, actual Body) {
expectedCanonical, err := base.JSONMarshalCanonical(expected)
assert.NoError(t, err)
actualCanonical, err := base.JSONMarshalCanonical(actual)
assert.NoError(t, err)
assert.Equal(t, string(expectedCanonical), string(actualCanonical))
}
func WaitForUserWaiterChange(userWaiter *ChangeWaiter) bool {
var isChanged bool
for i := 0; i < 100; i++ {
isChanged = userWaiter.RefreshUserCount()
if isChanged {
break
}
time.Sleep(10 * time.Millisecond)
}
return isChanged
}
// purgeWithDCPFeed purges all documents seen on a DCP feed with system xattrs, including tombstones which aren't found when emptying the primary index.
func purgeWithDCPFeed(ctx context.Context, dataStore sgbucket.DataStore, tbp *base.TestBucketPool) (numCompacted int, err error) {
purgeTimeout := 60 * time.Second
purgeBody := Body{"_purged": true}
var processedDocCount atomic.Int64
var purgedDocCount atomic.Int64
var purgeErrors *base.MultiError
collection, err := base.AsCollection(dataStore)
if err != nil {
return 0, fmt.Errorf("dataStore was not a gocb collection: %w", err)
}
var collectionIDs []uint32
if collection.IsSupported(sgbucket.BucketStoreFeatureCollections) {
collectionIDs = append(collectionIDs, collection.GetCollectionID())
}
dcpClientOpts := base.DCPClientOptions{
OneShot: true,
FailOnRollback: false,
CollectionIDs: collectionIDs,
MetadataStoreType: base.DCPMetadataStoreInMemory,
}
purgeCallback := func(event sgbucket.FeedEvent) bool {
var purgeErr error
processedDocCount.Add(1)
// We only need to purge mutations/deletions
if event.Opcode != sgbucket.FeedOpMutation && event.Opcode != sgbucket.FeedOpDeletion {
return false
}
// If it's a deletion but doesn't have xattrs, ignore it
if event.Opcode == sgbucket.FeedOpDeletion && event.DataType&base.MemcachedDataTypeXattr == 0 {
return false
}
key := string(event.Key)
if base.TestUseXattrs() {
purgeErr = dataStore.DeleteWithXattrs(ctx, key, []string{base.SyncXattrName})
} else {
purgeErr = dataStore.Delete(key)
}
if base.IsDocNotFoundError(purgeErr) {
// If key no longer exists, need to add and remove to trigger removal from view
_, addErr := dataStore.Add(key, 0, purgeBody)
if addErr != nil {
purgeErrors = purgeErrors.Append(addErr)
tbp.Logf(ctx, "Error adding key %s to force deletion. %v", key, addErr)
return false
}
if delErr := dataStore.Delete(key); delErr != nil {
purgeErrors = purgeErrors.Append(delErr)
tbp.Logf(ctx, "Error deleting key %s. %v", key, delErr)
}
purgedDocCount.Add(1)
} else if purgeErr != nil {
purgeErrors = purgeErrors.Append(purgeErr)
tbp.Logf(ctx, "Error removing key %s (purge). %v", key, purgeErr)
}
return false
}
feedID := "purgeFeed-" + collection.CollectionName()
dcpClient, err := base.NewDCPClient(ctx, feedID, purgeCallback, dcpClientOpts, collection.Bucket)
if err != nil {
return 0, err
}
doneChan, err := dcpClient.Start()
if err != nil {
return 0, fmt.Errorf("error starting purge DCP feed: %w", err)
}
// wait for feed to complete
timeout := time.After(purgeTimeout)
select {
case err := <-doneChan:
if err != nil {
tbp.Logf(ctx, "purgeDCPFeed finished with error: %v", err)
}
case <-timeout:
return 0, fmt.Errorf("timeout waiting for purge DCP feed to complete")
}
closeErr := dcpClient.Close()
if closeErr != nil {
tbp.Logf(ctx, "error closing purge DCP feed: %v", closeErr)
}
tbp.Logf(ctx, "Finished purge DCP feed ... Total docs purged: %d", purgedDocCount.Load())
tbp.Logf(ctx, "Finished purge DCP feed ... Total docs processed: %d", processedDocCount.Load())
return int(purgedDocCount.Load()), purgeErrors.ErrorOrNil()
}
// viewsAndGSIBucketReadier empties the bucket, initializes Views, and waits until GSI indexes are empty. It is run asynchronously as soon as a test is finished with a bucket.
var viewsAndGSIBucketReadier base.TBPBucketReadierFunc = func(ctx context.Context, b base.Bucket, tbp *base.TestBucketPool) error {
if base.TestsDisableGSI() {
tbp.Logf(ctx, "flushing bucket and readying views")
if err := base.FlushBucketEmptierFunc(ctx, b, tbp); err != nil {
return err
}
// Exit early if we're not using GSI.
return viewBucketReadier(ctx, b.DefaultDataStore(), tbp)
}
tbp.Logf(ctx, "emptying bucket via DCP purge")
dataStores, err := b.ListDataStores()
if err != nil {
return err
}
for _, dataStoreName := range dataStores {
dataStore, err := b.NamedDataStore(dataStoreName)
if err != nil {
return err
}
if _, err := purgeWithDCPFeed(ctx, dataStore, tbp); err != nil {
return err
}
}
if len(dataStores) == 1 {
dataStoreName := dataStores[0]
if base.IsDefaultCollection(dataStoreName.ScopeName(), dataStoreName.CollectionName()) {
dataStore, err := b.NamedDataStore(dataStoreName)
if err != nil {
return err
}
tbp.Logf(ctx, "readying views for bucket")
if err := viewBucketReadier(ctx, dataStore, tbp); err != nil {
return err
}
}
}
return nil
}
// deleteDocsAndIndexesBucketReadier purges the datastore using DCP and drops any indexes on the bucket
var deleteDocsAndIndexesBucketReadier base.TBPBucketReadierFunc = func(ctx context.Context, b base.Bucket, tbp *base.TestBucketPool) error {
dataStores, err := b.ListDataStores()
if err != nil {
return err
}
for _, dataStoreName := range dataStores {
dataStore, err := b.NamedDataStore(dataStoreName)
if err != nil {
return err
}
if _, err := purgeWithDCPFeed(ctx, dataStore, tbp); err != nil {
return err
}
n1qlStore, ok := base.AsN1QLStore(dataStore)
if !ok {
return errors.New("attempting to empty indexes with non-N1QL store")
}
tbp.Logf(ctx, "dropping existing bucket indexes %s.%s.%s", b.GetName(), dataStore.ScopeName(), dataStore.CollectionName())
if err := base.DropAllIndexes(ctx, n1qlStore); err != nil {
tbp.Logf(ctx, "Failed to drop bucket indexes: %v", err)
return err
}
}
return nil
}
// viewsAndGSIBucketInit is run synchronously only once per-bucket to do any initial setup. For non-integration Walrus buckets, this is run for each new Walrus bucket.
var viewsAndGSIBucketInit base.TBPBucketInitFunc = func(ctx context.Context, b base.Bucket, tbp *base.TestBucketPool) error {
skipGSI := false
if base.TestsDisableGSI() {
tbp.Logf(ctx, "bucket not a gocb bucket... skipping GSI setup")
skipGSI = true
}
tbp.Logf(ctx, "Starting bucket init function")
dataStores, err := b.ListDataStores()
if err != nil {
return err
}
for _, dataStoreName := range dataStores {
ctx := base.KeyspaceLogCtx(ctx, b.GetName(), dataStoreName.ScopeName(), dataStoreName.CollectionName())
dataStore, err := b.NamedDataStore(dataStoreName)
if err != nil {
return err
}
// Views
if skipGSI || base.TestsDisableGSI() {
if err := viewBucketReadier(ctx, dataStore, tbp); err != nil {
return err
}
continue
}
// GSI
n1qlStore, ok := base.AsN1QLStore(dataStore)
if !ok {
return fmt.Errorf("bucket %T was not a N1QL store", b)
}
tbp.Logf(ctx, "dropping existing bucket indexes")
if err := base.DropAllIndexes(ctx, n1qlStore); err != nil {
tbp.Logf(ctx, "Failed to drop bucket indexes: %v", err)
return err
}
tbp.Logf(ctx, "creating SG bucket indexes")
options := InitializeIndexOptions{
UseXattrs: base.TestUseXattrs(),
NumReplicas: 0,
WaitForIndexesOnlineOption: base.WaitForIndexesDefault,
Serverless: false,
MetadataIndexes: IndexesWithoutMetadata,
}
if base.IsDefaultCollection(dataStore.ScopeName(), dataStore.CollectionName()) {
options.MetadataIndexes = IndexesAll
}
if err := InitializeIndexes(ctx, n1qlStore, options); err != nil {
return err
}
tbp.Logf(ctx, "finished creating SG bucket indexes")
}
return nil
}
// viewBucketReadier removes any existing views and installs a new set into the given bucket.
func viewBucketReadier(ctx context.Context, dataStore base.DataStore, tbp *base.TestBucketPool) error {
viewStore, ok := base.AsViewStore(dataStore)
if !ok {
return fmt.Errorf("dataStore %T was not a View store", dataStore)
}
ddocs, err := viewStore.GetDDocs()
if err != nil {
return err
}
for ddocName := range ddocs {
tbp.Logf(ctx, "removing existing view: %s", ddocName)
if err := viewStore.DeleteDDoc(ddocName); err != nil {
return err
}
}
tbp.Logf(ctx, "initializing bucket views")
err = InitializeViews(ctx, dataStore)
if err != nil {
return err
}
tbp.Logf(ctx, "bucket views initialized")
return nil
}
func (db *DatabaseContext) GetChannelQueryCount() int64 {
if db.UseViews() {
return db.DbStats.Query(fmt.Sprintf(base.StatViewFormat, DesignDocSyncGateway(), ViewChannels)).QueryCount.Value()
}
return db.DbStats.Query(QueryTypeChannels).QueryCount.Value()
}
// GetLocalActiveReplicatorForTest is a test util for retrieving an Active Replicator for deeper introspection/assertions.
func (m *sgReplicateManager) GetLocalActiveReplicatorForTest(t testing.TB, replicationID string) (ar *ActiveReplicator, ok bool) {
// Check if replication is assigned locally
m.activeReplicatorsLock.RLock()
replication, isLocal := m.activeReplicators[replicationID]
m.activeReplicatorsLock.RUnlock()
return replication, isLocal
}
// SuspendSequenceBatching disables sequence batching for multi-RT tests (pending CBG-1000)
func SuspendSequenceBatching() func() {
oldFrequency := MaxSequenceIncrFrequency
MaxSequenceIncrFrequency = 0 * time.Millisecond
return func() { MaxSequenceIncrFrequency = oldFrequency }
}
// Public channel view call - for unit test support
func (dbc *DatabaseContext) ChannelViewForTest(tb testing.TB, channelName string, startSeq, endSeq uint64) (LogEntries, error) {
collection, err := dbc.GetDefaultDatabaseCollection()
if err != nil {
return nil, err
}
return dbc.CollectionChannelViewForTest(tb, collection, channelName, startSeq, endSeq)
}
func (dbc *DatabaseContext) CollectionChannelViewForTest(tb testing.TB, collection *DatabaseCollection, channelName string, startSeq, endSeq uint64) (LogEntries, error) {
return collection.getChangesInChannelFromQuery(base.TestCtx(tb), channelName, startSeq, endSeq, 0, false)
}
// Test-only version of GetPrincipal that doesn't trigger channel/role recalculation
func (dbc *DatabaseContext) GetPrincipalForTest(tb testing.TB, name string, isUser bool) (info *auth.PrincipalConfig, err error) {
ctx := base.TestCtx(tb)
var princ auth.Principal
if isUser {
princ, err = dbc.Authenticator(ctx).GetUser(name)
} else {
princ, err = dbc.Authenticator(ctx).GetRole(name)
}
if princ == nil {
return
}
info = new(auth.PrincipalConfig)
info.Name = &name
info.ExplicitChannels = princ.CollectionExplicitChannels(base.DefaultScope, base.DefaultCollection).AsSet()
if user, ok := princ.(auth.User); ok {
info.Channels = user.InheritedCollectionChannels(base.DefaultScope, base.DefaultCollection).AsSet()
email := user.Email()
info.Email = &email
info.Disabled = base.BoolPtr(user.Disabled())
info.ExplicitRoleNames = user.ExplicitRoles().AsSet()
info.RoleNames = user.RoleNames().AllKeys()
} else {
info.Channels = princ.Channels().AsSet()
}
return
}
// FlushRevisionCacheForTest creates a new revision cache. This is currently at the database level. Only use this in test code.
func (db *DatabaseContext) FlushRevisionCacheForTest() {
backingStores := make(map[uint32]RevisionCacheBackingStore, len(db.CollectionByID))
for i, v := range db.CollectionByID {
backingStores[i] = v
}
db.revisionCache = NewRevisionCache(
db.Options.RevisionCacheOptions,
backingStores,
db.DbStats.Cache(),
)
}
// TestBucketPoolWithIndexes runs a TestMain for packages that require creation of indexes
func TestBucketPoolWithIndexes(ctx context.Context, m *testing.M, tbpOptions base.TestBucketPoolOptions) {
base.TestBucketPoolMain(ctx, m, viewsAndGSIBucketReadier, viewsAndGSIBucketInit, tbpOptions)
}
// TestBucketPoolEnsureNoIndexes runs a TestMain for packages that expects no indexes to exist.
func TestBucketPoolEnsureNoIndexes(ctx context.Context, m *testing.M, tbpOptions base.TestBucketPoolOptions) {
base.TestBucketPoolMain(ctx, m, deleteDocsAndIndexesBucketReadier, base.NoopInitFunc, tbpOptions)
}
// Parse the plan looking for use of the fetch operation (appears as the key/value pair "#operator":"Fetch")
// If there's no fetch operator in the plan, we can assume the query is covered by the index.
// The plan returned by an EXPLAIN is a nested hierarchy with operators potentially appearing at different
// depths, so need to traverse the JSON object.
// https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/explain.html
func IsCovered(plan map[string]interface{}) bool {
for key, value := range plan {
switch value := value.(type) {
case string:
if key == "#operator" && value == "Fetch" {
return false
}
case map[string]interface{}:
if !IsCovered(value) {
return false
}
case []interface{}:
for _, arrayValue := range value {
jsonArrayValue, ok := arrayValue.(map[string]interface{})
if ok {
if !IsCovered(jsonArrayValue) {
return false
}
}
}
default:
}
}
return true
}
// If certain environment variables are set, for example to turn on XATTR support, then update
// the DatabaseContextOptions accordingly
func AddOptionsFromEnvironmentVariables(dbcOptions *DatabaseContextOptions) {
if base.TestUseXattrs() {
dbcOptions.EnableXattr = true
}
if base.TestsDisableGSI() {
dbcOptions.UseViews = true
}
}
// Sets up test db with the specified database context options. Note that environment variables can
// override somedbcOptions properties.
func SetupTestDBWithOptions(t testing.TB, dbcOptions DatabaseContextOptions) (*Database, context.Context) {
tBucket := base.GetTestBucket(t)
return SetupTestDBForDataStoreWithOptions(t, tBucket, dbcOptions)
}
func SetupTestDBForDataStoreWithOptions(t testing.TB, tBucket *base.TestBucket, dbcOptions DatabaseContextOptions) (*Database, context.Context) {
ctx := base.TestCtx(t)
AddOptionsFromEnvironmentVariables(&dbcOptions)
if dbcOptions.Scopes == nil {
dbcOptions.Scopes = GetScopesOptions(t, tBucket, 1)
}
dbCtx, err := NewDatabaseContext(ctx, "db", tBucket, false, dbcOptions)
require.NoError(t, err, "Couldn't create context for database 'db'")
ctx = dbCtx.AddDatabaseLogContext(ctx)
err = dbCtx.StartOnlineProcesses(ctx)
require.NoError(t, err)
db, err := CreateDatabase(dbCtx)
require.NoError(t, err, "Couldn't create database 'db'")
return db, addDatabaseAndTestUserContext(ctx, db)
}
// addDatabaseAndTestUserContext adds a fake user to the context
func addDatabaseAndTestUserContext(ctx context.Context, db *Database) context.Context {
return db.AddDatabaseLogContext(base.UserLogCtx(ctx, "gotest", base.UserDomainBuiltin, nil))
}
// GetScopesOptions sets up a ScopesOptions from a TestBucket. This will set up default or non default collections depending on the test harness use of SG_TEST_USE_DEFAULT_COLLECTION and whether the backing store supports collections.
func GetScopesOptions(t testing.TB, testBucket *base.TestBucket, numCollections int) ScopesOptions {
if !base.TestsUseNamedCollections() {
if numCollections != 1 {
t.Fatal("Setting numCollections on a test that can't use collections is invalid")
}
return GetScopesOptionsDefaultCollectionOnly(t)
}
// Get a datastore as provided by the test
stores := testBucket.GetNonDefaultDatastoreNames()
require.True(t, len(stores) >= numCollections, "Requested more collections %d than found on testBucket %d", numCollections, len(stores))
scopesConfig := ScopesOptions{}
for i := 0; i < numCollections; i++ {
dataStoreName := stores[i]
if scopeConfig, ok := scopesConfig[dataStoreName.ScopeName()]; ok {
if _, ok := scopeConfig.Collections[dataStoreName.CollectionName()]; ok {
// already present
} else {
scopeConfig.Collections[dataStoreName.CollectionName()] = CollectionOptions{}
}
} else {
scopesConfig[dataStoreName.ScopeName()] = ScopeOptions{
Collections: map[string]CollectionOptions{
dataStoreName.CollectionName(): {},
}}
}
}
return scopesConfig
}
// Return Scopes options without any configuration for only the default collection.
func GetScopesOptionsDefaultCollectionOnly(_ testing.TB) ScopesOptions {
return map[string]ScopeOptions{
base.DefaultScope: ScopeOptions{
Collections: map[string]CollectionOptions{
base.DefaultCollection: {},
},
},
}
}
func GetSingleDatabaseCollectionWithUser(ctx context.Context, tb testing.TB, database *Database) (*DatabaseCollectionWithUser, context.Context) {
c := &DatabaseCollectionWithUser{
DatabaseCollection: GetSingleDatabaseCollection(tb, database.DatabaseContext),
user: database.user,
}
return c, c.AddCollectionContext(ctx)
}
func GetSingleDatabaseCollection(tb testing.TB, database *DatabaseContext) *DatabaseCollection {
require.Equal(tb, 1, len(database.CollectionByID), fmt.Sprintf("Database must only have a single collection configured has %d", len(database.CollectionByID)))
for _, collection := range database.CollectionByID {
return collection
}
tb.Fatalf("Could not find a collection")
return nil
}
// AllocateTestSequence allocates a sequence via the sequenceAllocator. For use by non-db tests
func AllocateTestSequence(database *DatabaseContext) (uint64, error) {
return database.sequences.incrementSequence(1)
}
// ReleaseTestSequence releases a sequence via the sequenceAllocator. For use by non-db tests
func ReleaseTestSequence(ctx context.Context, database *DatabaseContext, sequence uint64) error {
return database.sequences.releaseSequence(ctx, sequence)
}
func (a *ActiveReplicator) GetActiveReplicatorConfig() *ActiveReplicatorConfig {
return a.config
}
func (apr *ActivePullReplicator) GetBlipSender() *blip.Sender {
return apr.blipSender
}
func DefaultMutateInOpts() *sgbucket.MutateInOptions {
return &sgbucket.MutateInOptions{
MacroExpansion: macroExpandSpec(base.SyncXattrName),
}
}
func RawDocWithInlineSyncData(_ testing.TB) string {
return `
{
"_sync": {
"rev": "1-ca9ad22802b66f662ff171f226211d5c",
"sequence": 1,
"recent_sequences": [1],
"history": {
"revs": ["1-ca9ad22802b66f662ff171f226211d5c"],
"parents": [-1],
"channels": [null]
},
"cas": "",
"time_saved": "2017-11-29T12:46:13.456631-08:00"
}
}
`
}
// DisableSequenceWaitOnDbStart disables the release sequence wait on db start. Appropriate for tests
// that make changes to database config after first startup, and don't assert/require on sequence correctness
func DisableSequenceWaitOnDbRestart(tb testing.TB) {
//
BypassReleasedSequenceWait.Store(true)
tb.Cleanup(func() {
BypassReleasedSequenceWait.Store(false)
})
}
// WriteDirect will write a document named doc-{sequence} with a given set of channels. This is used to simulate out of order sequence writes by bypassing typical Sync Gateway CRUD functions.
func WriteDirect(t *testing.T, collection *DatabaseCollection, channelArray []string, sequence uint64) {
key := fmt.Sprintf("doc-%v", sequence)
rev := "1-a"
chanMap := make(map[string]*channels.ChannelRemoval, 10)
for _, channel := range channelArray {
chanMap[channel] = nil
}
syncData := &SyncData{
CurrentRev: rev,
Sequence: sequence,
Channels: chanMap,
TimeSaved: time.Now(),
}
body := fmt.Sprintf(`{"key": "%s"}`, key)
if base.TestUseXattrs() {
opts := &sgbucket.MutateInOptions{
MacroExpansion: macroExpandSpec(base.SyncXattrName),
}
ctx := base.TestCtx(t)
_, err := collection.dataStore.WriteWithXattrs(ctx, key, 0, 0, []byte(body), map[string][]byte{base.SyncXattrName: base.MustJSONMarshal(t, syncData)}, nil, opts)
require.NoError(t, err)
} else {
_, err := collection.dataStore.Add(key, 0, Body{base.SyncPropertyName: syncData, "key": key})
require.NoError(t, err)
}
}