Skip to content

Commit b5f0e80

Browse files
authored
CBG-4270: Implement sequence-based storage for BlipTesterClient (#7226)
* Implement sequence-based storage for BlipTesterClient * Add fixmes for the 3 bad tests * remove accidental test change * addlicense * pull out old comments and unrelated changes * drop more test logging * drop more test logging * fix lints * fix race - not using copied seq value * Updates based on review/race fixes * Inherit context, wrap with cancel and pass through to push changes feed - handle broadcast wake on close * Since btcc.ctx inherits from rt.Context, don't do an explicit check - rely on the context chaining * Block Close until the push replicator goroutine has exited * misspell fix * log around sync cond/locking to diagnose close deadlock * Fix Close deadlock and remove associated investigation logging * Replace test client logging with debug and drop assertions in favour of errors and requires * Reduce nesting - defer channel close * godoc comments
1 parent 83a8b5c commit b5f0e80

14 files changed

+1073
-339
lines changed

base/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,12 @@ func LogLevelPtr(value LogLevel) *LogLevel {
851851
return &value
852852
}
853853

854+
// Ptr returns a pointer to the given literal.
855+
// This is useful for wrapping around function calls that return a value, where you can't just use `&`.
856+
func Ptr[T any](v T) *T {
857+
return &v
858+
}
859+
854860
// StringPtr returns a pointer to the given string literal.
855861
func StringPtr(value string) *string {
856862
return &value
@@ -889,6 +895,14 @@ func IntPtr(i int) *int {
889895
return &i
890896
}
891897

898+
// IntDefault returns ifNil if i is nil, or else returns dereferenced value of i
899+
func IntDefault(i *int, ifNil int) int {
900+
if i != nil {
901+
return *i
902+
}
903+
return ifNil
904+
}
905+
892906
// BoolPtr returns a pointer to the given bool literal.
893907
func BoolPtr(b bool) *bool {
894908
return &b

rest/api_test_helpers.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type PutDocResponse struct {
4343

4444
// PutNewEditsFalse builds a new_edits=false style put to create a revision with the specified revID.
4545
// If parentRevID is not specified, treated as insert
46-
func (rt *RestTester) PutNewEditsFalse(docID string, newVersion DocVersion, parentVersion DocVersion, bodyString string) DocVersion {
46+
func (rt *RestTester) PutNewEditsFalse(docID string, newVersion DocVersion, parentVersion *DocVersion, bodyString string) *DocVersion {
4747

4848
var body db.Body
4949
marshalErr := base.JSONUnmarshal([]byte(bodyString), &body)
@@ -55,8 +55,9 @@ func (rt *RestTester) PutNewEditsFalse(docID string, newVersion DocVersion, pare
5555
revisions := make(map[string]interface{})
5656
revisions["start"] = newRevGeneration
5757
ids := []string{newRevDigest}
58-
if parentVersion.RevID != "" {
59-
_, parentDigest := db.ParseRevID(base.TestCtx(rt.TB()), parentVersion.RevID)
58+
if parentVersion != nil {
59+
parentVersionCopy := *parentVersion
60+
_, parentDigest := db.ParseRevID(base.TestCtx(rt.TB()), parentVersionCopy.RevID)
6061
ids = append(ids, parentDigest)
6162
}
6263
revisions["ids"] = ids
@@ -69,7 +70,7 @@ func (rt *RestTester) PutNewEditsFalse(docID string, newVersion DocVersion, pare
6970

7071
rt.WaitForPendingChanges()
7172

72-
return DocVersionFromPutResponse(rt.TB(), resp)
73+
return base.Ptr(DocVersionFromPutResponse(rt.TB(), resp))
7374
}
7475

7576
func (rt *RestTester) RequireWaitChanges(numChangesExpected int, since string) ChangesResults {

rest/attachment_test.go

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func TestConflictWithInvalidAttachment(t *testing.T) {
720720
require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &body))
721721

722722
// Modify Doc
723-
parentRevList := [3]string{"foo3", "foo2", version.Digest()}
723+
parentRevList := [3]string{"foo3", "foo2", version.RevIDDigest()}
724724
body["_rev"] = "3-foo3"
725725
body["rev"] = "3-foo3"
726726
body["_revisions"].(map[string]interface{})["ids"] = parentRevList
@@ -786,13 +786,13 @@ func TestConflictingBranchAttachments(t *testing.T) {
786786

787787
// //Create diverging tree
788788

789-
reqBodyRev2 := `{"_rev": "2-two", "_revisions": {"ids": ["two", "` + version.Digest() + `"], "start": 2}}`
789+
reqBodyRev2 := `{"_rev": "2-two", "_revisions": {"ids": ["two", "` + version.RevIDDigest() + `"], "start": 2}}`
790790
response := rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc1?new_edits=false", reqBodyRev2)
791791
RequireStatus(t, response, http.StatusCreated)
792792

793793
docVersion2 := DocVersionFromPutResponse(t, response)
794794

795-
reqBodyRev2a := `{"_rev": "2-two", "_revisions": {"ids": ["twoa", "` + version.Digest() + `"], "start": 2}}`
795+
reqBodyRev2a := `{"_rev": "2-two", "_revisions": {"ids": ["twoa", "` + version.RevIDDigest() + `"], "start": 2}}`
796796
response = rt.SendAdminRequest("PUT", "/{{.keyspace}}/doc1?new_edits=false", reqBodyRev2a)
797797
RequireStatus(t, response, http.StatusCreated)
798798
docVersion2a := DocVersionFromPutResponse(t, response)
@@ -2104,10 +2104,10 @@ func TestAttachmentRemovalWithConflicts(t *testing.T) {
21042104
losingVersion3 := rt.UpdateDoc(docID, version, `{"_attachments": {"hello.txt": {"revpos":2,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`)
21052105

21062106
// Create doc conflicting with previous revid referencing previous attachment too
2107-
winningVersion3 := rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("3-b"), version, `{"_attachments": {"hello.txt": {"revpos":2,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}, "Winning Rev": true}`)
2107+
winningVersion3 := rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("3-b"), &version, `{"_attachments": {"hello.txt": {"revpos":2,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}, "Winning Rev": true}`)
21082108

21092109
// Update the winning rev 3 and ensure attachment remains around as the other leaf still references this attachment
2110-
finalVersion4 := rt.UpdateDoc(docID, winningVersion3, `{"update": 2}`)
2110+
finalVersion4 := rt.UpdateDoc(docID, *winningVersion3, `{"update": 2}`)
21112111

21122112
type docResp struct {
21132113
Attachments db.AttachmentsMeta `json:"_attachments"`
@@ -2158,7 +2158,7 @@ func TestAttachmentsMissing(t *testing.T) {
21582158

21592159
version2 := rt.UpdateDoc(docID, version1, `{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}, "testval": ["xxx","xxx"]}`)
21602160

2161-
_ = rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("2-b"), version1, `{"_rev": "2-b", "_revisions": {"ids": ["b", "ca9ad22802b66f662ff171f226211d5c"], "start": 2}, "Winning Rev": true}`)
2161+
_ = rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("2-b"), &version1, `{"_rev": "2-b", "_revisions": {"ids": ["b", "ca9ad22802b66f662ff171f226211d5c"], "start": 2}, "Winning Rev": true}`)
21622162

21632163
rt.GetDatabase().FlushRevisionCacheForTest()
21642164

@@ -2177,7 +2177,7 @@ func TestAttachmentsMissingNoBody(t *testing.T) {
21772177

21782178
version2 := rt.UpdateDoc(docID, version1, `{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`)
21792179

2180-
_ = rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("2-b"), version1, `{}`)
2180+
_ = rt.PutNewEditsFalse(docID, NewDocVersionFromFakeRev("2-b"), &version1, `{}`)
21812181

21822182
rt.GetDatabase().FlushRevisionCacheForTest()
21832183

@@ -2272,38 +2272,41 @@ func TestUpdateExistingAttachment(t *testing.T) {
22722272
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, opts)
22732273
defer btc.Close()
22742274

2275+
btcRunner.StartPull(btc.id)
2276+
btcRunner.StartPush(btc.id)
2277+
22752278
doc1Version := rt.PutDoc(doc1ID, `{}`)
22762279
doc2Version := rt.PutDoc(doc2ID, `{}`)
2277-
22782280
rt.WaitForPendingChanges()
2279-
btcRunner.StartOneshotPull(btc.id)
2281+
22802282
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
22812283
btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
22822284

22832285
attachmentAData := base64.StdEncoding.EncodeToString([]byte("attachmentA"))
22842286
attachmentBData := base64.StdEncoding.EncodeToString([]byte("attachmentB"))
22852287

2286-
doc1Version, err := btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentAData+`"}}}`))
2288+
var err error
2289+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentAData+`"}}}`))
22872290
require.NoError(t, err)
2288-
doc2Version, err = btcRunner.PushRev(btc.id, doc2ID, doc2Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentBData+`"}}}`))
2291+
doc2Version, err = btcRunner.AddRev(btc.id, doc2ID, &doc2Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentBData+`"}}}`))
22892292
require.NoError(t, err)
22902293

2291-
assert.NoError(t, rt.WaitForVersion(doc1ID, doc1Version))
2292-
assert.NoError(t, rt.WaitForVersion(doc2ID, doc2Version))
2294+
require.NoError(t, rt.WaitForVersion(doc1ID, doc1Version))
2295+
require.NoError(t, rt.WaitForVersion(doc2ID, doc2Version))
22932296

22942297
collection, ctx := rt.GetSingleTestDatabaseCollection()
22952298
_, err = collection.GetDocument(ctx, "doc1", db.DocUnmarshalAll)
22962299
require.NoError(t, err)
22972300
_, err = collection.GetDocument(ctx, "doc2", db.DocUnmarshalAll)
22982301
require.NoError(t, err)
22992302

2300-
doc1Version, err = btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-SKk0IV40XSHW37d3H0xpv2+z9Ck=","length":11,"content_type":"","stub":true,"revpos":3}}}`))
2303+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-SKk0IV40XSHW37d3H0xpv2+z9Ck=","length":11,"content_type":"","stub":true,"revpos":3}}}`))
23012304
require.NoError(t, err)
23022305

23032306
assert.NoError(t, rt.WaitForVersion(doc1ID, doc1Version))
23042307

23052308
doc1, err := collection.GetDocument(ctx, "doc1", db.DocUnmarshalAll)
2306-
assert.NoError(t, err)
2309+
require.NoError(t, err)
23072310

23082311
assert.Equal(t, "sha1-SKk0IV40XSHW37d3H0xpv2+z9Ck=", doc1.Attachments["attachment"].(map[string]interface{})["digest"])
23092312

@@ -2328,11 +2331,13 @@ func TestPushUnknownAttachmentAsStub(t *testing.T) {
23282331
opts := BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
23292332
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &opts)
23302333
defer btc.Close()
2334+
2335+
btcRunner.StartPull(btc.id)
2336+
btcRunner.StartPush(btc.id)
2337+
23312338
// Add doc1 and doc2
23322339
doc1Version := btc.rt.PutDoc(doc1ID, `{}`)
2333-
23342340
btc.rt.WaitForPendingChanges()
2335-
btcRunner.StartOneshotPull(btc.id)
23362341

23372342
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
23382343

@@ -2343,7 +2348,7 @@ func TestPushUnknownAttachmentAsStub(t *testing.T) {
23432348
length, digest, err := btcRunner.saveAttachment(btc.id, contentType, attachmentAData)
23442349
require.NoError(t, err)
23452350
// Update doc1, include reference to non-existing attachment with recent revpos
2346-
doc1Version, err = btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(fmt.Sprintf(`{"key": "val", "_attachments":{"attachment":{"digest":"%s","length":%d,"content_type":"%s","stub":true,"revpos":1}}}`, digest, length, contentType)))
2351+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(fmt.Sprintf(`{"key": "val", "_attachments":{"attachment":{"digest":"%s","length":%d,"content_type":"%s","stub":true,"revpos":1}}}`, digest, length, contentType)))
23472352
require.NoError(t, err)
23482353

23492354
require.NoError(t, btc.rt.WaitForVersion(doc1ID, doc1Version))
@@ -2356,7 +2361,6 @@ func TestPushUnknownAttachmentAsStub(t *testing.T) {
23562361
}
23572362

23582363
func TestMinRevPosWorkToAvoidUnnecessaryProveAttachment(t *testing.T) {
2359-
base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
23602364
rtConfig := &RestTesterConfig{
23612365
GuestEnabled: true,
23622366
DatabaseConfig: &DatabaseConfig{
@@ -2376,25 +2380,44 @@ func TestMinRevPosWorkToAvoidUnnecessaryProveAttachment(t *testing.T) {
23762380
opts := BlipTesterClientOpts{SupportedBLIPProtocols: SupportedBLIPProtocols}
23772381
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &opts)
23782382
defer btc.Close()
2379-
// Push an initial rev with attachment data
2383+
2384+
btcRunner.StartPull(btc.id)
2385+
2386+
// Write an initial rev with attachment data
23802387
initialVersion := btc.rt.PutDoc(docID, `{"_attachments": {"hello.txt": {"data": "aGVsbG8gd29ybGQ="}}}`)
23812388

23822389
// Replicate data to client and ensure doc arrives
23832390
btc.rt.WaitForPendingChanges()
2384-
btcRunner.StartOneshotPull(btc.id)
23852391
btcRunner.WaitForVersion(btc.id, docID, initialVersion)
23862392

2387-
// Push a revision with a bunch of history simulating doc updated on mobile device
2388-
// Note this references revpos 1 and therefore SGW has it - Shouldn't need proveAttachment
2393+
// Create a set of revisions before we start the replicator to ensure there's a significant amount of history to push
2394+
version := initialVersion
2395+
for i := 0; i < 25; i++ {
2396+
var err error
2397+
version, err = btcRunner.AddRev(btc.id, docID, &version, []byte(`{"update_count":`+strconv.Itoa(i)+`,"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`))
2398+
require.NoError(t, err)
2399+
}
2400+
2401+
// Note this references revpos 1 and therefore SGW has it - Shouldn't need proveAttachment, even when we replicate it
23892402
proveAttachmentBefore := btc.pushReplication.replicationStats.ProveAttachment.Value()
2390-
revid, err := btcRunner.PushRevWithHistory(btc.id, docID, initialVersion.RevID, []byte(`{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`), 25, 5)
2391-
assert.NoError(t, err)
2403+
btcRunner.StartPushWithOpts(btc.id, BlipTesterPushOptions{Continuous: false})
2404+
require.NoError(t, rt.WaitForVersion(docID, version))
2405+
23922406
proveAttachmentAfter := btc.pushReplication.replicationStats.ProveAttachment.Value()
23932407
assert.Equal(t, proveAttachmentBefore, proveAttachmentAfter)
23942408

2395-
// Push another bunch of history
2396-
_, err = btcRunner.PushRevWithHistory(btc.id, docID, revid, []byte(`{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`), 25, 5)
2397-
assert.NoError(t, err)
2409+
// start another push to run in the background from where we last left off
2410+
latestSeq := btcRunner.SingleCollection(btc.id).lastSeq()
2411+
btcRunner.StartPushWithOpts(btc.id, BlipTesterPushOptions{Continuous: true, Since: strconv.Itoa(int(latestSeq))})
2412+
2413+
// Push another bunch of history, this time whilst a replicator is actively pushing them
2414+
for i := 25; i < 50; i++ {
2415+
var err error
2416+
version, err = btcRunner.AddRev(btc.id, docID, &version, []byte(`{"update_count":`+strconv.Itoa(i)+`,"_attachments": {"hello.txt": {"revpos":1,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`))
2417+
require.NoError(t, err)
2418+
}
2419+
2420+
require.NoError(t, rt.WaitForVersion(docID, version))
23982421
proveAttachmentAfter = btc.pushReplication.replicationStats.ProveAttachment.Value()
23992422
assert.Equal(t, proveAttachmentBefore, proveAttachmentAfter)
24002423
})
@@ -2430,7 +2453,7 @@ func TestAttachmentWithErroneousRevPos(t *testing.T) {
24302453
btcRunner.AttachmentsLock(btc.id).Unlock()
24312454

24322455
// Put doc with an erroneous revpos 1 but with a different digest, referring to the above attachment
2433-
_, err := btcRunner.PushRevWithHistory(btc.id, docID, version.RevID, []byte(`{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"length": 19,"digest":"sha1-l+N7VpXGnoxMm8xfvtWPbz2YvDc="}}}`), 1, 0)
2456+
_, err := btcRunner.PushRevWithHistory(btc.id, docID, &version, []byte(`{"_attachments": {"hello.txt": {"revpos":1,"stub":true,"length": 19,"digest":"sha1-l+N7VpXGnoxMm8xfvtWPbz2YvDc="}}}`), 1, 0)
24342457
require.NoError(t, err)
24352458

24362459
// Ensure message and attachment is pushed up
@@ -2605,12 +2628,14 @@ func TestCBLRevposHandling(t *testing.T) {
26052628
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
26062629
btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
26072630

2631+
btcRunner.StartPush(btc.id)
2632+
26082633
attachmentAData := base64.StdEncoding.EncodeToString([]byte("attachmentA"))
26092634
attachmentBData := base64.StdEncoding.EncodeToString([]byte("attachmentB"))
26102635

2611-
doc1Version, err := btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentAData+`"}}}`))
2636+
doc1Version, err := btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentAData+`"}}}`))
26122637
require.NoError(t, err)
2613-
doc2Version, err = btcRunner.PushRev(btc.id, doc2ID, doc2Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentBData+`"}}}`))
2638+
doc2Version, err = btcRunner.AddRev(btc.id, doc2ID, &doc2Version, []byte(`{"key": "val", "_attachments": {"attachment": {"data": "`+attachmentBData+`"}}}`))
26142639
require.NoError(t, err)
26152640

26162641
assert.NoError(t, btc.rt.WaitForVersion(doc1ID, doc1Version))
@@ -2623,25 +2648,29 @@ func TestCBLRevposHandling(t *testing.T) {
26232648
require.NoError(t, err)
26242649

26252650
// Update doc1, don't change attachment, use correct revpos
2626-
doc1Version, err = btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=","length":11,"content_type":"","stub":true,"revpos":2}}}`))
2651+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=","length":11,"content_type":"","stub":true,"revpos":2}}}`))
26272652
require.NoError(t, err)
26282653

26292654
assert.NoError(t, btc.rt.WaitForVersion(doc1ID, doc1Version))
26302655

26312656
// Update doc1, don't change attachment, use revpos=generation of revid, as CBL 2.x does. Should not proveAttachment on digest match.
2632-
doc1Version, err = btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=","length":11,"content_type":"","stub":true,"revpos":4}}}`))
2657+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-wzp8ZyykdEuZ9GuqmxQ7XDrY7Co=","length":11,"content_type":"","stub":true,"revpos":4}}}`))
26332658
require.NoError(t, err)
26342659

2660+
require.NoError(t, rt.WaitForVersion(doc1ID, doc1Version))
2661+
26352662
// Validate attachment exists
26362663
attResponse := btc.rt.SendAdminRequest("GET", "/{{.keyspace}}/doc1/attachment", "")
26372664
assert.Equal(t, 200, attResponse.Code)
26382665
assert.Equal(t, "attachmentA", string(attResponse.BodyBytes()))
26392666

26402667
attachmentPushCount := btc.rt.GetDatabase().DbStats.CBLReplicationPushStats.AttachmentPushCount.Value()
26412668
// Update doc1, change attachment digest with CBL revpos=generation. Should getAttachment
2642-
_, err = btcRunner.PushRev(btc.id, doc1ID, doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-SKk0IV40XSHW37d3H0xpv2+z9Ck=","length":11,"content_type":"","stub":true,"revpos":5}}}`))
2669+
doc1Version, err = btcRunner.AddRev(btc.id, doc1ID, &doc1Version, []byte(`{"key": "val", "_attachments":{"attachment":{"digest":"sha1-SKk0IV40XSHW37d3H0xpv2+z9Ck=","length":11,"content_type":"","stub":true,"revpos":5}}}`))
26432670
require.NoError(t, err)
26442671

2672+
require.NoError(t, rt.WaitForVersion(doc1ID, doc1Version))
2673+
26452674
// Validate attachment exists and is updated
26462675
attResponse = btc.rt.SendAdminRequest("GET", "/{{.keyspace}}/doc1/attachment", "")
26472676
assert.Equal(t, 200, attResponse.Code)

rest/audit_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,12 +1507,16 @@ func TestAuditBlipCRUD(t *testing.T) {
15071507
{
15081508
name: "add attachment",
15091509
attachmentName: "attachment1",
1510-
auditableCode: func(t testing.TB, docID string, docVersion DocVersion) {
1510+
setupCode: func(t testing.TB, docID string) DocVersion {
15111511
attData := base64.StdEncoding.EncodeToString([]byte("attach"))
1512-
1513-
version, err := btcRunner.PushRev(btc.id, docID, EmptyDocVersion(), []byte(`{"key":"val","_attachments":{"attachment1":{"data":"`+attData+`"}}}`))
1512+
version, err := btcRunner.AddRev(btc.id, docID, EmptyDocVersion(), []byte(`{"key":"val","_attachments":{"attachment1":{"data":"`+attData+`"}}}`))
15141513
require.NoError(t, err)
1515-
btcRunner.WaitForVersion(btc.id, docID, version)
1514+
return version
1515+
},
1516+
auditableCode: func(t testing.TB, docID string, version DocVersion) {
1517+
btcRunner.StartPushWithOpts(btc.id, BlipTesterPushOptions{Continuous: false})
1518+
// wait for the doc to be replicated, since that's what we're actually auditing
1519+
require.NoError(t, rt.WaitForVersion(docID, version))
15161520
},
15171521
attachmentCreateCount: 1,
15181522
},
@@ -1527,12 +1531,11 @@ func TestAuditBlipCRUD(t *testing.T) {
15271531
output := base.AuditLogContents(t, func(t testing.TB) {
15281532
testCase.auditableCode(t, docID, docVersion)
15291533
})
1530-
postAttachmentVersion, _ := rt.GetDoc(docID)
15311534

1532-
requireAttachmentEvents(rt, base.AuditIDAttachmentCreate, output, docID, postAttachmentVersion.RevID, testCase.attachmentName, testCase.attachmentCreateCount)
1533-
requireAttachmentEvents(rt, base.AuditIDAttachmentRead, output, docID, postAttachmentVersion.RevID, testCase.attachmentName, testCase.attachmentReadCount)
1534-
requireAttachmentEvents(rt, base.AuditIDAttachmentUpdate, output, docID, postAttachmentVersion.RevID, testCase.attachmentName, testCase.attachmentUpdateCount)
1535-
requireAttachmentEvents(rt, base.AuditIDAttachmentDelete, output, docID, postAttachmentVersion.RevID, testCase.attachmentName, testCase.attachmentDeleteCount)
1535+
requireAttachmentEvents(rt, base.AuditIDAttachmentCreate, output, docID, docVersion.RevID, testCase.attachmentName, testCase.attachmentCreateCount)
1536+
requireAttachmentEvents(rt, base.AuditIDAttachmentRead, output, docID, docVersion.RevID, testCase.attachmentName, testCase.attachmentReadCount)
1537+
requireAttachmentEvents(rt, base.AuditIDAttachmentUpdate, output, docID, docVersion.RevID, testCase.attachmentName, testCase.attachmentUpdateCount)
1538+
requireAttachmentEvents(rt, base.AuditIDAttachmentDelete, output, docID, docVersion.RevID, testCase.attachmentName, testCase.attachmentDeleteCount)
15361539
})
15371540
}
15381541
})

0 commit comments

Comments
 (0)