|
| 1 | +package keepcurrent |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// lastModifiedServer serves body with Last-Modified mod, answering 304 to an |
| 18 | +// If-Modified-Since at or after it, as a CDN or object store does. |
| 19 | +func lastModifiedServer(t *testing.T, mod time.Time, body string) *httptest.Server { |
| 20 | + t.Helper() |
| 21 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + if ims, err := http.ParseTime(r.Header.Get("If-Modified-Since")); err == nil && !mod.After(ims) { |
| 23 | + w.WriteHeader(http.StatusNotModified) |
| 24 | + return |
| 25 | + } |
| 26 | + w.Header().Set("Last-Modified", mod.UTC().Format(http.TimeFormat)) |
| 27 | + _, _ = w.Write([]byte(body)) |
| 28 | + })) |
| 29 | + t.Cleanup(srv.Close) |
| 30 | + return srv |
| 31 | +} |
| 32 | + |
| 33 | +// A runner started from a cached file older than the web copy fetches the web |
| 34 | +// copy, even when the process starts after the web copy was published. Before, |
| 35 | +// InitFrom recorded the start time as the data's age, so the first web check |
| 36 | +// asked only for data newer than the start, got 304, and kept the old file |
| 37 | +// until the web copy changed again. |
| 38 | +func TestInitFromOlderFileFetchesNewerWebCopy(t *testing.T) { |
| 39 | + published := time.Now().Add(-time.Hour).Truncate(time.Second) |
| 40 | + srv := lastModifiedServer(t, published, "new") |
| 41 | + |
| 42 | + path := filepath.Join(t.TempDir(), "db") |
| 43 | + require.NoError(t, os.WriteFile(path, []byte("old"), 0644)) |
| 44 | + cached := published.Add(-24 * time.Hour) |
| 45 | + require.NoError(t, os.Chtimes(path, cached, cached)) |
| 46 | + |
| 47 | + ch := make(chan []byte, 4) |
| 48 | + runner := New(FromWeb(srv.URL), ToFile(path), ToChannel(ch)) |
| 49 | + runner.InitFrom(FromFile(path)) |
| 50 | + assert.Equal(t, "old", string(<-ch), "InitFrom loads the cached file") |
| 51 | + stop := runner.Start(time.Hour) |
| 52 | + defer stop() |
| 53 | + |
| 54 | + select { |
| 55 | + case got := <-ch: |
| 56 | + assert.Equal(t, "new", string(got), "the first web check fetches the newer copy") |
| 57 | + case <-time.After(5 * time.Second): |
| 58 | + t.Fatal("the runner never fetched the web copy published after its cached file") |
| 59 | + } |
| 60 | + b, err := os.ReadFile(path) |
| 61 | + require.NoError(t, err) |
| 62 | + assert.Equal(t, "new", string(b), "and saves it") |
| 63 | +} |
| 64 | + |
| 65 | +// A runner started from a cached file at least as new as the web copy doesn't |
| 66 | +// fetch it again. |
| 67 | +func TestInitFromCurrentFileSkipsTheWebCopy(t *testing.T) { |
| 68 | + published := time.Now().Add(-time.Hour).Truncate(time.Second) |
| 69 | + srv := lastModifiedServer(t, published, "new") |
| 70 | + |
| 71 | + path := filepath.Join(t.TempDir(), "db") |
| 72 | + require.NoError(t, os.WriteFile(path, []byte("cached"), 0644)) |
| 73 | + cached := published.Add(time.Minute) |
| 74 | + require.NoError(t, os.Chtimes(path, cached, cached)) |
| 75 | + |
| 76 | + ch := make(chan []byte, 4) |
| 77 | + runner := New(FromWeb(srv.URL), ToFile(path), ToChannel(ch)) |
| 78 | + runner.InitFrom(FromFile(path)) |
| 79 | + assert.Equal(t, "cached", string(<-ch)) |
| 80 | + stop := runner.Start(time.Hour) |
| 81 | + time.Sleep(200 * time.Millisecond) |
| 82 | + stop() |
| 83 | + select { |
| 84 | + case got := <-ch: |
| 85 | + t.Fatalf("fetched %q although the cached file is current", got) |
| 86 | + default: |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// InitFrom doesn't write the file it read back to itself, so the file keeps |
| 91 | +// the age that tells the next runner whether it is current. |
| 92 | +func TestInitFromKeepsTheCachedFilesAge(t *testing.T) { |
| 93 | + path := filepath.Join(t.TempDir(), "db") |
| 94 | + require.NoError(t, os.WriteFile(path, []byte("cached"), 0644)) |
| 95 | + cached := time.Now().Add(-48 * time.Hour).Truncate(time.Second) |
| 96 | + require.NoError(t, os.Chtimes(path, cached, cached)) |
| 97 | + |
| 98 | + ch := make(chan []byte, 1) |
| 99 | + runner := New(FromWeb("http://127.0.0.1:1/unused"), ToFile(path), ToChannel(ch)) |
| 100 | + runner.InitFrom(FromFile(path)) |
| 101 | + assert.Equal(t, "cached", string(<-ch)) |
| 102 | + |
| 103 | + fi, err := os.Stat(path) |
| 104 | + require.NoError(t, err) |
| 105 | + assert.True(t, fi.ModTime().Equal(cached), "modtime %v, want %v", fi.ModTime(), cached) |
| 106 | +} |
| 107 | + |
| 108 | +// If-Modified-Since is the cutoff in GMT, whatever the host's time zone. |
| 109 | +func TestWebSourceSendsIfModifiedSinceInGMT(t *testing.T) { |
| 110 | + var got string |
| 111 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 112 | + got = r.Header.Get("If-Modified-Since") |
| 113 | + w.WriteHeader(http.StatusNotModified) |
| 114 | + })) |
| 115 | + t.Cleanup(srv.Close) |
| 116 | + cutoff := time.Date(2026, 10, 8, 17, 15, 0, 0, time.FixedZone("UTC-6", -6*3600)) |
| 117 | + _, err := FromWeb(srv.URL).Fetch(cutoff) |
| 118 | + assert.ErrorIs(t, err, ErrUnmodified) |
| 119 | + assert.Equal(t, "Thu, 08 Oct 2026 23:15:00 GMT", got) |
| 120 | +} |
| 121 | + |
| 122 | +// A sink that preprocesses still writes to the file the source read, since |
| 123 | +// its output can differ from what was read. |
| 124 | +func TestInitFromStillWritesAPreprocessedCopyInPlace(t *testing.T) { |
| 125 | + path := filepath.Join(t.TempDir(), "db") |
| 126 | + require.NoError(t, os.WriteFile(path, []byte("cached"), 0644)) |
| 127 | + upper := func(r io.Reader) (io.Reader, error) { |
| 128 | + b, err := io.ReadAll(r) |
| 129 | + return strings.NewReader(strings.ToUpper(string(b))), err |
| 130 | + } |
| 131 | + runner := New(FromWeb("http://127.0.0.1:1/unused"), ToFileWithPreprocessor(path, upper)) |
| 132 | + runner.InitFrom(FromFile(path)) |
| 133 | + b, err := os.ReadFile(path) |
| 134 | + require.NoError(t, err) |
| 135 | + assert.Equal(t, "CACHED", string(b)) |
| 136 | +} |
| 137 | + |
| 138 | +// A cached file dated in the future says nothing about its age, so the first |
| 139 | +// web check is unconditional and fetches a copy published before that date. |
| 140 | +func TestInitFromFutureDatedFileFetchesTheWebCopy(t *testing.T) { |
| 141 | + published := time.Now().Add(-time.Hour).Truncate(time.Second) |
| 142 | + srv := lastModifiedServer(t, published, "new") |
| 143 | + |
| 144 | + path := filepath.Join(t.TempDir(), "db") |
| 145 | + require.NoError(t, os.WriteFile(path, []byte("old"), 0644)) |
| 146 | + future := time.Now().Add(48 * time.Hour) |
| 147 | + require.NoError(t, os.Chtimes(path, future, future)) |
| 148 | + |
| 149 | + ch := make(chan []byte, 4) |
| 150 | + runner := New(FromWeb(srv.URL), ToFile(path), ToChannel(ch)) |
| 151 | + runner.InitFrom(FromFile(path)) |
| 152 | + assert.Equal(t, "old", string(<-ch)) |
| 153 | + stop := runner.Start(time.Hour) |
| 154 | + defer stop() |
| 155 | + select { |
| 156 | + case got := <-ch: |
| 157 | + assert.Equal(t, "new", string(got)) |
| 158 | + case <-time.After(5 * time.Second): |
| 159 | + t.Fatal("a future-dated cached file kept the runner from fetching the web copy") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// A sink that reaches the source's file through a symlink is the same file, so |
| 164 | +// InitFrom doesn't rewrite it either. |
| 165 | +func TestInitFromKeepsTheAgeThroughASymlink(t *testing.T) { |
| 166 | + dir := t.TempDir() |
| 167 | + path := filepath.Join(dir, "db") |
| 168 | + link := filepath.Join(dir, "db-link") |
| 169 | + require.NoError(t, os.WriteFile(path, []byte("cached"), 0644)) |
| 170 | + require.NoError(t, os.Symlink(path, link)) |
| 171 | + cached := time.Now().Add(-48 * time.Hour).Truncate(time.Second) |
| 172 | + require.NoError(t, os.Chtimes(path, cached, cached)) |
| 173 | + |
| 174 | + ch := make(chan []byte, 1) |
| 175 | + runner := New(FromWeb("http://127.0.0.1:1/unused"), ToFile(link), ToChannel(ch)) |
| 176 | + runner.InitFrom(FromFile(path)) |
| 177 | + assert.Equal(t, "cached", string(<-ch)) |
| 178 | + |
| 179 | + fi, err := os.Lstat(link) |
| 180 | + require.NoError(t, err) |
| 181 | + assert.NotZero(t, fi.Mode()&os.ModeSymlink, "the link is still a link") |
| 182 | + fi, err = os.Stat(path) |
| 183 | + require.NoError(t, err) |
| 184 | + assert.True(t, fi.ModTime().Equal(cached), "modtime %v, want %v", fi.ModTime(), cached) |
| 185 | +} |
0 commit comments