Dont rely on filesystem mtimes for test as they can be flaky

This commit is contained in:
Kovid Goyal 2023-03-14 21:11:46 +05:30
parent e539035639
commit 5520a75bba
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -76,6 +76,7 @@ func TestThemeCollections(t *testing.T) {
received_etag := "" received_etag := ""
request_count := 0 request_count := 0
send_count := 0
check_etag := true check_etag := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
request_count++ request_count++
@ -84,6 +85,7 @@ func TestThemeCollections(t *testing.T) {
w.WriteHeader(http.StatusNotModified) w.WriteHeader(http.StatusNotModified)
return return
} }
send_count++
w.Header().Add("ETag", `"xxx"`) w.Header().Add("ETag", `"xxx"`)
w.Write(buf.Bytes()) w.Write(buf.Bytes())
})) }))
@ -112,7 +114,6 @@ func TestThemeCollections(t *testing.T) {
if request_count != 1 { if request_count != 1 {
t.Fatalf("Cached zip file was not used: %d", request_count) t.Fatalf("Cached zip file was not used: %d", request_count)
} }
before, _ := os.Stat(filepath.Join(tdir, "test.zip"))
_, err = fetch_cached("test", ts.URL, tdir, 0) _, err = fetch_cached("test", ts.URL, tdir, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -123,23 +124,16 @@ func TestThemeCollections(t *testing.T) {
if received_etag != `"xxx"` { if received_etag != `"xxx"` {
t.Fatalf("Got invalid ETag: %#v", received_etag) t.Fatalf("Got invalid ETag: %#v", received_etag)
} }
after, _ := os.Stat(filepath.Join(tdir, "test.zip")) if send_count != 1 {
if before.ModTime() != after.ModTime() { t.Fatalf("Cached zip file was incorrectly re-downloaded: %d", send_count)
t.Fatalf("Cached zip file was incorrectly re-downloaded: %s", cmp.Diff(before.ModTime(), after.ModTime()))
} }
err = os.Chtimes(filepath.Join(tdir, "test.zip"), time.Time{}, time.Time{})
if err != nil {
t.Fatal(err)
}
before, _ = os.Stat(filepath.Join(tdir, "test.zip"))
check_etag = false check_etag = false
_, err = fetch_cached("test", ts.URL, tdir, 0) _, err = fetch_cached("test", ts.URL, tdir, 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
after, _ = os.Stat(filepath.Join(tdir, "test.zip")) if send_count != 2 {
if before.ModTime() == after.ModTime() { t.Fatalf("Cached zip file was incorrectly not re-downloaded. %d", send_count)
t.Fatalf("Cached zip file was incorrectly not re-downloaded. %#v == %#v", before.ModTime(), after.ModTime())
} }
coll := Themes{name_map: map[string]*Theme{}} coll := Themes{name_map: map[string]*Theme{}}
closer, err := coll.add_from_zip_file(filepath.Join(tdir, "test.zip")) closer, err := coll.add_from_zip_file(filepath.Join(tdir, "test.zip"))