Files
terminarandClaude Opus 5.5 6a97dbd4c6 Shared Atlassian clients: atlassian/jira and atlassian/confluence
New library module yetano.dev/yetano/yetano-clients (owner decision
2026-11-01) replacing the three own Jira clients in yareco, yabaratla
and yamcp:

- atlassian: Options, Auth (api_token, basic, bearer/pat, cookie),
  Deployment (Cloud/Data Center), TLS, APIError and sentinel errors.
- atlassian/internal/transport: retry with backoff on 5xx/connection
  errors, shared adaptive 429 throttle honouring Retry-After (from
  yabaratla/yareco), CA file, proxy, cross-host redirect hardening,
  response cap (from yamcp), startAt pagination, file downloads.
- atlassian/jira: typed reads (projects, versions, fields, issues,
  comments, changelog, editmeta, transitions), JQL search on Cloud
  (/search/jql) and Data Center, classic search pages, issue writes,
  ADF/plain-text bodies per deployment, custom-field helpers.
- atlassian/confluence: small content API client (CQL search, pages,
  spaces, current user, create/update page with version check,
  comment, label), taken over from yamcp.

Standard library only; tests against httptest servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 14:11:00 +01:00

119 lines
3.1 KiB
Go

package transport
import (
"context"
"net/http"
"sync"
"testing"
"time"
)
// fakeClock drives the throttle without real waiting.
type fakeClock struct {
mu sync.Mutex
t time.Time
slept time.Duration
}
func (c *fakeClock) now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.t
}
func (c *fakeClock) sleep(_ context.Context, d time.Duration) error {
c.mu.Lock()
defer c.mu.Unlock()
c.t = c.t.Add(d)
c.slept += d
return nil
}
func newFakeThrottle() (*throttle, *fakeClock) {
clk := &fakeClock{t: time.Date(2026, 10, 14, 3, 0, 0, 0, time.UTC)}
th := newThrottle(quietLogger())
th.now = clk.now
return th, clk
}
func TestThrottleUnthrottledByDefault(t *testing.T) {
th, clk := newFakeThrottle()
for i := 0; i < 5; i++ {
if err := th.acquire(context.Background(), clk.sleep); err != nil {
t.Fatal(err)
}
}
if clk.slept != 0 {
t.Errorf("slept %s without any 429, want 0", clk.slept)
}
}
func TestThrottleRateLimitPausesAndSpacesRequests(t *testing.T) {
th, clk := newFakeThrottle()
th.rateLimited(5 * time.Second)
if th.interval != minInterval {
t.Fatalf("interval = %s, want %s", th.interval, minInterval)
}
_ = th.acquire(context.Background(), clk.sleep) // waits out the pause
_ = th.acquire(context.Background(), clk.sleep) // then one interval later
if want := 5*time.Second + minInterval; clk.slept != want {
t.Errorf("slept %s, want %s", clk.slept, want)
}
}
func TestThrottleConcurrent429sCountOnce(t *testing.T) {
th, _ := newFakeThrottle()
th.rateLimited(time.Second)
th.rateLimited(time.Second)
th.rateLimited(2 * time.Second)
if th.interval != minInterval {
t.Errorf("interval = %s, want %s (one slowdown per burst)", th.interval, minInterval)
}
if want := th.now().Add(2 * time.Second); !th.pauseUntil.Equal(want) {
t.Errorf("pauseUntil = %s, want the longest Retry-After %s", th.pauseUntil, want)
}
}
func TestThrottleBacksOffAndRecovers(t *testing.T) {
th, clk := newFakeThrottle()
for i := 0; i < 20; i++ {
th.rateLimited(0)
_ = clk.sleep(context.Background(), time.Millisecond) // next 429 arrives after the (zero) pause
}
if th.interval != maxInterval {
t.Fatalf("interval = %s, want capped at %s", th.interval, maxInterval)
}
for i := 0; i < 1000 && th.interval > 0; i++ {
th.succeeded()
}
if th.interval != 0 {
t.Errorf("interval = %s after many successes, want 0", th.interval)
}
}
func TestThrottleRespectsCancel(t *testing.T) {
th, _ := newFakeThrottle()
th.rateLimited(time.Hour)
ctx, cancel := context.WithCancel(context.Background())
cancel()
if err := th.acquire(ctx, sleepCtx); err == nil {
t.Error("acquire must return the context error")
}
}
func TestRetryAfter(t *testing.T) {
now := time.Date(2026, 10, 14, 3, 0, 0, 0, time.UTC)
h := http.Header{}
h.Set("Retry-After", "7")
if got := retryAfter(h, 1, now); got != 7*time.Second {
t.Errorf("seconds: got %s", got)
}
h.Set("Retry-After", now.Add(30*time.Second).Format(http.TimeFormat))
if got := retryAfter(h, 1, now); got != 30*time.Second {
t.Errorf("http date: got %s", got)
}
if got := retryAfter(http.Header{}, 2, now); got != 4*time.Second {
t.Errorf("fallback backoff: got %s", got)
}
}