|
| 1 | +package rpc_reader |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "sync" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// withSharedCache swaps the process-wide cache for the duration of a test. |
| 14 | +func withSharedCache(t *testing.T, ttl time.Duration) { |
| 15 | + t.Helper() |
| 16 | + prev := sharedCache |
| 17 | + sharedCache = newResponseCache(ttl) |
| 18 | + t.Cleanup(func() { sharedCache = prev }) |
| 19 | +} |
| 20 | + |
| 21 | +// countingServer returns an httptest server that records how many times it was hit. |
| 22 | +func countingServer(t *testing.T, body string) (*httptest.Server, *int32) { |
| 23 | + t.Helper() |
| 24 | + var hits int32 |
| 25 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 26 | + atomic.AddInt32(&hits, 1) |
| 27 | + _, _ = w.Write([]byte(body)) |
| 28 | + })) |
| 29 | + t.Cleanup(srv.Close) |
| 30 | + return srv, &hits |
| 31 | +} |
| 32 | + |
| 33 | +// newTestReader builds a reader with a generous timeout (NewReader treats the |
| 34 | +// timeout arg as seconds for the client and ms for the per-attempt context). |
| 35 | +func newTestReader(t *testing.T, url string) *Reader { |
| 36 | + t.Helper() |
| 37 | + r, err := NewReader(url, http.MethodGet, "", nil, nil, 5000, nil) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("NewReader: %v", err) |
| 40 | + } |
| 41 | + return r |
| 42 | +} |
| 43 | + |
| 44 | +func TestFetchJSON_ServesFromCacheWithinTTL(t *testing.T) { |
| 45 | + withSharedCache(t, time.Minute) |
| 46 | + srv, hits := countingServer(t, `{"price":1}`) |
| 47 | + r := newTestReader(t, srv.URL) |
| 48 | + |
| 49 | + for i := 0; i < 5; i++ { |
| 50 | + body, err := r.FetchJSON(context.Background()) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("FetchJSON: %v", err) |
| 53 | + } |
| 54 | + if string(body) != `{"price":1}` { |
| 55 | + t.Fatalf("unexpected body: %s", body) |
| 56 | + } |
| 57 | + } |
| 58 | + if got := atomic.LoadInt32(hits); got != 1 { |
| 59 | + t.Fatalf("expected 1 upstream hit, got %d", got) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestFetchJSON_RefetchesAfterTTL(t *testing.T) { |
| 64 | + withSharedCache(t, 20*time.Millisecond) |
| 65 | + srv, hits := countingServer(t, `{"price":1}`) |
| 66 | + r := newTestReader(t, srv.URL) |
| 67 | + |
| 68 | + if _, err := r.FetchJSON(context.Background()); err != nil { |
| 69 | + t.Fatalf("FetchJSON: %v", err) |
| 70 | + } |
| 71 | + time.Sleep(40 * time.Millisecond) |
| 72 | + if _, err := r.FetchJSON(context.Background()); err != nil { |
| 73 | + t.Fatalf("FetchJSON: %v", err) |
| 74 | + } |
| 75 | + if got := atomic.LoadInt32(hits); got != 2 { |
| 76 | + t.Fatalf("expected 2 upstream hits after TTL expiry, got %d", got) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestFetchJSON_DisabledPassesThrough(t *testing.T) { |
| 81 | + withSharedCache(t, 0) |
| 82 | + srv, hits := countingServer(t, `{"price":1}`) |
| 83 | + r := newTestReader(t, srv.URL) |
| 84 | + |
| 85 | + for i := 0; i < 3; i++ { |
| 86 | + if _, err := r.FetchJSON(context.Background()); err != nil { |
| 87 | + t.Fatalf("FetchJSON: %v", err) |
| 88 | + } |
| 89 | + } |
| 90 | + if got := atomic.LoadInt32(hits); got != 3 { |
| 91 | + t.Fatalf("expected 3 upstream hits when cache disabled, got %d", got) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestFetchJSON_CoalescesConcurrentRequests(t *testing.T) { |
| 96 | + withSharedCache(t, time.Minute) |
| 97 | + var hits int32 |
| 98 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 99 | + atomic.AddInt32(&hits, 1) |
| 100 | + time.Sleep(80 * time.Millisecond) // hold the connection so callers overlap |
| 101 | + _, _ = w.Write([]byte(`{"price":1}`)) |
| 102 | + })) |
| 103 | + t.Cleanup(srv.Close) |
| 104 | + r := newTestReader(t, srv.URL) |
| 105 | + |
| 106 | + var wg sync.WaitGroup |
| 107 | + for i := 0; i < 10; i++ { |
| 108 | + wg.Add(1) |
| 109 | + go func() { |
| 110 | + defer wg.Done() |
| 111 | + if _, err := r.FetchJSON(context.Background()); err != nil { |
| 112 | + t.Errorf("FetchJSON: %v", err) |
| 113 | + } |
| 114 | + }() |
| 115 | + } |
| 116 | + wg.Wait() |
| 117 | + if got := atomic.LoadInt32(&hits); got != 1 { |
| 118 | + t.Fatalf("expected concurrent requests to coalesce into 1 upstream hit, got %d", got) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestCacheKey_DistinctByURLQueryAndHeaders(t *testing.T) { |
| 123 | + mk := func(url, query string, headers map[string]string) string { |
| 124 | + r, err := NewReader(url, http.MethodGet, query, headers, nil, 5000, nil) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("NewReader: %v", err) |
| 127 | + } |
| 128 | + return r.cacheKey() |
| 129 | + } |
| 130 | + base := mk("http://x/a", "", nil) |
| 131 | + cases := map[string]string{ |
| 132 | + "different url": mk("http://x/b", "", nil), |
| 133 | + "different query": mk("http://x/a", "{q}", nil), |
| 134 | + "different header": mk("http://x/a", "", map[string]string{"k": "v"}), |
| 135 | + } |
| 136 | + for name, key := range cases { |
| 137 | + if key == base { |
| 138 | + t.Errorf("cacheKey should differ for %s but matched base", name) |
| 139 | + } |
| 140 | + } |
| 141 | + // Same inputs must produce the same key. |
| 142 | + if mk("http://x/a", "", nil) != base { |
| 143 | + t.Error("cacheKey not stable for identical inputs") |
| 144 | + } |
| 145 | +} |
0 commit comments