detached 573527d

test(updater): cover request-error retry paths

commit
573527dd5bfd6aa633ba018eb2b243309621aabb
author
brice · · 2026-06-25 15:49 UTC
parent
2bfaca9
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
1 file · +67 -0
M internal/infrastructure/updater/github_test.go +67 -0
@@ -454,6 +454,73 @@ func TestGitHubCheckerDoRequestWithRetry_RetryableStatus(t *testing.T) {
454454 }
455455 }
456456
457+func TestGitHubCheckerDoRequestWithRetry_RetryableRequestError(t *testing.T) {
458+ var calls int32
459+ tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {
460+ attempt := atomic.AddInt32(&calls, 1)
461+ if attempt == 1 {
462+ return nil, &net.OpError{Op: "read", Net: "tcp", Err: syscall.ECONNRESET}
463+ }
464+ return testResponse(http.StatusOK, `{"tag_name":"v1.2.3"}`), nil
465+ })
466+
467+ checker := NewGitHubChecker()
468+ checker.client = &http.Client{Transport: tr}
469+ checker.randInt63 = func(_ int64) int64 { return 0 }
470+ checker.sleep = func(context.Context, time.Duration) error { return nil }
471+
472+ req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, githubAPIURL, http.NoBody)
473+ if err != nil {
474+ t.Fatalf("failed to create request: %v", err)
475+ }
476+
477+ resp, err := checker.doRequestWithRetry(context.Background(), req)
478+ if err != nil {
479+ t.Fatalf("doRequestWithRetry() unexpected error: %v", err)
480+ }
481+ defer func() { _ = resp.Body.Close() }()
482+
483+ if resp.StatusCode != http.StatusOK {
484+ t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
485+ }
486+ if atomic.LoadInt32(&calls) != 2 {
487+ t.Fatalf("calls = %d, want 2", calls)
488+ }
489+}
490+
491+func TestGitHubCheckerDoRequestWithRetry_NoRetryOnNonRetryableRequestError(t *testing.T) {
492+ var calls int32
493+ wantErr := errors.New("ordinary request failure")
494+ tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {
495+ atomic.AddInt32(&calls, 1)
496+ return nil, wantErr
497+ })
498+
499+ checker := NewGitHubChecker()
500+ checker.client = &http.Client{Transport: tr}
501+ checker.randInt63 = func(_ int64) int64 { return 0 }
502+ checker.sleep = func(context.Context, time.Duration) error { return nil }
503+
504+ req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, githubAPIURL, http.NoBody)
505+ if err != nil {
506+ t.Fatalf("failed to create request: %v", err)
507+ }
508+
509+ resp, err := checker.doRequestWithRetry(context.Background(), req)
510+ if err == nil {
511+ if resp != nil && resp.Body != nil {
512+ _ = resp.Body.Close()
513+ }
514+ t.Fatal("doRequestWithRetry() returned nil error, want request failure")
515+ }
516+ if !errors.Is(err, wantErr) {
517+ t.Fatalf("doRequestWithRetry() error = %v, want %v", err, wantErr)
518+ }
519+ if atomic.LoadInt32(&calls) != 1 {
520+ t.Fatalf("calls = %d, want 1", calls)
521+ }
522+}
523+
457524 func TestGitHubCheckerCheckForUpdate_TransientError(t *testing.T) {
458525 var calls int32
459526 tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {
@@ -454,6 +454,73 @@ func TestGitHubCheckerDoRequestWithRetry_RetryableStatus(t *testing.T) {
454 }454 }
455 }455 }
456 456
457+func TestGitHubCheckerDoRequestWithRetry_RetryableRequestError(t *testing.T) {
458+ var calls int32
459+ tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {
460+ attempt := atomic.AddInt32(&calls, 1)
461+ if attempt == 1 {
462+ return nil, &net.OpError{Op: "read", Net: "tcp", Err: syscall.ECONNRESET}
463+ }
464+ return testResponse(http.StatusOK, `{"tag_name":"v1.2.3"}`), nil
465+ })
466+
467+ checker := NewGitHubChecker()
468+ checker.client = &http.Client{Transport: tr}
469+ checker.randInt63 = func(_ int64) int64 { return 0 }
470+ checker.sleep = func(context.Context, time.Duration) error { return nil }
471+
472+ req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, githubAPIURL, http.NoBody)
473+ if err != nil {
474+ t.Fatalf("failed to create request: %v", err)
475+ }
476+
477+ resp, err := checker.doRequestWithRetry(context.Background(), req)
478+ if err != nil {
479+ t.Fatalf("doRequestWithRetry() unexpected error: %v", err)
480+ }
481+ defer func() { _ = resp.Body.Close() }()
482+
483+ if resp.StatusCode != http.StatusOK {
484+ t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
485+ }
486+ if atomic.LoadInt32(&calls) != 2 {
487+ t.Fatalf("calls = %d, want 2", calls)
488+ }
489+}
490+
491+func TestGitHubCheckerDoRequestWithRetry_NoRetryOnNonRetryableRequestError(t *testing.T) {
492+ var calls int32
493+ wantErr := errors.New("ordinary request failure")
494+ tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {
495+ atomic.AddInt32(&calls, 1)
496+ return nil, wantErr
497+ })
498+
499+ checker := NewGitHubChecker()
500+ checker.client = &http.Client{Transport: tr}
501+ checker.randInt63 = func(_ int64) int64 { return 0 }
502+ checker.sleep = func(context.Context, time.Duration) error { return nil }
503+
504+ req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, githubAPIURL, http.NoBody)
505+ if err != nil {
506+ t.Fatalf("failed to create request: %v", err)
507+ }
508+
509+ resp, err := checker.doRequestWithRetry(context.Background(), req)
510+ if err == nil {
511+ if resp != nil && resp.Body != nil {
512+ _ = resp.Body.Close()
513+ }
514+ t.Fatal("doRequestWithRetry() returned nil error, want request failure")
515+ }
516+ if !errors.Is(err, wantErr) {
517+ t.Fatalf("doRequestWithRetry() error = %v, want %v", err, wantErr)
518+ }
519+ if atomic.LoadInt32(&calls) != 1 {
520+ t.Fatalf("calls = %d, want 1", calls)
521+ }
522+}
523+
457 func TestGitHubCheckerCheckForUpdate_TransientError(t *testing.T) {524 func TestGitHubCheckerCheckForUpdate_TransientError(t *testing.T) {
458 var calls int32525 var calls int32
459 tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {526 tr := roundTripFunc(func(_ *http.Request) (*http.Response, error) {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close