(t *testing.T)
| 310 | } |
| 311 | |
| 312 | func 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() |
nothing calls this directly
no test coverage detected