MCPcopy Index your code
hub / github.com/coder/coder / TestRun_StreamRetry_RecordsMetric

Function TestRun_StreamRetry_RecordsMetric

coderd/x/chatd/chatloop/metrics_test.go:513–578  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

511// this test pays chatretry.InitialDelay (1s) of real wall-clock
512// time per retry. Keep it to one retry.
513func 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

Callers

nothing calls this directly

Calls 6

NewMetricsFunction · 0.92
RunFunction · 0.92
requireCounterFunction · 0.85
NewMethod · 0.65
LenMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected