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

Function TestBufferedUpdates

coderd/notifications/manager_test.go:27–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestBufferedUpdates(t *testing.T) {
28 t.Parallel()
29
30 // setup
31
32 store, ps := dbtestutil.NewDB(t)
33 logger := testutil.Logger(t)
34
35 interceptor := &syncInterceptor{Store: store}
36 santa := &santaHandler{}
37 santaInbox := &santaHandler{}
38
39 cfg := defaultNotificationsConfig(database.NotificationMethodSmtp)
40 cfg.StoreSyncInterval = serpent.Duration(time.Hour) // Ensure we don't sync the store automatically.
41
42 // GIVEN: a manager which will pass or fail notifications based on their "nice" labels
43 mgr, err := notifications.NewManager(cfg, interceptor, ps, defaultHelpers(), createMetrics(), logger.Named("notifications-manager"))
44 require.NoError(t, err)
45
46 handlers := map[database.NotificationMethod]notifications.Handler{
47 database.NotificationMethodSmtp: santa,
48 database.NotificationMethodInbox: santaInbox,
49 }
50
51 mgr.WithHandlers(handlers)
52 enq, err := notifications.NewStoreEnqueuer(cfg, interceptor, defaultHelpers(), logger.Named("notifications-enqueuer"), quartz.NewReal())
53 require.NoError(t, err)
54
55 user := dbgen.User(t, store, database.User{})
56
57 // WHEN: notifications are enqueued which should succeed and fail
58 ctx := testutil.Context(t, testutil.WaitSuperLong)
59 _, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "true", "i": "0"}, "") // Will succeed.
60 require.NoError(t, err)
61 _, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "true", "i": "1"}, "") // Will succeed.
62 require.NoError(t, err)
63 _, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "false", "i": "2"}, "") // Will fail.
64 require.NoError(t, err)
65
66 mgr.Run(ctx)
67
68 // THEN:
69
70 const (
71 expectedSuccess = 2
72 expectedFailure = 1
73 )
74
75 // Wait for messages to be dispatched.
76 require.Eventually(t, func() bool {
77 return santa.naughty.Load() == expectedFailure &&
78 santa.nice.Load() == expectedSuccess
79 }, testutil.WaitMedium, testutil.IntervalFast)
80
81 // Wait for the expected number of buffered updates to be accumulated.
82 require.Eventually(t, func() bool {
83 success, failure := mgr.BufferedUpdatesCount()
84 return success == expectedSuccess*len(handlers) && failure == expectedFailure*len(handlers)

Callers

nothing calls this directly

Calls 15

WithHandlersMethod · 0.95
EnqueueMethod · 0.95
RunMethod · 0.95
BufferedUpdatesCountMethod · 0.95
StopMethod · 0.95
NewDBFunction · 0.92
LoggerFunction · 0.92
NewManagerFunction · 0.92
NewStoreEnqueuerFunction · 0.92
UserFunction · 0.92
ContextFunction · 0.92

Tested by

no test coverage detected