package api_test import ( "context" "testing" "time" "github.com/google/uuid" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" ) func TestLongPollsEndWhenDraining(t *testing.T) { e := newEnv(t) tok, _ := e.platformChecker(t, uniq("eu-")) drain := make(chan struct{}) e.api.EndLongPollsOn(drain) client := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "test").WithHTTPClient(e.srv.Client()) go func() { time.Sleep(300 * time.Millisecond); close(drain) }() start := time.Now() jobs, err := client.Jobs(context.Background(), 10, 20*time.Second) if err != nil || len(jobs) != 0 { t.Fatalf("Jobs() while draining = %v, %v; want no jobs and no error", jobs, err) } if took := time.Since(start); took > 3*time.Second { t.Errorf("a long poll ran %v after draining began, want it to end within about a second", took) } // Once draining, polls answer at once. start = time.Now() if _, err := client.Jobs(context.Background(), 10, 20*time.Second); err != nil || time.Since(start) > 2*time.Second { t.Errorf("a poll on a draining instance took %v (%v)", time.Since(start), err) } }