56 lines
2.0 KiB
Go
56 lines
2.0 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/store"
|
|
)
|
|
|
|
func TestV04ControlBacklogClosesAdmissionBeforeTaskConsumption(t *testing.T) {
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "dispatcher.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
now := time.Now().UTC()
|
|
assignment := store.LocalDiscoveredTask{TaskID: localTestTaskID, TenantID: localTestTenantID, TenantKey: localTestTenantKey, Status: "running", TaskRevision: 1}
|
|
if err := st.ApplyLocalTaskSnapshot(localTestDispatcherID, []store.LocalDiscoveredTask{assignment}, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d, err := NewV3(localTestDispatcherID, st, nil, time.Now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
broker := &fakeQueueBroker{controlDrain: func(_ context.Context) (int, error) {
|
|
var ready int
|
|
if err := st.DB().QueryRow(`SELECT ready FROM local_v04_task_discovery_state WHERE dispatcher_id=?`, localTestDispatcherID).Scan(&ready); err != nil || ready != 0 {
|
|
t.Fatalf("control backlog processed with task admission open: ready=%d err=%v", ready, err)
|
|
}
|
|
if _, err := st.SetLocalTaskAdmissionBarrier(localTestDispatcherID, localTestTaskID, localTestTenantID, localTestTenantKey, "paused"); err != nil {
|
|
return 0, err
|
|
}
|
|
return 1, nil
|
|
}}
|
|
queues := newTaskQueueController(d, broker, nil)
|
|
runtime := &LocalV01Runtime{dispatcher: d, queues: queues, controls: &localTaskControlProcessor{}}
|
|
if err := runtime.catchUpControlBeforeTasks(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !broker.controlDrained {
|
|
t.Fatal("control backlog was not processed")
|
|
}
|
|
var ready int
|
|
if err := st.DB().QueryRow(`SELECT ready FROM local_v04_task_discovery_state WHERE dispatcher_id=?`, localTestDispatcherID).Scan(&ready); err != nil || ready != 1 {
|
|
t.Fatalf("admission did not open after backlog: %d %v", ready, err)
|
|
}
|
|
if err := runtime.reconcileTaskQueues(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(broker.started) != 0 {
|
|
t.Fatalf("task queue started despite queued pause: %v", broker.started)
|
|
}
|
|
}
|