MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestConflicts

Function TestConflicts

db/database_test.go:2027–2149  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2025}
2026
2027func TestConflicts(t *testing.T) {
2028
2029 db, ctx := setupTestDBAllowConflicts(t)
2030 defer db.Close(ctx)
2031 collection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db)
2032
2033 collection.ChannelMapper = channels.NewChannelMapper(ctx, channels.DocChannelsSyncFunction, db.Options.JavascriptTimeout)
2034
2035 // Instantiate channel cache for channel 'all'
2036 collectionID := collection.GetCollectionID()
2037
2038 allChannel := channels.NewID("all", collectionID)
2039 _, err := db.changeCache.getChannelCache().getSingleChannelCache(ctx, allChannel)
2040 require.NoError(t, err)
2041
2042 cacheWaiter := db.NewDCPCachingCountWaiter(t)
2043
2044 // Create rev 1 of "doc":
2045 body := Body{"n": 1, "channels": []string{"all", "1"}}
2046 _, _, err = collection.PutExistingRevWithBody(ctx, "doc", body, []string{"1-a"}, false, ExistingVersionWithUpdateToHLV)
2047 assert.NoError(t, err, "add 1-a")
2048
2049 // Wait for rev to be cached
2050 cacheWaiter.AddAndWait(1)
2051
2052 changeLog, err := collection.GetChangeLog(ctx, channels.NewID("all", collectionID), 0)
2053 require.NoError(t, err)
2054 assert.Len(t, changeLog, 1)
2055
2056 // Create two conflicting changes:
2057 body["n"] = 2
2058 body["channels"] = []string{"all", "2b"}
2059 _, _, err = collection.PutExistingRevWithBody(ctx, "doc", body, []string{"2-b", "1-a"}, false, ExistingVersionWithUpdateToHLV)
2060 assert.NoError(t, err, "add 2-b")
2061 body["n"] = 3
2062 body["channels"] = []string{"all", "2a"}
2063 _, _, err = collection.PutExistingRevWithBody(ctx, "doc", body, []string{"2-a", "1-a"}, false, ExistingVersionWithUpdateToHLV)
2064 assert.NoError(t, err, "add 2-a")
2065
2066 cacheWaiter.Add(2)
2067
2068 rawBody, _, _ := collection.dataStore.GetRaw("doc")
2069
2070 log.Printf("got raw body: %s", rawBody)
2071
2072 // Verify the change with the higher revid won:
2073 gotBody, err := collection.Get1xBody(ctx, "doc")
2074 require.NoError(t, err)
2075 expectedResult := Body{BodyId: "doc", BodyRev: "2-b", "n": 2, "channels": []string{"all", "2b"}}
2076 AssertEqualBodies(t, expectedResult, gotBody)
2077
2078 // Verify we can still get the other two revisions:
2079 gotBody, err = collection.Get1xRevBody(ctx, "doc", "1-a", false, nil)
2080 require.NoError(t, err)
2081 expectedResult = Body{BodyId: "doc", BodyRev: "1-a", "n": 1, "channels": []string{"all", "1"}}
2082 AssertEqualBodies(t, expectedResult, gotBody)
2083 gotBody, err = collection.Get1xRevBody(ctx, "doc", "2-a", false, nil)
2084 require.NoError(t, err)

Callers

nothing calls this directly

Calls 15

NewChannelMapperFunction · 0.92
NewIDFunction · 0.92
BaseSetOfFunction · 0.92
AssertEqualBodiesFunction · 0.85
getChangesFunction · 0.85
AddAndWaitMethod · 0.80
GetChangeLogMethod · 0.80
Get1xBodyMethod · 0.80

Tested by

no test coverage detected