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