package configread import ( "context" "net/http" "net/http/httptest" "strings" "testing" ) func TestReadAllCurrentTasksContinuesAfterShortPageUntilEmptyPage(t *testing.T) { id := "c046b893-8628-4589-ae50-619d049248a6" queries := []string{} server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/internal/v1/dispatcher/tasks" { t.Errorf("path=%s", r.URL.Path) } queries = append(queries, r.URL.RawQuery) w.Header().Set("Content-Type", "application/json") switch r.URL.Query().Get("after") { case "": _, _ = w.Write([]byte(`{"dispatcher_id":"` + id + `","cursor":"p-1","tasks":[{"task_id":"task-asr","tenant_id":1001,"status":"running","task_revision":1}]}`)) case "p-1": _, _ = w.Write([]byte(`{"dispatcher_id":"` + id + `","cursor":"p-2","tasks":[{"task_id":"task-full","tenant_id":1001,"status":"paused","task_revision":2}]}`)) case "p-2": _, _ = w.Write([]byte(`{"dispatcher_id":"` + id + `","cursor":"p-end","tasks":[]}`)) default: t.Errorf("unexpected cursor %q", r.URL.RawQuery) } })) defer server.Close() client, err := NewClient(server.URL, id, "test-secret", server.Client()) if err != nil { t.Fatal(err) } list, cursor, err := client.ReadAllCurrentTasks(context.Background()) if err != nil { t.Fatal(err) } if cursor != "p-end" || len(list) != 2 || list[0].TenantID != 1001 || list[1].TaskID != "task-full" || strings.Join(queries, ",") != ",after=p-1,after=p-2" { t.Fatalf("short page ended discovery early: cursor=%q tasks=%+v queries=%v", cursor, list, queries) } } func TestReadCurrentTasksFailClosed(t *testing.T) { id := "c046b893-8628-4589-ae50-619d049248a6" for _, tc := range []struct { name string body string status int after string }{ {"string tenant", `{"dispatcher_id":"` + id + `","cursor":"next","tasks":[{"task_id":"task-a","tenant_id":"1001","status":"running","task_revision":1}]}`, 200, ""}, {"missing cursor", `{"dispatcher_id":"` + id + `","tasks":[]}`, 200, ""}, {"old snapshot", `{"dispatcher_id":"` + id + `","cursor":"next","snapshot_id":"old","tasks":[]}`, 200, ""}, {"wrong owner", `{"dispatcher_id":"c046b893-8628-4589-ae50-619d049248a7","cursor":"next","tasks":[]}`, 200, ""}, {"same cursor with tasks", `{"dispatcher_id":"` + id + `","cursor":"p-1","tasks":[{"task_id":"task-a","tenant_id":1001,"status":"running","task_revision":1}]}`, 200, "p-1"}, {"duplicate in page", `{"dispatcher_id":"` + id + `","cursor":"next","tasks":[{"task_id":"task-a","tenant_id":1001,"status":"running","task_revision":1},{"task_id":"task-a","tenant_id":1001,"status":"paused","task_revision":2}]}`, 200, ""}, {"service unavailable", `{"resource":"error","error":{"code":"unavailable","message":"try later"}}`, 503, ""}, } { t.Run(tc.name, func(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(tc.status) _, _ = w.Write([]byte(tc.body)) })) defer server.Close() client, err := NewClient(server.URL, id, "test-secret", server.Client()) if err != nil { t.Fatal(err) } if _, err := client.ReadCurrentTasks(context.Background(), tc.after); err == nil { t.Fatal("unexpectedly accepted invalid discovery page") } }) } }