MCPcopy Index your code
hub / github.com/coder/coder / TestInflightDispatchesMetric

Function TestInflightDispatchesMetric

coderd/notifications/metrics_test.go:312–384  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

310}
311
312func TestInflightDispatchesMetric(t *testing.T) {
313 t.Parallel()
314
315 // SETUP
316 store, pubsub := dbtestutil.NewDB(t)
317 logger := testutil.Logger(t)
318
319 reg := prometheus.NewRegistry()
320 metrics := notifications.NewMetrics(reg)
321 tmpl := notifications.TemplateWorkspaceDeleted
322
323 const method = database.NotificationMethodSmtp
324
325 // GIVEN: a notification manager whose dispatches are intercepted and delayed to measure the number of inflight requests
326 cfg := defaultNotificationsConfig(method)
327 cfg.LeaseCount = 10
328 cfg.FetchInterval = serpent.Duration(time.Millisecond * 50)
329 cfg.RetryInterval = serpent.Duration(time.Hour) // Delay retries so they don't interfere.
330 cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100)
331
332 mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), metrics, logger.Named("manager"))
333 require.NoError(t, err)
334 ctx := testutil.Context(t, testutil.WaitSuperLong)
335 t.Cleanup(func() {
336 assert.NoError(t, mgr.Stop(ctx))
337 })
338
339 handler := &fakeHandler{}
340 const msgCount = 2
341
342 // Barrier handler will wait until all notification messages are in-flight.
343 barrier := newBarrierHandler(msgCount, handler)
344 mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
345 method: barrier,
346 database.NotificationMethodInbox: &fakeHandler{},
347 })
348
349 enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
350 require.NoError(t, err)
351
352 user := createSampleUser(t, store)
353
354 // WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
355 for i := 0; i < msgCount; i++ {
356 _, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success", "i": strconv.Itoa(i)}, "test")
357 require.NoError(t, err)
358 }
359
360 mgr.Run(ctx)
361
362 // THEN:
363 // Ensure we see the dispatches of the messages inflight.
364 require.Eventually(t, func() bool {
365 return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), tmpl.String())) == msgCount
366 }, testutil.WaitShort, testutil.IntervalFast)
367
368 for i := 0; i < msgCount; i++ {
369 barrier.wg.Done()

Callers

nothing calls this directly

Calls 15

StopMethod · 0.95
WithHandlersMethod · 0.95
EnqueueMethod · 0.95
RunMethod · 0.95
NewDBFunction · 0.92
LoggerFunction · 0.92
NewMetricsFunction · 0.92
NewManagerFunction · 0.92
ContextFunction · 0.92
NewStoreEnqueuerFunction · 0.92
defaultHelpersFunction · 0.85

Tested by

no test coverage detected