46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"orchestra/internal/domain"
|
|
"orchestra/internal/registry"
|
|
"orchestra/internal/store"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type reachable struct{}
|
|
|
|
func (reachable) Reachable(string, time.Duration) bool { return true }
|
|
|
|
func TestAssignsByAffinityCapabilityAndConcurrency(t *testing.T) {
|
|
s, err := store.Open(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := registry.New(registry.Config{
|
|
Projects: []registry.Project{{ID: "p", MachineAffinity: []string{"m"}}},
|
|
Machines: []registry.Machine{{ID: "m", Address: "unused"}},
|
|
Herdrs: []registry.Herdr{{ID: "h", MachineID: "m", Capabilities: []string{"go"}, Concurrency: 1}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
makeTask := func(id string) {
|
|
b, _ := json.Marshal(map[string]any{"source": "test", "external_id": id, "project": "p", "capability": []string{"go"}})
|
|
if err := s.Append(domain.Event{ID: id, TaskID: id, Type: "TaskCreated", Version: 1, Payload: b}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
makeTask("a")
|
|
makeTask("b")
|
|
rt := Router{Store: s, Registry: r, Reachability: reachable{}}
|
|
got, err := rt.AssignPending()
|
|
if err != nil || len(got) != 1 {
|
|
t.Fatalf("assigned %d events, err=%v", len(got), err)
|
|
}
|
|
if s.Tasks()[0].State != domain.StateLeased && s.Tasks()[1].State != domain.StateLeased {
|
|
t.Fatal("no task leased")
|
|
}
|
|
}
|