diff --git a/tracker/handler.go b/tracker/handler.go index 17c917f1..21d5131f 100644 --- a/tracker/handler.go +++ b/tracker/handler.go @@ -113,6 +113,10 @@ func (h *Handler) nextJob(resp http.ResponseWriter, req *http.Request) *JobWithT return nil } jt := h.jobservice.NextJob(req.Context()) + if jt == nil { + resp.WriteHeader(http.StatusInternalServerError) + return nil + } // Check for empty job (no job found with files) if jt.Job.Date.Equal(time.Time{}) { diff --git a/tracker/handler_test.go b/tracker/handler_test.go index e8db8308..2af0ab7b 100644 --- a/tracker/handler_test.go +++ b/tracker/handler_test.go @@ -31,6 +31,10 @@ type fakeJobService struct { } func (f *fakeJobService) NextJob(ctx context.Context) *tracker.JobWithTarget { + if f.calls >= len(f.jobs) { + // Return nil if there are no more jobs. + return nil + } j := f.jobs[f.calls] f.calls++ return &tracker.JobWithTarget{Job: j} @@ -280,4 +284,7 @@ func TestNextJobV2Handler(t *testing.T) { // This one should fail because the fakeJobService returns a duplicate job. postAndExpect(t, &url, http.StatusInternalServerError) + + // Get nil result. + postAndExpect(t, &url, http.StatusInternalServerError) }