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

Function TestWebhookBasic

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

Source from the content-addressed store, hash-verified

436}
437
438func TestWebhookBasic(t *testing.T) {
439 base.LongRunningTest(t)
440
441 terminator := make(chan bool)
442 defer close(terminator)
443
444 ts, wr := InitWebhookTest()
445 defer ts.Close()
446 url := ts.URL
447
448 ids := make([]string, 200)
449 for i := 0; i < 200; i++ {
450 ids[i] = fmt.Sprintf("%d", i)
451 }
452
453 eventForTest := func(i int) (Body, string, base.Set) {
454 testBody := Body{
455 BodyId: ids[i],
456 "value": i,
457 }
458 var channelSet base.Set
459 if i%2 == 0 {
460 channelSet = base.SetFromArray([]string{"Even"})
461 } else {
462 channelSet = base.SetFromArray([]string{"Odd"})
463 }
464 return testBody, ids[i], channelSet
465 }
466
467 // Test basic webhook
468 log.Println("Test basic webhook")
469 em := NewEventManager(terminator)
470 ctx := base.TestCtx(t)
471 em.Start(ctx, 0, -1)
472 webhookHandler, _ := NewWebhook(ctx, fmt.Sprintf("%s/echo", url), "", nil, nil)
473 em.RegisterEventHandler(ctx, webhookHandler, DocumentChange)
474 for i := 0; i < 10; i++ {
475 body, docId, channels := eventForTest(i)
476 bodyBytes := base.MustJSONMarshal(t, body)
477 err := em.RaiseDocumentChangeEvent(ctx, bodyBytes, docId, "", channels, false)
478 assert.NoError(t, err)
479 }
480 err := em.waitForProcessedTotal(ctx, 10, DefaultWaitForWebhook)
481 assert.NoError(t, err)
482 assert.Equal(t, int64(10), em.GetEventsProcessedSuccess())
483
484 // Test webhook filter function
485 log.Println("Test filter function")
486 wr.Clear()
487 em = NewEventManager(terminator)
488 em.Start(ctx, 0, -1)
489 filterFunction := `function(doc) {
490 if (doc.value < 6) {
491 return false;
492 } else {
493 return true;
494 }
495 }`

Callers

nothing calls this directly

Calls 15

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

Tested by

no test coverage detected