mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-15 04:30:55 +08:00
New restores every unfinished task in the shared database and runs its planner, worker and deadline loops on the server context. Tests that passed context.Background() never stopped or joined them, so they kept writing under the manager TempDir after the test returned and the TempDir cleanup failed with directory not empty. Add stopTestServer, which cancels the context, waits for each task to go quiescent, joins its loops, the archive worker and the side-question writer, and defer it right after New in every test that started a live server.
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestWorkerControlRoutes(t *testing.T) {
|
|
m, err := NewManager(t.TempDir(), "")
|
|
if err != nil {
|
|
t.Skipf("postgres unavailable (%v) - skipping", err)
|
|
}
|
|
defer m.Close()
|
|
|
|
td := t.TempDir()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
s := New(ctx, m, td, td, td)
|
|
defer stopTestServer(t, s, cancel)
|
|
h := s.Handler()
|
|
token, err := signJWT(s.jwtKey)
|
|
if err != nil {
|
|
t.Fatalf("sign jwt: %v", err)
|
|
}
|
|
|
|
request := func(path string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"action":"pause"}`))
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
// The single-worker route is still registered and reaches its JSON handler.
|
|
single := request("/api/tasks/missing/intents/1/control")
|
|
if single.Code != http.StatusNotFound || !strings.Contains(single.Body.String(), `"error":"task not found"`) {
|
|
t.Fatalf("single control route unavailable: status=%d body=%s", single.Code, single.Body.String())
|
|
}
|
|
|
|
// The former batch route must fall through the mux instead of reaching a task handler.
|
|
batch := request("/api/tasks/missing/intents/control/batch")
|
|
if batch.Code != http.StatusNotFound || !strings.Contains(batch.Body.String(), "404 page not found") {
|
|
t.Fatalf("batch control route still registered: status=%d body=%s", batch.Code, batch.Body.String())
|
|
}
|
|
}
|