71 lines
3.0 KiB
Go
71 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func TestWorkerDispatchCandidateContract(t *testing.T) {
|
|
check := func(t *testing.T, s Store) {
|
|
ctx := context.Background()
|
|
now := time.Now().UTC()
|
|
for _, r := range []domain.RunRecord{
|
|
{ID: "old", WorkflowID: "a", CreatedAt: "2020-01-01T00:00:00Z"},
|
|
{ID: "new", WorkflowID: "b", CreatedAt: "2021-01-01T00:00:00Z"},
|
|
{ID: "release", WorkflowID: "a", Trigger: domain.RunTriggerHumanRelease, CreatedAt: "2022-01-01T00:00:00Z"},
|
|
{ID: "future", WorkflowID: "a", CreatedAt: "2010-01-01T00:00:00Z", AvailableAt: now.Add(time.Hour).Format(time.RFC3339Nano)},
|
|
{ID: "leased", WorkflowID: "a", CreatedAt: "2010-01-01T00:00:00Z", ExecutionLeaseOwner: "live", ExecutionLeaseExpiresAt: now.Add(time.Hour).Format(time.RFC3339Nano)},
|
|
{ID: "expired", WorkflowID: "b", CreatedAt: "2023-01-01T00:00:00Z", ExecutionLeaseOwner: "dead", ExecutionLeaseExpiresAt: now.Add(-time.Hour).Format(time.RFC3339Nano)},
|
|
} {
|
|
r.Status = domain.RunStatusQueued
|
|
if err := s.UpsertRun(ctx, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
page, err := s.ListQueuedRunsForDispatch(ctx, WorkerDispatchQuery{Now: now, Limit: 1})
|
|
if err != nil || len(page) != 1 || page[0].ID != "old" {
|
|
t.Fatalf("FIFO/eligibility: %+v %v", page, err)
|
|
}
|
|
page, err = s.ListQueuedRunsForDispatch(ctx, WorkerDispatchQuery{Now: now, ExcludedWorkflowIDs: []string{"a"}, After: &domain.RunSummaryCursor{CreatedAt: page[0].CreatedAt, ID: page[0].ID}, Limit: 100})
|
|
if err != nil || len(page) != 3 || page[0].ID != "new" || page[1].ID != "release" || page[2].ID != "expired" {
|
|
t.Fatalf("cursor, release bypass and expired lease: %+v %v", page, err)
|
|
}
|
|
claimed, err := s.ClaimQueuedRun(ctx, "expired", "restarted", time.Minute)
|
|
if err != nil || claimed == nil {
|
|
t.Fatalf("expired lease recovery: %+v %v", claimed, err)
|
|
}
|
|
duplicate, err := s.ClaimQueuedRun(ctx, "expired", "other", time.Minute)
|
|
if err != nil || duplicate != nil {
|
|
t.Fatalf("duplicate claim: %+v %v", duplicate, err)
|
|
}
|
|
}
|
|
t.Run("memory", func(t *testing.T) { check(t, NewMemoryStore()) })
|
|
t.Run("local-mongo", func(t *testing.T) {
|
|
if os.Getenv("WORKER_LOCAL_MONGO_TEST") != "1" {
|
|
t.Skip("local snapshot Mongo opt-in")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
c, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Disconnect(context.Background())
|
|
db := c.Database(fmt.Sprintf("worker_dispatch_local_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
_, err = db.Collection("runs").Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{Key: "id", Value: 1}}, Options: options.Index().SetUnique(true)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
check(t, NewMongoStore(db))
|
|
})
|
|
}
|