- Notifications
You must be signed in to change notification settings - Fork1k
feat: implement thin vertical slice of system-generated notifications#13537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
53c9cbb
4856aed
cda6efb
86f937a
e8f1af2
a056f54
8c64d30
ac149ec
884fadf
4e362e7
8097290
1b841ad
61f5bd6
3c8e33b
757327c
6f909ae
36698c5
c5701a6
9d4c312
9380d8e
4b7214d
6679ef1
337997d
ba5f7c6
0f29293
7c6c486
c6e75c2
aff9e6c
613e074
faea7fc
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Signed-off-by: Danny Kopping <danny@coder.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,7 +5,9 @@ import ( | ||
"encoding/json" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
"github.com/coder/serpent" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
@@ -31,11 +33,14 @@ func TestBufferedUpdates(t *testing.T) { | ||
if !dbtestutil.WillUsePostgres() { | ||
t.Skip("This test requires postgres") | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
ctx, logger, db := setup(t) | ||
interceptor := &bulkUpdateInterceptor{Store: db} | ||
santa := &santaHandler{} | ||
cfg := defaultNotificationsConfig(database.NotificationMethodSmtp) | ||
cfg.StoreSyncInterval = serpent.Duration(time.Hour) // Ensure we don't sync the store automatically. | ||
mgr, err := notifications.NewManager(cfg, interceptor, logger.Named("notifications-manager")) | ||
require.NoError(t, err) | ||
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{ | ||
@@ -47,20 +52,34 @@ func TestBufferedUpdates(t *testing.T) { | ||
user := dbgen.User(t, db, database.User{}) | ||
// given | ||
_, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "true"}, "") // Will succeed. | ||
require.NoError(t, err) | ||
_, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "true"}, "") // Will succeed. | ||
require.NoError(t, err) | ||
_, err = enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{"nice": "false"}, "") // Will fail. | ||
require.NoError(t, err) | ||
// when | ||
mgr.Run(ctx) | ||
// then | ||
const ( | ||
expectedSuccess = 2 | ||
expectedFailure = 1 | ||
) | ||
// Wait for messages to be dispatched. | ||
require.Eventually(t, func() bool { | ||
return santa.naughty.Load() == expectedFailure && | ||
santa.nice.Load() == expectedSuccess | ||
}, testutil.WaitMedium, testutil.IntervalFast) | ||
// Wait for the expected number of buffered updates to be accumulated. | ||
require.Eventually(t, func() bool { | ||
success, failure := mgr.BufferedUpdatesCount() | ||
return success == expectedSuccess && failure == expectedFailure | ||
}, testutil.WaitShort, testutil.IntervalFast) | ||
// Stop the manager which forces an update of buffered updates. | ||
require.NoError(t, mgr.Stop(ctx)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. there is a race condition here, which is that above we wait for notifications to be sent to santa, but sending to santa happensbefore sending on the Quartz can help here. The manager's bulk update and the notifier's processing loop depend on Tickers. If you convert them to use a This lets us get rid of the It also means we can get rid of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I've got a follow-up item to implement Quartz in a subsequent PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Addressed in613e074 | ||
@@ -73,8 +92,8 @@ func TestBufferedUpdates(t *testing.T) { | ||
ct.FailNow() | ||
} | ||
assert.EqualValues(ct,expectedFailure, interceptor.failed.Load()) | ||
assert.EqualValues(ct,expectedSuccess, interceptor.sent.Load()) | ||
}, testutil.WaitMedium, testutil.IntervalFast) | ||
} | ||