Skip to content

Commit b39477c

Browse files
fix: resolve flakey inbox tests (#17010)
1 parent 4ea5ef9 commit b39477c

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

coderd/inboxnotifications.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,6 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
9494
return
9595
}
9696

97-
conn, err := websocket.Accept(rw, r, nil)
98-
if err != nil {
99-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
100-
Message: "Failed to upgrade connection to websocket.",
101-
Detail: err.Error(),
102-
})
103-
return
104-
}
105-
106-
go httpapi.Heartbeat(ctx, conn)
107-
defer conn.Close(websocket.StatusNormalClosure, "connection closed")
108-
10997
notificationCh := make(chan codersdk.InboxNotification, 10)
11098

11199
closeInboxNotificationsSubscriber, err := api.Pubsub.SubscribeWithErr(pubsub.InboxNotificationForOwnerEventChannel(apikey.UserID),
@@ -161,9 +149,20 @@ func (api *API) watchInboxNotifications(rw http.ResponseWriter, r *http.Request)
161149
api.Logger.Error(ctx, "subscribe to inbox notification event", slog.Error(err))
162150
return
163151
}
164-
165152
defer closeInboxNotificationsSubscriber()
166153

154+
conn, err := websocket.Accept(rw, r, nil)
155+
if err != nil {
156+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
157+
Message: "Failed to upgrade connection to websocket.",
158+
Detail: err.Error(),
159+
})
160+
return
161+
}
162+
163+
go httpapi.Heartbeat(ctx, conn)
164+
defer conn.Close(websocket.StatusNormalClosure, "connection closed")
165+
167166
encoder := wsjson.NewEncoder[codersdk.GetInboxNotificationResponse](conn, websocket.MessageText)
168167
defer encoder.Close(websocket.StatusNormalClosure)
169168

coderd/inboxnotifications_test.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func TestInboxNotification_Watch(t *testing.T) {
122122
}, "notification title", "notification content", nil)
123123
require.NoError(t, err)
124124

125-
dispatchFunc(ctx, uuid.New())
125+
_, err = dispatchFunc(ctx, uuid.New())
126+
require.NoError(t, err)
126127

127128
_, message, err := wsConn.Read(ctx)
128129
require.NoError(t, err)
@@ -174,7 +175,8 @@ func TestInboxNotification_Watch(t *testing.T) {
174175
}, "memory related title", "memory related content", nil)
175176
require.NoError(t, err)
176177

177-
dispatchFunc(ctx, uuid.New())
178+
_, err = dispatchFunc(ctx, uuid.New())
179+
require.NoError(t, err)
178180

179181
_, message, err := wsConn.Read(ctx)
180182
require.NoError(t, err)
@@ -193,15 +195,17 @@ func TestInboxNotification_Watch(t *testing.T) {
193195
}, "disk related title", "disk related title", nil)
194196
require.NoError(t, err)
195197

196-
dispatchFunc(ctx, uuid.New())
198+
_, err = dispatchFunc(ctx, uuid.New())
199+
require.NoError(t, err)
197200

198201
dispatchFunc, err = inboxHandler.Dispatcher(types.MessagePayload{
199202
UserID: memberClient.ID.String(),
200203
NotificationTemplateID: notifications.TemplateWorkspaceOutOfMemory.String(),
201204
}, "second memory related title", "second memory related title", nil)
202205
require.NoError(t, err)
203206

204-
dispatchFunc(ctx, uuid.New())
207+
_, err = dispatchFunc(ctx, uuid.New())
208+
require.NoError(t, err)
205209

206210
_, message, err = wsConn.Read(ctx)
207211
require.NoError(t, err)
@@ -256,7 +260,8 @@ func TestInboxNotification_Watch(t *testing.T) {
256260
}, "memory related title", "memory related content", nil)
257261
require.NoError(t, err)
258262

259-
dispatchFunc(ctx, uuid.New())
263+
_, err = dispatchFunc(ctx, uuid.New())
264+
require.NoError(t, err)
260265

261266
_, message, err := wsConn.Read(ctx)
262267
require.NoError(t, err)
@@ -276,7 +281,8 @@ func TestInboxNotification_Watch(t *testing.T) {
276281
}, "second memory related title", "second memory related title", nil)
277282
require.NoError(t, err)
278283

279-
dispatchFunc(ctx, uuid.New())
284+
_, err = dispatchFunc(ctx, uuid.New())
285+
require.NoError(t, err)
280286

281287
dispatchFunc, err = inboxHandler.Dispatcher(types.MessagePayload{
282288
UserID: memberClient.ID.String(),
@@ -285,7 +291,8 @@ func TestInboxNotification_Watch(t *testing.T) {
285291
}, "another memory related title", "another memory related title", nil)
286292
require.NoError(t, err)
287293

288-
dispatchFunc(ctx, uuid.New())
294+
_, err = dispatchFunc(ctx, uuid.New())
295+
require.NoError(t, err)
289296

290297
_, message, err = wsConn.Read(ctx)
291298
require.NoError(t, err)

0 commit comments

Comments
 (0)