Skip to content

Commit

Permalink
Simplify unit test based on PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcfraser committed Oct 18, 2024
1 parent 6b87c41 commit 0c61361
Showing 1 changed file with 22 additions and 77 deletions.
99 changes: 22 additions & 77 deletions rest/blip_api_crud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3157,86 +3157,31 @@ func TestOnDemandImportBlipFailure(t *testing.T) {
func TestBlipDatabaseClose(t *testing.T) {

base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP, base.KeySync, base.KeySyncMsg, base.KeyChanges, base.KeyCache)

bt, err := NewBlipTester(t)
require.NoError(t, err, "Error creating BlipTester")
rt := bt.restTester
defer bt.Close()
// Counter/Waitgroup to help ensure that all callbacks on continuous changes handler are received
receivedChangesWg := sync.WaitGroup{}

// When this test sends subChanges, Sync Gateway will send a changes request that must be handled
changeCount := 0
bt.blipContext.HandlerForProfile["changes"] = func(request *blip.Message) {

body, err := request.Body()
require.NoError(t, err)
if string(body) != "null" {
// Expected changes body: [[1,"foo","1-abc"]]
changeListReceived := [][]interface{}{}
err = base.JSONUnmarshal(body, &changeListReceived)
assert.NoError(t, err, "Error unmarshalling changes received")

for _, change := range changeListReceived {

// The change should have three items in the array
// [1,"foo","1-abc"]
assert.Len(t, change, 3)

// Verify doc id has expected vals
docID := change[1].(string)
assert.True(t, strings.HasPrefix(docID, "foo"))
changeCount++
log.Printf("changeCount: %v", changeCount)
receivedChangesWg.Done()
}

}

if !request.NoReply() {
// Send an empty response to avoid the Sync: Invalid response to 'changes' message
response := request.Response()
emptyResponseVal := []interface{}{}
emptyResponseValBytes, err := base.JSONMarshal(emptyResponseVal)
assert.NoError(t, err, "Error marshalling response")
response.SetBody(emptyResponseValBytes)
btcRunner := NewBlipTesterClientRunner(t)
btcRunner.Run(func(t *testing.T, SupportedBLIPProtocols []string) {
rt := NewRestTesterPersistentConfig(t)
defer rt.Close()
const username = "alice"
rt.CreateUser(username, []string{"*"})
btc := btcRunner.NewBlipTesterClientOptsWithRT(rt, &BlipTesterClientOpts{Username: username})
var blipContextClosed atomic.Bool
btcRunner.clients[btc.id].pullReplication.bt.blipContext.OnExitCallback = func() {
log.Printf("on exit callback invoked")
blipContextClosed.Store(true)
}

}

blipContextClosed := false
bt.blipContext.OnExitCallback = func() {
blipContextClosed = true
}

// Send subChanges to subscribe to changes, which will cause the "changes" profile handler above to be called back
subChangesRequest := bt.newRequest()
subChangesRequest.SetProfile("subChanges")
subChangesRequest.Properties["continuous"] = "true"
subChangesRequest.Properties["batch"] = "10" // default batch size is 200, lower this to 10 to make sure we get multiple batches
subChangesRequest.SetCompressed(false)
sent := bt.sender.Send(subChangesRequest)
assert.True(t, sent)
subChangesResponse := subChangesRequest.Response()
assert.Equal(t, subChangesRequest.SerialNumber(), subChangesResponse.SerialNumber())

// Write some documents to the server and wait for replication to confirm blip connection is established
for i := 1; i < 10; i++ {
receivedChangesWg.Add(1)
// Write a doc to the server
_ = rt.CreateTestDoc(fmt.Sprintf("foo-%d", i))
}

// Wait until all expected changes are received by change handler
require.NoError(t, WaitWithTimeout(&receivedChangesWg, time.Second*10))
// put a doc, and make sure blip connection is established
markerDoc := "markerDoc"
markerDocVersion := rt.CreateTestDoc(markerDoc)
rt.WaitForPendingChanges()
btcRunner.StartPull(btc.id)

// Remove the database to trigger close (avoids issues with double-close when the rest tester is closed during test teardown
dbName := rt.GetDatabase().Name
rt.ServerContext().RemoveDatabase(base.TestCtx(t), dbName)
btcRunner.WaitForVersion(btc.id, markerDoc, markerDocVersion)

// Verify the blip connection is closed
require.NoError(t, rt.WaitForCondition(func() bool {
return blipContextClosed
}))
RequireStatus(t, rt.SendAdminRequest(http.MethodDelete, "/{{.db}}/", ""), http.StatusOK)

require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.True(c, blipContextClosed.Load())
}, time.Second*10, time.Millisecond*100)
})
}

0 comments on commit 0c61361

Please sign in to comment.