MCPcopy Create free account
hub / github.com/coder/coder / TestSubscribeRelayReconnectsOnDrop

Function TestSubscribeRelayReconnectsOnDrop

enterprise/coderd/x/chatd/chatd_test.go:216–308  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

214}
215
216func TestSubscribeRelayReconnectsOnDrop(t *testing.T) {
217 t.Parallel()
218
219 db, ps := dbtestutil.NewDB(t)
220 workerID := uuid.New()
221 subscriberID := uuid.New()
222
223 var callCount atomic.Int32
224
225 provider := func(ctx context.Context, _ uuid.UUID, _ uuid.UUID, _ http.Header) (
226 []codersdk.ChatStreamEvent, <-chan codersdk.ChatStreamEvent, func(), error,
227 ) {
228 call := callCount.Add(1)
229 ch := make(chan codersdk.ChatStreamEvent, 10)
230 if call == 1 {
231 // First relay: send a part then close to simulate a drop.
232 ch <- codersdk.ChatStreamEvent{
233 Type: codersdk.ChatStreamEventTypeMessagePart,
234 MessagePart: &codersdk.ChatStreamMessagePart{
235 Role: "assistant",
236 Part: codersdk.ChatMessageText("first-relay"),
237 },
238 }
239 close(ch)
240 } else {
241 // Second relay: send a different part, keep open.
242 ch <- codersdk.ChatStreamEvent{
243 Type: codersdk.ChatStreamEventTypeMessagePart,
244 MessagePart: &codersdk.ChatStreamMessagePart{
245 Role: "assistant",
246 Part: codersdk.ChatMessageText("second-relay"),
247 },
248 }
249 // Don't close — keep alive so the subscriber stays connected.
250 }
251 return nil, ch, func() {}, nil
252 }
253
254 mclk := quartz.NewMock(t)
255 // Trap the reconnect timer so we can fire it deterministically
256 // instead of waiting real time.
257 trapReconnect := mclk.Trap().NewTimer("reconnect")
258 defer trapReconnect.Close()
259
260 subscriber := newTestServer(t, db, ps, subscriberID, provider, mclk)
261
262 ctx := testutil.Context(t, testutil.WaitLong)
263 user, org, model := seedChatDependencies(t, db)
264
265 chat := seedRemoteRunningChat(ctx, t, db, org.ID, user, model, workerID, "relay-reconnect")
266
267 _, events, cancel, ok := subscriber.Subscribe(ctx, chat.ID, nil, 0)
268 require.True(t, ok)
269 t.Cleanup(cancel)
270
271 // Should get the first relay part.
272 require.Eventually(t, func() bool {
273 select {

Callers

nothing calls this directly

Calls 12

NewDBFunction · 0.92
ChatMessageTextFunction · 0.92
ContextFunction · 0.92
seedRemoteRunningChatFunction · 0.85
newTestServerFunction · 0.70
seedChatDependenciesFunction · 0.70
NewMethod · 0.65
AddMethod · 0.65
CloseMethod · 0.65
SubscribeMethod · 0.65
CleanupMethod · 0.65
LoadMethod · 0.45

Tested by

no test coverage detected