TestRun_StreamRetry_RecordsMetric exercises the end-to-end retry path: a retryable error on the first Stream call, success on the second. Asserts both the metric and the back-compat OnRetry callback fire. Note: chatretry.Retry uses time.NewTimer (not quartz.Clock), so this test pays chatretry.Initi
(t *testing.T)
| 511 | // this test pays chatretry.InitialDelay (1s) of real wall-clock |
| 512 | // time per retry. Keep it to one retry. |
| 513 | func TestRun_StreamRetry_RecordsMetric(t *testing.T) { |
| 514 | t.Parallel() |
| 515 | |
| 516 | reg := prometheus.NewRegistry() |
| 517 | metrics := chatloop.NewMetrics(reg) |
| 518 | |
| 519 | type retryCall struct { |
| 520 | attempt int |
| 521 | classified chatretry.ClassifiedError |
| 522 | } |
| 523 | var retries []retryCall |
| 524 | |
| 525 | calls := 0 |
| 526 | model := &chattest.FakeModel{ |
| 527 | ProviderName: "test-provider", |
| 528 | ModelName: "test-model", |
| 529 | StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) { |
| 530 | calls++ |
| 531 | if calls == 1 { |
| 532 | return nil, xerrors.New("received status 429 from upstream") |
| 533 | } |
| 534 | return func(yield func(fantasy.StreamPart) bool) { |
| 535 | yield(fantasy.StreamPart{ |
| 536 | Type: fantasy.StreamPartTypeFinish, |
| 537 | FinishReason: fantasy.FinishReasonStop, |
| 538 | }) |
| 539 | }, nil |
| 540 | }, |
| 541 | } |
| 542 | |
| 543 | err := chatloop.Run(context.Background(), chatloop.RunOptions{ |
| 544 | Model: model, |
| 545 | MaxSteps: 1, |
| 546 | ContextLimitFallback: 4096, |
| 547 | PersistStep: func(_ context.Context, _ chatloop.PersistedStep) error { |
| 548 | return nil |
| 549 | }, |
| 550 | Metrics: metrics, |
| 551 | OnRetry: func( |
| 552 | attempt int, |
| 553 | _ error, |
| 554 | classified chatretry.ClassifiedError, |
| 555 | _ time.Duration, |
| 556 | ) { |
| 557 | retries = append(retries, retryCall{ |
| 558 | attempt: attempt, |
| 559 | classified: classified, |
| 560 | }) |
| 561 | }, |
| 562 | }) |
| 563 | require.NoError(t, err) |
| 564 | |
| 565 | // Back-compat: OnRetry still fires with classified error. |
| 566 | require.Len(t, retries, 1) |
| 567 | assert.Equal(t, 1, retries[0].attempt) |
| 568 | assert.Equal(t, codersdk.ChatErrorKindRateLimit, retries[0].classified.Kind) |
| 569 | assert.Equal(t, "test-provider", retries[0].classified.Provider) |
| 570 |
nothing calls this directly
no test coverage detected