package steprun import ( "context" "errors" "strings" "testing" "time" sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" ) // Design 47 H2 §799: a fetch met by a forge's 401 or a connection fault, and a publish met by no responders or a bundle // that exited, end their run `again` after again-delays' delay, with no seat call; a third run's fault ends the step failed // with the why `fetch: ` or `publish: `. ADR 0664 §1 with the bus's count above the runs that started: an ask // given back twice before its first run (the bus says delivered 3, the step key 1 run began) meets its fault in run 2, // given again after again-delays' second, 60 s, not 3. func TestAFetchOrAPublishMetAwayIsATransportFault(t *testing.T) { delay, err := AgainDelays("") if err != nil || delay(1) != 15*time.Second || delay(2) != time.Minute || delay(3) != time.Minute { t.Fatalf("the default again-delays: %v", err) } if own, err := AgainDelays("2s, 5s"); err != nil || own(1) != 2*time.Second || own(2) != 5*time.Second { t.Errorf("a setting's own again-delays: %v", err) } if _, err := AgainDelays("15s,soon"); err == nil || !strings.Contains(err.Error(), AgainDelaysSetting) { t.Errorf("a delay that is no duration is refused naming the setting: %v", err) } for _, tc := range []struct { name, why string err error }{ {"a fetch met by a 401", "fetch: ", gitFailure("fetch", "fatal: unable to access the forge: The requested URL returned error: 401")}, {"a fetch met by a 503", "fetch: ", gitFailure("fetch", "remote: HTTP 503 store unavailable")}, {"a fetch met by a reset connection", "fetch: ", gitFailure("fetch", "kex_exchange_identification: read: Connection reset by peer")}, {"a publish met by no responders", "publish: ", PublishFault(errors.New("nats: no responders available for request"))}, {"a publish met by a bundle that exited", "publish: ", PublishFault(errors.New("the bundle exited"))}, {"a fetch of a commit that is not there", "", gitFailure("fetch", "fatal: unknown revision or path not in the working tree")}, {"a publish refused", "", PublishFault(errors.New("refused: the digest differs"))}, } { t.Run(tc.name, func(t *testing.T) { if tc.why == "" { if sdkstep.IsPassing(tc.err) { t.Fatalf("%v is a transport fault, want a failure", tc.err) } return } if !sdkstep.IsPassing(tc.err) || !strings.HasPrefix(tc.err.Error(), tc.why) { t.Fatalf("%v: want a transport fault opening with %q", tc.err, tc.why) } // Its first run ends again after 15 s; its second, delivered three times, again after 60 s; its third ends the step failed. for _, run := range []struct { prior int delivered uint64 }{{0, 0}, {1, 3}, {2, 0}} { prior := run.prior src, st := NewFakeAsks(), newSteps() src.Delivered = run.delivered a := testAsk(sdkstep.AskDelivery) _ = src.PublishAsk(a) p := &Puller{Source: src, Language: sdkstep.AskGo, Node: "shanks", Put: st.put, Capacity: func() int { return 1 }, PullWait: 10 * time.Millisecond, Prior: func(string) int { return prior }, Run: func(context.Context, sdkstep.StepAsk) (any, error) { return nil, tc.err }} ctx, cancel := context.WithCancel(context.Background()) go p.Pull(ctx) key, _ := sdkstep.AskStepKey("", "shanks", a.ID) waitFor(t, "the run's end", func() bool { s, ok := st.get(key); return ok && s.State != sdkstep.StepRunning }) cancel() s, _ := st.get(key) src.mu.Lock() delays := append([]time.Duration(nil), src.Delays...) src.mu.Unlock() if prior == 0 && (s.State != sdkstep.StepAgain || len(delays) != 1 || delays[0] != 15*time.Second) { t.Errorf("the first run: step %+v, delays %v, want again after 15s", s, delays) } if prior == 1 && (s.State != sdkstep.StepAgain || s.Run != 2 || len(delays) != 1 || delays[0] != time.Minute) { t.Errorf("the second run, delivered %d times: step %+v, delays %v, want again in run 2 after 60s", run.delivered, s, delays) } if prior == 2 && (s.State != sdkstep.StepFailed || !strings.HasPrefix(s.Reason, tc.why)) { t.Errorf("the third run: step %+v, want failed with the why %q", s, tc.why) } } }) } }