(t *testing.T)
| 308 | } |
| 309 | |
| 310 | func TestSubscribeRelayAsyncDoesNotBlock(t *testing.T) { |
| 311 | t.Parallel() |
| 312 | |
| 313 | db, ps := dbtestutil.NewDB(t) |
| 314 | workerID := uuid.New() |
| 315 | subscriberID := uuid.New() |
| 316 | |
| 317 | dialStarted := make(chan struct{}) |
| 318 | dialContinue := make(chan struct{}) |
| 319 | |
| 320 | provider := func(ctx context.Context, _ uuid.UUID, _ uuid.UUID, _ http.Header) ( |
| 321 | []codersdk.ChatStreamEvent, <-chan codersdk.ChatStreamEvent, func(), error, |
| 322 | ) { |
| 323 | // Signal that the dial has started, then block until released. |
| 324 | select { |
| 325 | case <-dialStarted: |
| 326 | default: |
| 327 | close(dialStarted) |
| 328 | } |
| 329 | select { |
| 330 | case <-dialContinue: |
| 331 | case <-ctx.Done(): |
| 332 | return nil, nil, nil, ctx.Err() |
| 333 | } |
| 334 | ch := make(chan codersdk.ChatStreamEvent, 10) |
| 335 | return nil, ch, func() {}, nil |
| 336 | } |
| 337 | |
| 338 | subscriber := newTestServer(t, db, ps, subscriberID, provider, nil) |
| 339 | |
| 340 | ctx := testutil.Context(t, testutil.WaitLong) |
| 341 | user, org, model := seedChatDependencies(t, db) |
| 342 | |
| 343 | // Seed a waiting chat so Subscribe does not trigger a synchronous |
| 344 | // relay. |
| 345 | chat := seedWaitingChat(t, db, org.ID, user, model, "relay-async-nonblock") |
| 346 | |
| 347 | // Subscribe before the chat is marked running so the relay opens |
| 348 | // via pubsub notification (openRelayAsync path). |
| 349 | _, events, cancel, ok := subscriber.Subscribe(ctx, chat.ID, nil, 0) |
| 350 | require.True(t, ok) |
| 351 | t.Cleanup(cancel) |
| 352 | |
| 353 | // Now mark the chat as running on a remote worker. This publishes |
| 354 | // a status notification which triggers openRelayAsync on the |
| 355 | // subscriber. |
| 356 | notify := coderdpubsub.ChatStreamNotifyMessage{ |
| 357 | Status: string(database.ChatStatusRunning), |
| 358 | WorkerID: workerID.String(), |
| 359 | } |
| 360 | payload, err := json.Marshal(notify) |
| 361 | require.NoError(t, err) |
| 362 | err = ps.Publish(coderdpubsub.ChatStreamNotifyChannel(chat.ID), payload) |
| 363 | require.NoError(t, err) |
| 364 | |
| 365 | // Wait for the relay dial to actually start (blocking in the |
| 366 | // provider). |
| 367 | select { |
nothing calls this directly
no test coverage detected