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

Function TestWebhookOldDoc

db/event_manager_test.go:625–764  ·  view source on GitHub ↗

Test Webhook where there is an old doc revision and where the filter function is expecting an old doc revision.

(t *testing.T)

Source from the content-addressed store, hash-verified

623// Test Webhook where there is an old doc revision and where the filter
624// function is expecting an old doc revision.
625func TestWebhookOldDoc(t *testing.T) {
626 terminator := make(chan bool)
627 defer close(terminator)
628
629 ts, wr := InitWebhookTest()
630 defer ts.Close()
631 url := ts.URL
632
633 ids := make([]string, 200)
634 for i := 0; i < 200; i++ {
635 ids[i] = fmt.Sprintf("%d", i)
636 }
637
638 eventForTest := func(k string, v int) (Body, string, base.Set) {
639 testBody := Body{
640 BodyId: ids[v],
641 "value": k,
642 }
643 var channelSet base.Set
644 if v%2 == 0 {
645 channelSet = base.SetFromArray([]string{"Even"})
646 } else {
647 channelSet = base.SetFromArray([]string{"Odd"})
648 }
649 return testBody, ids[v], channelSet
650 }
651
652 // Test basic webhook where an old doc is passed but not filtered
653 log.Println("Test basic webhook where an old doc is passed but not filtered")
654 em := NewEventManager(terminator)
655 ctx := base.TestCtx(t)
656 em.Start(ctx, 0, -1)
657 webhookHandler, _ := NewWebhook(ctx, fmt.Sprintf("%s/echo", url), "", nil, nil)
658 em.RegisterEventHandler(ctx, webhookHandler, DocumentChange)
659 for i := 0; i < 10; i++ {
660 oldBody, oldDocId, _ := eventForTest(strconv.Itoa(-i), i)
661 oldBody[BodyId] = oldDocId
662 oldBodyBytes := base.MustJSONMarshal(t, oldBody)
663 body, docId, channels := eventForTest(strconv.Itoa(i), i)
664 bodyBytes := base.MustJSONMarshal(t, body)
665 err := em.RaiseDocumentChangeEvent(ctx, bodyBytes, docId, string(oldBodyBytes), channels, false)
666 assert.NoError(t, err)
667
668 }
669 err := em.waitForProcessedTotal(ctx, 10, DefaultWaitForWebhook)
670 assert.NoError(t, err)
671 assert.Equal(t, int64(10), em.eventsProcessedSuccess)
672 log.Printf("Actual: %v, Expected: %v", wr.GetCount(), 10)
673
674 // Test webhook where an old doc is passed and is not used by the filter
675 log.Println("Test filter function with old doc which is not referenced")
676 wr.Clear()
677 em = NewEventManager(terminator)
678 em.Start(ctx, 0, -1)
679 filterFunction := `function(doc) {
680 if (doc.value < 6) {
681 return false;
682 } else {

Callers

nothing calls this directly

Calls 14

StartMethod · 0.95
RegisterEventHandlerMethod · 0.95
waitForProcessedTotalMethod · 0.95
SetFromArrayFunction · 0.92
TestCtxFunction · 0.92
MustJSONMarshalFunction · 0.92
InitWebhookTestFunction · 0.85
NewEventManagerFunction · 0.85
NewWebhookFunction · 0.85
GetCountMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected