lil further cron one

main
Bel LaPointe 2025-04-27 12:59:33 -06:00
parent f95563e849
commit 49c44b9df8
2 changed files with 19 additions and 9 deletions

View File

@ -39,9 +39,12 @@ func one(ctx context.Context, feed feeds.Feed) error {
return nil
}
return fmt.Errorf("should GET %s", feed.Version.URL)
items, err := feed.Fetch(ctx)
if err != nil {
return err
}
return fmt.Errorf("should parse %s", feed.Version.URL)
return fmt.Errorf("what do with %+v", items)
return feed.Executed(ctx)
}

View File

@ -18,20 +18,27 @@ func TestOne(t *testing.T) {
ctx, can := context.WithTimeout(context.Background(), 5*time.Second)
defer can()
for name, aCtx := range map[string]func() context.Context{
"empty": func() context.Context {
for name, aCtx := range map[string]func(t *testing.T) context.Context{
"empty": func(t *testing.T) context.Context {
return db.Test(t, ctx)
},
"feeds": func() context.Context {
"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])
switch r.URL.Query().Get("id") {
case "0":
case "1":
default:
http.NotFound(w, r)
}
}))
t.Cleanup(s.Close)
t.Cleanup(func() {
slices.Sort(gets)
if len(gets) != 2+2+2 { // id=1+id=2 for each of 2 unrecycled ctx, id=1+id=2 for one across shared ctx
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)
}
})
@ -39,7 +46,7 @@ func TestOne(t *testing.T) {
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"); err != nil {
if _, err := feeds.Insert(ctx, fmt.Sprintf("%s?idx=%d", s.URL, i), "* * * * *", "matches", "tag"); err != nil {
t.Fatal(err)
}
}
@ -50,7 +57,7 @@ func TestOne(t *testing.T) {
aCtx := aCtx
t.Run(name, func(t *testing.T) {
t.Run("same ctx", func(t *testing.T) {
ctx := aCtx()
ctx := aCtx(t)
for i := 0; i < 2; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if err := cron.One(ctx); err != nil && ctx.Err() == nil {
@ -63,7 +70,7 @@ func TestOne(t *testing.T) {
t.Run("new ctx", func(t *testing.T) {
for i := 0; i < 2; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
ctx := aCtx()
ctx := aCtx(t)
if err := cron.One(ctx); err != nil && ctx.Err() == nil {
t.Fatalf("failed %d: %v", i, err)
}