Skip to content

Commit

Permalink
make blip tests fail with stack traces using testify (#6748)
Browse files Browse the repository at this point in the history
* Cleanup tests

- Create TB() helper functions to make it easier to pick up the right
  TB function since it can get reset or hard to pick up
- use require.EventuallyT instead of testing.T.Fatalf to get tracebacks
  when they fail. Because these functions fail anyway, removed the
  return of boolean found.
- switch panic to require statements
  • Loading branch information
torcolvin authored and bbrks committed Apr 3, 2024
1 parent 12835f5 commit 535a869
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 311 deletions.
24 changes: 8 additions & 16 deletions rest/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2285,10 +2285,8 @@ func TestUpdateExistingAttachment(t *testing.T) {

err := btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
require.True(t, ok)
_, ok = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
require.True(t, ok)
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)

attachmentAData := base64.StdEncoding.EncodeToString([]byte("attachmentA"))
attachmentBData := base64.StdEncoding.EncodeToString([]byte("attachmentB"))
Expand Down Expand Up @@ -2345,8 +2343,7 @@ func TestPushUnknownAttachmentAsStub(t *testing.T) {
err := btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)

_, ok := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
require.True(t, ok)
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)

// force attachment into test client's store to validate it's fetched
attachmentAData := base64.StdEncoding.EncodeToString([]byte("attachmentA"))
Expand Down Expand Up @@ -2396,8 +2393,7 @@ func TestMinRevPosWorkToAvoidUnnecessaryProveAttachment(t *testing.T) {
// Replicate data to client and ensure doc arrives
err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, found := btcRunner.WaitForVersion(btc.id, docID, initialVersion)
assert.True(t, found)
btcRunner.WaitForVersion(btc.id, docID, initialVersion)

// Push a revision with a bunch of history simulating doc updated on mobile device
// Note this references revpos 1 and therefore SGW has it - Shouldn't need proveAttachment
Expand Down Expand Up @@ -2438,8 +2434,7 @@ func TestAttachmentWithErroneousRevPos(t *testing.T) {
// Pull rev and attachment down to client
err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, found := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, found)
btcRunner.WaitForVersion(btc.id, docID, version)

// Add an attachment to client
btcRunner.AttachmentsLock(btc.id).Lock()
Expand All @@ -2451,8 +2446,7 @@ func TestAttachmentWithErroneousRevPos(t *testing.T) {
require.NoError(t, err)

// Ensure message and attachment is pushed up
_, ok := btc.pushReplication.WaitForMessage(2)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(2)

// Get the attachment and ensure the data is updated
resp := btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/doc/hello.txt", "")
Expand Down Expand Up @@ -2623,10 +2617,8 @@ func TestCBLRevposHandling(t *testing.T) {

err := btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
require.True(t, ok)
_, ok = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
require.True(t, ok)
btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)

attachmentAData := base64.StdEncoding.EncodeToString([]byte("attachmentA"))
attachmentBData := base64.StdEncoding.EncodeToString([]byte("attachmentB"))
Expand Down
27 changes: 9 additions & 18 deletions rest/blip_api_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func TestBlipPushPullV2AttachmentV2Client(t *testing.T) {
bodyText := `{"greetings":[{"hi": "alice"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`
version := btc.rt.PutDoc(docID, bodyText)

data, ok := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
data := btcRunner.WaitForVersion(btc.id, docID, version)
bodyTextExpected := `{"greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
require.JSONEq(t, bodyTextExpected, string(data))

Expand All @@ -77,8 +76,7 @@ func TestBlipPushPullV2AttachmentV2Client(t *testing.T) {
require.NoError(t, err)

// Wait for the document to be replicated at SG
_, ok = btc.pushReplication.WaitForMessage(2)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(2)

respBody := btc.rt.GetDocVersion(docID, version)

Expand Down Expand Up @@ -139,8 +137,7 @@ func TestBlipPushPullV2AttachmentV3Client(t *testing.T) {
bodyText := `{"greetings":[{"hi": "alice"}],"_attachments":{"hello.txt":{"data":"aGVsbG8gd29ybGQ="}}}`
version := btc.rt.PutDoc(docID, bodyText)

data, ok := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
data := btcRunner.WaitForVersion(btc.id, docID, version)
bodyTextExpected := `{"greetings":[{"hi":"alice"}],"_attachments":{"hello.txt":{"revpos":1,"length":11,"stub":true,"digest":"sha1-Kq5sNclPz7QV2+lfQIuc6R7oRu0="}}}`
require.JSONEq(t, bodyTextExpected, string(data))

Expand All @@ -150,8 +147,7 @@ func TestBlipPushPullV2AttachmentV3Client(t *testing.T) {
require.NoError(t, err)

// Wait for the document to be replicated at SG
_, ok = btc.pushReplication.WaitForMessage(2)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(2)

respBody := btc.rt.GetDocVersion(docID, version)

Expand Down Expand Up @@ -215,17 +211,15 @@ func TestBlipProveAttachmentV2(t *testing.T) {
doc1Body := fmt.Sprintf(`{"greetings":[{"hi": "alice"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc1Version := btc.rt.PutDoc(doc1ID, doc1Body)

data, ok := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
require.True(t, ok)
data := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
bodyTextExpected := fmt.Sprintf(`{"greetings":[{"hi":"alice"}],"_attachments":{"%s":{"revpos":1,"length":%d,"stub":true,"digest":"%s"}}}`, attachmentName, len(attachmentData), attachmentDigest)
require.JSONEq(t, bodyTextExpected, string(data))

// create doc2 now that we know the client has the attachment
doc2Body := fmt.Sprintf(`{"greetings":[{"howdy": "bob"}],"_attachments":{"%s":{"data":"%s"}}}`, attachmentName, attachmentDataB64)
doc2Version := btc.rt.PutDoc(doc2ID, doc2Body)

data, ok = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
require.True(t, ok)
data = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
bodyTextExpected = fmt.Sprintf(`{"greetings":[{"howdy":"bob"}],"_attachments":{"%s":{"revpos":1,"length":%d,"stub":true,"digest":"%s"}}}`, attachmentName, len(attachmentData), attachmentDigest)
require.JSONEq(t, bodyTextExpected, string(data))

Expand Down Expand Up @@ -320,8 +314,7 @@ func TestBlipPushPullNewAttachmentCommonAncestor(t *testing.T) {
assert.Equal(t, "2-abc", revId)

// Wait for the documents to be replicated at SG
_, ok := btc.pushReplication.WaitForMessage(2)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(2)

resp := btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+revId, "")
assert.Equal(t, http.StatusOK, resp.Code)
Expand All @@ -334,8 +327,7 @@ func TestBlipPushPullNewAttachmentCommonAncestor(t *testing.T) {
assert.Equal(t, "4-abc", revId)

// Wait for the document to be replicated at SG
_, ok = btc.pushReplication.WaitForMessage(4)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(4)

resp = btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+revId, "")
assert.Equal(t, http.StatusOK, resp.Code)
Expand Down Expand Up @@ -397,8 +389,7 @@ func TestBlipPushPullNewAttachmentNoCommonAncestor(t *testing.T) {
assert.Equal(t, "4-abc", revId)

// Wait for the document to be replicated at SG
_, ok := btc.pushReplication.WaitForMessage(2)
assert.True(t, ok)
btc.pushReplication.WaitForMessage(2)

resp := btc.rt.SendAdminRequest(http.MethodGet, "/{{.keyspace}}/"+docID+"?rev="+revId, "")
assert.Equal(t, http.StatusOK, resp.Code)
Expand Down
9 changes: 3 additions & 6 deletions rest/blip_api_collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ func TestCollectionsReplication(t *testing.T) {
err := btcCollection.StartOneshotPull()
require.NoError(t, err)

_, ok := btcCollection.WaitForVersion(docID, version)
require.True(t, ok)
btcCollection.WaitForVersion(docID, version)
})
}

Expand Down Expand Up @@ -301,8 +300,7 @@ func TestBlipReplicationMultipleCollections(t *testing.T) {
}

for i, collectionClient := range btc.collectionClients {
msg, ok := collectionClient.WaitForVersion(docName, versions[i])
require.True(t, ok)
msg := collectionClient.WaitForVersion(docName, versions[i])
require.Equal(t, body, string(msg))
}

Expand Down Expand Up @@ -358,8 +356,7 @@ func TestBlipReplicationMultipleCollectionsMismatchedDocSizes(t *testing.T) {
for _, collectionClient := range btc.collectionClients {
versions := collectionVersions[collectionClient.collection]
docIDs := collectionDocIDs[collectionClient.collection]
msg, ok := collectionClient.WaitForVersion(docIDs[len(docIDs)-1], versions[len(versions)-1])
require.True(t, ok)
msg := collectionClient.WaitForVersion(docIDs[len(docIDs)-1], versions[len(versions)-1])
require.Equal(t, body, string(msg))
}

Expand Down
59 changes: 21 additions & 38 deletions rest/blip_api_crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1901,19 +1901,16 @@ func TestBlipPullRevMessageHistory(t *testing.T) {
// create doc1 rev 1-0335a345b6ffed05707ccc4cbc1b67f4
version1 := rt.PutDoc(docID, `{"greetings": [{"hello": "world!"}, {"hi": "alice"}]}`)

data, ok := btcRunner.WaitForVersion(client.id, docID, version1)
assert.True(t, ok)
data := btcRunner.WaitForVersion(client.id, docID, version1)
assert.Equal(t, `{"greetings":[{"hello":"world!"},{"hi":"alice"}]}`, string(data))

// create doc1 rev 2-959f0e9ad32d84ff652fb91d8d0caa7e
version2 := rt.UpdateDoc(docID, version1, `{"greetings": [{"hello": "world!"}, {"hi": "alice"}, {"howdy": 12345678901234567890}]}`)

data, ok = btcRunner.WaitForVersion(client.id, docID, version2)
assert.True(t, ok)
data = btcRunner.WaitForVersion(client.id, docID, version2)
assert.Equal(t, `{"greetings":[{"hello":"world!"},{"hi":"alice"},{"howdy":12345678901234567890}]}`, string(data))

msg, ok := client.pullReplication.WaitForMessage(5)
assert.True(t, ok)
msg := client.pullReplication.WaitForMessage(5)
assert.Equal(t, version1.RevID, msg.Properties[db.RevMessageHistory]) // CBG-3268 update to use version
})
}
Expand All @@ -1939,15 +1936,13 @@ func TestActiveOnlyContinuous(t *testing.T) {

// start an initial pull
require.NoError(t, btcRunner.StartPullSince(btc.id, "true", "0", "true"))
rev, found := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, found)
rev := btcRunner.WaitForVersion(btc.id, docID, version)
assert.Equal(t, `{"test":true}`, string(rev))

// delete the doc and make sure the client still gets the tombstone replicated
deletedVersion := rt.DeleteDocReturnVersion(docID, version)

rev, found = btcRunner.WaitForVersion(btc.id, docID, deletedVersion)
assert.True(t, found)
rev = btcRunner.WaitForVersion(btc.id, docID, deletedVersion)
assert.Equal(t, `{}`, string(rev))
})
}
Expand Down Expand Up @@ -2040,8 +2035,7 @@ func TestRemovedMessageWithAlternateAccess(t *testing.T) {

err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, docID, version)

version = rt.UpdateDoc(docID, version, `{"channels": ["B"]}`)

Expand All @@ -2053,8 +2047,7 @@ func TestRemovedMessageWithAlternateAccess(t *testing.T) {

err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok = btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, docID, version)

version = rt.UpdateDoc(docID, version, `{"channels": []}`)
const docMarker = "docmarker"
Expand All @@ -2074,8 +2067,7 @@ func TestRemovedMessageWithAlternateAccess(t *testing.T) {

err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok = btcRunner.WaitForVersion(btc.id, docMarker, docMarkerVersion)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, docMarker, docMarkerVersion)

messages := btc.pullReplication.GetMessages()

Expand Down Expand Up @@ -2154,8 +2146,7 @@ func TestRemovedMessageWithAlternateAccessAndChannelFilteredReplication(t *testi

err = btcRunner.StartOneshotPull(btc.id)
assert.NoError(t, err)
_, ok := btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, docID, version)

version = rt.UpdateDoc(docID, version, `{"channels": ["C"]}`)
require.NoError(t, rt.WaitForPendingChanges())
Expand All @@ -2168,8 +2159,7 @@ func TestRemovedMessageWithAlternateAccessAndChannelFilteredReplication(t *testi

err = btcRunner.StartOneshotPullFiltered(btc.id, "A")
assert.NoError(t, err)
_, ok = btcRunner.WaitForVersion(btc.id, docID, version)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, docID, version)

_ = rt.UpdateDoc(docID, version, `{"channels": ["B"]}`)
markerID := "docmarker"
Expand All @@ -2185,8 +2175,7 @@ func TestRemovedMessageWithAlternateAccessAndChannelFilteredReplication(t *testi

err = btcRunner.StartOneshotPullFiltered(btc.id, "A")
assert.NoError(t, err)
_, ok = btcRunner.WaitForVersion(btc.id, markerID, markerVersion)
assert.True(t, ok)
_ = btcRunner.WaitForVersion(btc.id, markerID, markerVersion)

messages := btc.pullReplication.GetMessages()

Expand Down Expand Up @@ -2705,8 +2694,7 @@ func TestUnsubChanges(t *testing.T) {
require.NoError(t, err)

doc1Version := rt.PutDoc(doc1ID, `{"key":"val1"}`)
_, found := btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)
require.True(t, found)
_ = btcRunner.WaitForVersion(btc.id, doc1ID, doc1Version)

activeReplStat := rt.GetDatabase().DbStats.CBLReplicationPull().NumPullReplActiveContinuous
require.EqualValues(t, 1, activeReplStat.Value())
Expand All @@ -2721,7 +2709,7 @@ func TestUnsubChanges(t *testing.T) {
// Confirm no more changes are being sent
doc2Version := rt.PutDoc(doc2ID, `{"key":"val1"}`)
err = rt.WaitForConditionWithOptions(func() bool {
_, found = btcRunner.GetVersion(btc.id, "doc2", doc2Version)
_, found := btcRunner.GetVersion(btc.id, "doc2", doc2Version)
return found
}, 10, 100)
assert.Error(t, err)
Expand All @@ -2734,8 +2722,7 @@ func TestUnsubChanges(t *testing.T) {
// Confirm the pull replication can be restarted and it syncs doc2
err = btcRunner.StartPull(btc.id)
require.NoError(t, err)
_, found = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
assert.True(t, found)
_ = btcRunner.WaitForVersion(btc.id, doc2ID, doc2Version)
})
}

Expand Down Expand Up @@ -2792,8 +2779,7 @@ func TestRequestPlusPull(t *testing.T) {
require.NoError(t, releaseErr)

// The one-shot pull should unblock and replicate the document in the granted channel
data, ok := btcRunner.WaitForDoc(client.id, "pbs-1")
assert.True(t, ok)
data := btcRunner.WaitForDoc(client.id, "pbs-1")
assert.Equal(t, `{"channel":["PBS"]}`, string(data))
})
}
Expand Down Expand Up @@ -2857,8 +2843,7 @@ func TestRequestPlusPullDbConfig(t *testing.T) {
require.NoError(t, releaseErr)

// The one-shot pull should unblock and replicate the document in the granted channel
data, ok := btcRunner.WaitForDoc(client.id, "pbs-1")
assert.True(t, ok)
data := btcRunner.WaitForDoc(client.id, "pbs-1")
assert.Equal(t, `{"channel":["PBS"]}`, string(data))
})
}
Expand Down Expand Up @@ -2907,10 +2892,9 @@ func TestBlipRefreshUser(t *testing.T) {
err := btcRunner.StartPullSince(btc.id, "true", "0", "false")
require.NoError(t, err)

_, ok := btcRunner.WaitForDoc(btc.id, docID)
require.True(t, ok)
_ = btcRunner.WaitForDoc(btc.id, docID)

_, ok = btcRunner.GetVersion(btc.id, docID, version)
_, ok := btcRunner.GetVersion(btc.id, docID, version)
require.True(t, ok)

// delete user with an active blip connection
Expand Down Expand Up @@ -2979,8 +2963,8 @@ func TestOnDemandImportBlipFailure(t *testing.T) {
}
for i, testCase := range testCases {
rt.Run(testCase.name, func(t *testing.T) {
docID := fmt.Sprintf("doc%d,", i)
markerDoc := fmt.Sprintf("markerDoc%d", i)
docID := fmt.Sprintf("doc%d_%s,", i, testCase.name)
markerDoc := fmt.Sprintf("markerDoc%d_%s", i, testCase.name)
validBody := `{"foo":"bar"}`
_ = rt.PutDoc(docID, validBody)
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &BlipTesterClientOpts{
Expand All @@ -2991,8 +2975,7 @@ func TestOnDemandImportBlipFailure(t *testing.T) {
defer btc.Close()
require.NoError(t, btcRunner.StartOneshotPull(btc.id))

output, found := btcRunner.WaitForDoc(btc.id, docID)
require.True(t, found)
output := btcRunner.WaitForDoc(btc.id, docID)
require.JSONEq(t, validBody, string(output))

err := rt.GetSingleDataStore().SetRaw(docID, 0, nil, testCase.invalidBody)
Expand Down
Loading

0 comments on commit 535a869

Please sign in to comment.