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

Function TestWebhookTimeout

db/event_manager_test.go:766–888  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

764}
765
766func TestWebhookTimeout(t *testing.T) {
767 base.LongRunningTest(t)
768
769 terminator := make(chan bool)
770 defer close(terminator)
771
772 base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)
773 ts, wr := InitWebhookTest()
774 defer ts.Close()
775 url := ts.URL
776
777 ids := make([]string, 200)
778 for i := 0; i < 200; i++ {
779 ids[i] = fmt.Sprintf("%d", i)
780 }
781
782 eventForTest := func(k string, v int) (Body, string, base.Set) {
783 testBody := Body{
784 BodyId: ids[v],
785 "value": k,
786 }
787 var channelSet base.Set
788 if v%2 == 0 {
789 channelSet = base.SetFromArray([]string{"Even"})
790 } else {
791 channelSet = base.SetFromArray([]string{"Odd"})
792 }
793 return testBody, ids[v], channelSet
794 }
795
796 // Test fast execution, short timeout. All events processed
797 log.Println("Test fast webhook, short timeout")
798 em := NewEventManager(terminator)
799 ctx := base.TestCtx(t)
800 em.Start(ctx, 0, -1)
801 timeout := uint64(2)
802 webhookHandler, _ := NewWebhook(ctx, fmt.Sprintf("%s/echo", url), "", &timeout, nil)
803 em.RegisterEventHandler(ctx, webhookHandler, DocumentChange)
804 for i := 0; i < 10; i++ {
805 body, docid, channels := eventForTest(strconv.Itoa(i), i)
806 bodyBytes := base.MustJSONMarshal(t, body)
807 err := em.RaiseDocumentChangeEvent(ctx, bodyBytes, docid, "", channels, false)
808 assert.NoError(t, err)
809 }
810 err := em.waitForProcessedTotal(ctx, 10, DefaultWaitForWebhook)
811 assert.NoError(t, err)
812 assert.Equal(t, int64(10), em.eventsProcessedSuccess)
813
814 // Test slow webhook, short timeout, numProcess=1, waitForProcess > webhook timeout. All events should get processed.
815 // Webhook timeout 1s
816 // WaitForProcess event manager time 1.5s
817 // Webhook should timeout and clear item from queue before another item attempts to be added to the queue
818 log.Println("Test slow webhook, short timeout")
819 wr.Clear()
820 errCount := 0
821 em = NewEventManager(terminator)
822 em.Start(ctx, 1, 1500)
823 timeout = uint64(1)

Callers

nothing calls this directly

Calls 15

StartMethod · 0.95
RegisterEventHandlerMethod · 0.95
waitForProcessedTotalMethod · 0.95
LongRunningTestFunction · 0.92
SetUpTestLoggingFunction · 0.92
SetFromArrayFunction · 0.92
TestCtxFunction · 0.92
MustJSONMarshalFunction · 0.92
InitWebhookTestFunction · 0.85

Tested by

no test coverage detected