diff --git a/src/cmd/cron/main_test.go b/src/cmd/cron/main_test.go index 6f2c16b..01b09b9 100644 --- a/src/cmd/cron/main_test.go +++ b/src/cmd/cron/main_test.go @@ -1,10 +1,12 @@ package cron_test import ( + "bytes" "context" "fmt" + "io" "net/http" - "net/http/httptest" + "os" "show-rss/src/cmd/cron" "show-rss/src/db" "show-rss/src/feeds" @@ -24,29 +26,46 @@ func TestOne(t *testing.T) { }, "feeds": func(t *testing.T) context.Context { gets := []string{} - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - gets = append(gets, r.URL.String()) - t.Logf("%s", gets[len(gets)-1]) + sURL := "http://192.168.0.167:10000/" + s := &http.Server{ + Addr: ":10000", + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gets = append(gets, r.URL.String()) + t.Logf("serving fetch %s", gets[len(gets)-1]) - switch r.URL.Query().Get("id") { - case "0": - case "1": - default: - http.NotFound(w, r) - } - })) - t.Cleanup(s.Close) + switch r.URL.Query().Get("idx") { + case "0": + case "1": + default: + http.NotFound(w, r) + } + + b, _ := os.ReadFile(fmt.Sprintf("testdata/%s.rss", r.URL.Query().Get("idx"))) + io.Copy(w, bytes.NewReader(b)) + }), + } + go s.ListenAndServe() + t.Cleanup(func() { s.Close() }) t.Cleanup(func() { slices.Sort(gets) - if len(gets) != 2+2+2 { // id=0+id=1 for each of 2 unrecycled ctx, id=0+id=1 for one across shared ctx - t.Errorf("didn't call urls exactly twice: %+v", gets) + if len(gets) != 1+2+2+2 { // healthcheck, id=0+id=1 for each of 2 unrecycled ctx, id=0+id=1 for one across shared ctx + t.Errorf("didn't call urls: %+v", gets) } }) + for { + resp, err := http.Get(sURL) + if err == nil { + resp.Body.Close() + break + } + time.Sleep(100 * time.Millisecond) + } + ctx := db.Test(t, ctx) for i := 0; i < 2; i++ { - if _, err := feeds.Insert(ctx, fmt.Sprintf("%s?idx=%d", s.URL, i), "* * * * *", "matches", "tag"); err != nil { + if _, err := feeds.Insert(ctx, fmt.Sprintf("%s?idx=%d", sURL, i), "* * * * *", "matches", "tag"); err != nil { t.Fatal(err) } } diff --git a/src/cmd/cron/testdata/0.rss b/src/cmd/cron/testdata/0.rss new file mode 100644 index 0000000..f47f008 --- /dev/null +++ b/src/cmd/cron/testdata/0.rss @@ -0,0 +1,29 @@ + + + title matches + link + description + 2025-04-27T16:34:00+00:00 + + + + title + link matches + description + 2025-04-27T16:34:00+00:00 + + + + title + link + description matches + 2025-04-27T16:34:00+00:00 + + + + title + link + description + 2025-04-27T16:34:00+00:00 + + diff --git a/src/cmd/cron/testdata/1.rss b/src/cmd/cron/testdata/1.rss new file mode 100644 index 0000000..32294fc --- /dev/null +++ b/src/cmd/cron/testdata/1.rss @@ -0,0 +1,30 @@ + + + 1 title matches + 1 link + 1 description + 2025-04-27T16:34:00+00:00 + + + + 1 title + 1 link matches + 1 description + 2025-04-27T16:34:00+00:00 + + + + 1 title + 1 link + 1 description matches + 2025-04-27T16:34:00+00:00 + + + + 1 title + 1 link + 1 description + 2025-04-27T16:34:00+00:00 + + + diff --git a/src/feeds/http.go b/src/feeds/http.go index 6f8cf64..e909c02 100644 --- a/src/feeds/http.go +++ b/src/feeds/http.go @@ -28,7 +28,7 @@ var ( ) func (feed Feed) ShouldExecute() (bool, error) { - if feed.Entry.Deleted.IsZero() { + if !feed.Entry.Deleted.IsZero() { return false, nil } @@ -45,7 +45,7 @@ func (feed Feed) ShouldExecute() (bool, error) { return false, fmt.Errorf("illegal cron %q", feed.Version.Cron) } next := schedule.Next(feed.Execution.Executed) - return time.Now().Before(next), nil + return time.Now().After(next), nil } func (feed Feed) Fetch(ctx context.Context) (Items, error) {