Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/pkg/github/etagcache_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package github

import (
"net/http"
"net/http/httptest"
"testing"
)

// ETagCacheStats is the operator's window into #7430: it must report the
// SHARED cache — the one etagCacheWrap installs in every client's transport
// chain — not a private store. Exercise the shared cache through the wrapper
// and assert the deltas show up.
func TestETagCacheStats_ReportsSharedCache(t *testing.T) {
srv := &etagServer{body: `[{"number":7}]`, etag: `W/"stats-probe"`, remaining: 100}
ts := httptest.NewServer(http.HandlerFunc(srv.handler))
defer ts.Close()

url := ts.URL + "/repos/hivecommons/hive/issues?stats-probe=1"
// Drop this test's entry from the process-wide cache afterwards.
t.Cleanup(func() {
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", "Bearer tok-stats")
sharedETagCache.drop(etagCacheKey(req))
})

hits0, misses0, entries0 := ETagCacheStats()

tr := etagCacheWrap(http.DefaultTransport)

// Charged fetch: one miss, one stored body.
resp, _ := etagGet(t, tr, url, "tok-stats")
if resp.StatusCode != http.StatusOK {
t.Fatalf("first fetch = %d, want 200", resp.StatusCode)
}
// Free revalidation: one hit.
resp, _ = etagGet(t, tr, url, "tok-stats")
if resp.StatusCode != http.StatusOK || resp.Header.Get(etagCacheHeader) != "revalidated" {
t.Fatalf("second fetch = %d (%s=%q), want a replayed 200",
resp.StatusCode, etagCacheHeader, resp.Header.Get(etagCacheHeader))
}

hits1, misses1, entries1 := ETagCacheStats()
if hits1-hits0 != 1 {
t.Errorf("hits delta = %d, want 1", hits1-hits0)
}
if misses1-misses0 != 1 {
t.Errorf("misses delta = %d, want 1", misses1-misses0)
}
if entries1-entries0 != 1 {
t.Errorf("entries delta = %d, want 1", entries1-entries0)
}
}
26 changes: 26 additions & 0 deletions src/pkg/github/merge_verdict_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package github

import "testing"

// MergeVerdictKey is the contract between the governor (which records
// verdicts) and the dashboard (which looks them up): both must derive the
// same key from the same PullRequest, spelled exactly as the enumeration
// spelled Repo (#7478). Pin the format for both spellings.
func TestMergeVerdictKey(t *testing.T) {
cases := []struct {
name string
pr PullRequest
want string
}{
{"bare repo", PullRequest{Repo: "hive", Number: 42}, "hive#42"},
{"owner/name repo", PullRequest{Repo: "hivecommons/hive", Number: 7478}, "hivecommons/hive#7478"},
{"zero value", PullRequest{}, "#0"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := MergeVerdictKey(tc.pr); got != tc.want {
t.Errorf("MergeVerdictKey(%q, %d) = %q, want %q", tc.pr.Repo, tc.pr.Number, got, tc.want)
}
})
}
}
Loading