Skip to content

Commit edc4e7a

Browse files
committed
Make TestBasicNotificationRoundtrip use postgres and expand scope to include state sync
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent ac85d98 commit edc4e7a

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

coderd/notifications/notifications_test.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"golang.org/x/exp/slices"
8+
"golang.org/x/xerrors"
79
"net/http"
810
"net/http/httptest"
911
"net/url"
@@ -37,19 +39,24 @@ func TestMain(m *testing.M) {
3739
}
3840

3941
// TestBasicNotificationRoundtrip enqueues a message to the store, waits for it to be acquired by a notifier,
40-
// and passes it off to a fake handler.
42+
// passes it off to a fake handler, and ensures the results are synchronized to the store.
4143
func TestBasicNotificationRoundtrip(t *testing.T) {
4244
t.Parallel()
4345

4446
// SETUP
45-
ctx, logger, db := setupInMemory(t)
47+
if !dbtestutil.WillUsePostgres() {
48+
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
49+
}
50+
51+
ctx, logger, db := setup(t)
4652
method := database.NotificationMethodSmtp
4753

4854
// GIVEN: a manager with standard config but a faked dispatch handler
4955
handler := &fakeHandler{}
50-
56+
interceptor := &bulkUpdateInterceptor{Store: db}
5157
cfg := defaultNotificationsConfig(method)
52-
mgr, err := notifications.NewManager(cfg, db, logger.Named("manager"))
58+
cfg.RetryInterval = serpent.Duration(time.Hour) // Ensure retries don't interfere with the test
59+
mgr, err := notifications.NewManager(cfg, interceptor, logger.Named("manager"))
5360
require.NoError(t, err)
5461
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})
5562
t.Cleanup(func() {
@@ -68,17 +75,33 @@ func TestBasicNotificationRoundtrip(t *testing.T) {
6875

6976
mgr.Run(ctx)
7077

71-
// THEN: we expect that the handler will have received the notifications for delivery
78+
// THEN: we expect that the handler will have received the notifications for dispatch
7279
require.Eventually(t, func() bool {
7380
handler.mu.RLock()
7481
defer handler.mu.RUnlock()
75-
return handler.succeeded == sid.String()
76-
}, testutil.WaitLong, testutil.IntervalMedium)
82+
return slices.Contains(handler.succeeded, sid.String()) &&
83+
slices.Contains(handler.failed, fid.String())
84+
}, testutil.WaitLong, testutil.IntervalFast)
85+
86+
// THEN: we expect the store to be called with the updates of the earlier dispatches
7787
require.Eventually(t, func() bool {
78-
handler.mu.RLock()
79-
defer handler.mu.RUnlock()
80-
return handler.failed == fid.String()
81-
}, testutil.WaitLong, testutil.IntervalMedium)
88+
return interceptor.sent.Load() == 1 &&
89+
interceptor.failed.Load() == 1
90+
}, testutil.WaitShort, testutil.IntervalFast)
91+
92+
// THEN: we verify that the store contains notifications in their expected state
93+
success, err := db.GetNotificationMessagesByStatus(ctx, database.GetNotificationMessagesByStatusParams{
94+
Status: database.NotificationMessageStatusSent,
95+
Limit: 10,
96+
})
97+
require.NoError(t, err)
98+
require.Len(t, success, 1)
99+
failed, err := db.GetNotificationMessagesByStatus(ctx, database.GetNotificationMessagesByStatusParams{
100+
Status: database.NotificationMessageStatusTemporaryFailure,
101+
Limit: 10,
102+
})
103+
require.NoError(t, err)
104+
require.Len(t, failed, 1)
82105
}
83106

84107
func TestSMTPDispatch(t *testing.T) {
@@ -517,8 +540,8 @@ func TestInvalidConfig(t *testing.T) {
517540
type fakeHandler struct {
518541
mu sync.RWMutex
519542

520-
succeeded string
521-
failed string
543+
succeeded []string
544+
failed []string
522545
}
523546

524547
func (f *fakeHandler) Dispatcher(payload types.MessagePayload, _, _ string) (dispatch.DeliveryFunc, error) {
@@ -527,11 +550,12 @@ func (f *fakeHandler) Dispatcher(payload types.MessagePayload, _, _ string) (dis
527550
defer f.mu.Unlock()
528551

529552
if payload.Labels["type"] == "success" {
530-
f.succeeded = msgID.String()
531-
} else {
532-
f.failed = msgID.String()
553+
f.succeeded = append(f.succeeded, msgID.String())
554+
return false, nil
533555
}
534-
return false, nil
556+
557+
f.failed = append(f.failed, msgID.String())
558+
return true, xerrors.New("oops")
535559
}, nil
536560
}
537561

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy