75 lines
2.9 KiB
Go
75 lines
2.9 KiB
Go
//go:build integration
|
|
|
|
package redisactivity
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
controllerHealth "proxy-pool/internal/controller/health"
|
|
"proxy-pool/internal/domain/activitypool"
|
|
healthDomain "proxy-pool/internal/domain/health"
|
|
proxyDomain "proxy-pool/internal/domain/proxy"
|
|
)
|
|
|
|
func TestRedisHealthTasksLeaseAndRescheduleBasicChecks(t *testing.T) {
|
|
fixture := newRedisTestFixture(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
if _, err := fixture.Adapter.UpsertFetched(ctx, "provider-a", activitypool.FetchedBatch{
|
|
ObservedAt: now, ConfiguredTTL: time.Minute, MaxSize: 1,
|
|
Proxies: []proxyDomain.Proxy{testProxy("proxy-a", "192.0.2.10")},
|
|
}); err != nil {
|
|
t.Fatalf("UpsertFetched(): %v", err)
|
|
}
|
|
|
|
candidates, err := fixture.Adapter.DueCandidates(ctx, now, 1)
|
|
if err != nil || len(candidates) != 1 || candidates[0].ProxyID != "proxy-a" || candidates[0].State != proxyDomain.StateFetched {
|
|
t.Fatalf("DueCandidates() = (%+v, %v)", candidates, err)
|
|
}
|
|
planner, err := controllerHealth.NewPlanner(controllerHealth.SchedulePolicy{
|
|
Interval: 30 * time.Second, MaxInFlight: 1, Timeout: 5 * time.Second, MaxAttempts: 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("newHealthTaskPlanner(): %v", err)
|
|
}
|
|
plannedAt := now
|
|
plans, err := planner.Plan(plannedAt, 0, 1, candidates)
|
|
if err != nil || len(plans) != 1 {
|
|
t.Fatalf("Plan() = (%+v, %v)", plans, err)
|
|
}
|
|
if offered, err := fixture.Adapter.Offer(ctx, plans); err != nil || offered != 1 {
|
|
t.Fatalf("Offer() = (%d, %v)", offered, err)
|
|
}
|
|
|
|
claimed, err := fixture.Adapter.Claim(ctx, healthDomain.TaskClaim{
|
|
CheckerID: "checker-a", InstanceID: "instance-a", MaxInFlight: 1,
|
|
SupportedLevels: []healthDomain.Level{healthDomain.LevelBasic},
|
|
})
|
|
if err != nil || len(claimed) != 1 || claimed[0].ProxyID != "proxy-a" || claimed[0].LeaseToken == "" {
|
|
t.Fatalf("Claim() = (%+v, %v)", claimed, err)
|
|
}
|
|
observation := healthDomain.Observation{
|
|
TaskID: claimed[0].TaskID, ProxyID: claimed[0].ProxyID, Level: healthDomain.LevelBasic,
|
|
Success: true, Latency: time.Millisecond, ObservedAt: time.Now().UTC(),
|
|
}
|
|
if err := fixture.Adapter.AuthorizeObservation(ctx, "checker-a", claimed[0].LeaseToken, observation, time.Now().UTC()); err != nil {
|
|
t.Fatalf("AuthorizeObservation(): %v", err)
|
|
}
|
|
updated, err := fixture.Adapter.ApplyGlobalObservation(ctx, activitypool.GlobalHealthCommand{
|
|
Observation: observation, MaxConsecutiveFailures: 2,
|
|
})
|
|
if err != nil || updated.State != proxyDomain.StateAvailable {
|
|
t.Fatalf("ApplyGlobalObservation() = (%+v, %v)", updated, err)
|
|
}
|
|
if err := fixture.Adapter.CompleteObservation(ctx, "checker-a", claimed[0].LeaseToken, observation, time.Now().UTC()); err != nil {
|
|
t.Fatalf("CompleteObservation(): %v", err)
|
|
}
|
|
|
|
next, err := fixture.Adapter.DueCandidates(ctx, plans[0].NextDue, 1)
|
|
if err != nil || len(next) != 1 || next[0].ProxyID != "proxy-a" || next[0].State != proxyDomain.StateAvailable {
|
|
t.Fatalf("DueCandidates(next) = (%+v, %v)", next, err)
|
|
}
|
|
}
|