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

Function TestPendingUpdatesMetric

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

Source from the content-addressed store, hash-verified

217}
218
219func TestPendingUpdatesMetric(t *testing.T) {
220 t.Parallel()
221
222 // SETUP
223 store, pubsub := dbtestutil.NewDB(t)
224 logger := testutil.Logger(t)
225
226 reg := prometheus.NewRegistry()
227 metrics := notifications.NewMetrics(reg)
228 tmpl := notifications.TemplateWorkspaceDeleted
229
230 const method = database.NotificationMethodSmtp
231
232 // GIVEN: a notification manager whose store updates are intercepted so we can read the number of pending updates set in the metric
233 cfg := defaultNotificationsConfig(method)
234 cfg.RetryInterval = serpent.Duration(time.Hour) // Delay retries so they don't interfere.
235 cfg.FetchInterval = serpent.Duration(time.Millisecond * 50)
236 cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100)
237
238 syncer := &syncInterceptor{Store: store}
239 interceptor := newUpdateSignallingInterceptor(syncer)
240 mClock := quartz.NewMock(t)
241 trap := mClock.Trap().NewTicker("Manager", "storeSync")
242 defer trap.Close()
243 fetchTrap := mClock.Trap().TickerFunc("notifier", "fetchInterval")
244 defer fetchTrap.Close()
245 mgr, err := notifications.NewManager(cfg, interceptor, pubsub, defaultHelpers(), metrics, logger.Named("manager"),
246 notifications.WithTestClock(mClock))
247 require.NoError(t, err)
248 ctx := testutil.Context(t, testutil.WaitSuperLong)
249 t.Cleanup(func() {
250 assert.NoError(t, mgr.Stop(ctx))
251 })
252 handler := &fakeHandler{}
253 inboxHandler := &fakeHandler{}
254
255 mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
256 method: handler,
257 database.NotificationMethodInbox: inboxHandler,
258 })
259
260 enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
261 require.NoError(t, err)
262
263 user := createSampleUser(t, store)
264
265 // WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
266 _, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success"}, "test") // this will succeed
267 require.NoError(t, err)
268 _, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
269 require.NoError(t, err)
270
271 mgr.Run(ctx)
272 trap.MustWait(ctx).MustRelease(ctx) // ensures ticker has been set
273 fetchTrap.MustWait(ctx).MustRelease(ctx)
274
275 // Advance to the first fetch
276 mClock.Advance(cfg.FetchInterval.Value()).MustWait(ctx)

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
WithTestClockFunction · 0.92
ContextFunction · 0.92
NewStoreEnqueuerFunction · 0.92
TryReceiveFunction · 0.92

Tested by

no test coverage detected