122 lines
5.0 KiB
Go
122 lines
5.0 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var leaseStart = time.Date(2026, time.October, 11, 9, 0, 0, 0, time.UTC)
|
|
|
|
func leaseAttempt() AssessmentAttemptID { return AssessmentAttemptID(uuid.New()) }
|
|
|
|
func TestOnePageHoldsTheWorkSessionAndAnotherIsRefusedUntilItIsClosed(t *testing.T) {
|
|
attempt := leaseAttempt()
|
|
first, second := AssessmentDigest{1}, AssessmentDigest{2}
|
|
|
|
var none AssessmentWorkLease
|
|
if !none.AdmitsHolder(first, leaseStart) {
|
|
t.Fatal("an attempt that nobody holds refused a page")
|
|
}
|
|
held := none.Held(attempt, first, 1, leaseStart)
|
|
if err := held.Validate(); err != nil {
|
|
t.Fatalf("Held().Validate() error = %v", err)
|
|
}
|
|
if held.AdmitsHolder(second, leaseStart.Add(time.Second)) {
|
|
t.Fatal("a second page was admitted while the first one holds the work session")
|
|
}
|
|
if !held.AdmitsHolder(first, leaseStart.Add(time.Second)) {
|
|
t.Fatal("the holder was refused")
|
|
}
|
|
|
|
// Closing the page frees the session at once.
|
|
released := held.ReleasedBy(first, leaseStart.Add(5*time.Second))
|
|
if released.ActiveAt(leaseStart.Add(6*time.Second)) || !released.AdmitsHolder(second, leaseStart.Add(6*time.Second)) {
|
|
t.Fatal("the session is still held after the holder released it")
|
|
}
|
|
if err := released.Validate(); err != nil {
|
|
t.Fatalf("released lease Validate() error = %v", err)
|
|
}
|
|
// The second page takes it over as a new acquisition.
|
|
taken := released.Held(attempt, second, 1, leaseStart.Add(7*time.Second))
|
|
if taken.Holder != second || !taken.AcquiredAt.Equal(leaseStart.Add(7*time.Second)) || !taken.ReleasedAt.IsZero() {
|
|
t.Fatalf("takeover = %+v, want a fresh hold of the second page", taken)
|
|
}
|
|
}
|
|
|
|
func TestAHoldExpiresWhenThePageStopsRenewing(t *testing.T) {
|
|
attempt := leaseAttempt()
|
|
first, second := AssessmentDigest{1}, AssessmentDigest{2}
|
|
held := AssessmentWorkLease{}.Held(attempt, first, 1, leaseStart)
|
|
|
|
before := leaseStart.Add(AssessmentWorkLeaseDuration - time.Second)
|
|
if held.AdmitsHolder(second, before) {
|
|
t.Fatal("the hold ended before its duration")
|
|
}
|
|
if got, want := held.HeldUntil(), leaseStart.Add(AssessmentWorkLeaseDuration); !got.Equal(want) {
|
|
t.Fatalf("HeldUntil() = %s, want %s", got, want)
|
|
}
|
|
// A renewal by the holder moves the end and keeps the acquisition time.
|
|
renewed := held.Held(attempt, first, 1, leaseStart.Add(time.Minute))
|
|
if !renewed.AcquiredAt.Equal(leaseStart) || !renewed.RenewedAt.Equal(leaseStart.Add(time.Minute)) {
|
|
t.Fatalf("renewal = %+v, want the first acquisition time and a new renewal time", renewed)
|
|
}
|
|
if renewed.AdmitsHolder(second, leaseStart.Add(time.Minute+AssessmentWorkLeaseDuration-time.Second)) {
|
|
t.Fatal("a renewed hold ended early")
|
|
}
|
|
// Without a renewal the session is free once the duration has passed.
|
|
if !held.AdmitsHolder(second, leaseStart.Add(AssessmentWorkLeaseDuration)) {
|
|
t.Fatal("an expired hold refused another page")
|
|
}
|
|
// The holder coming back after an expiry is a new acquisition, not a renewal.
|
|
again := held.Held(attempt, first, 1, leaseStart.Add(time.Hour))
|
|
if !again.AcquiredAt.Equal(leaseStart.Add(time.Hour)) {
|
|
t.Fatalf("a hold after expiry kept the old acquisition time: %+v", again)
|
|
}
|
|
}
|
|
|
|
func TestOnlyTheHolderReleasesAndAReleaseNeverRevealsAnything(t *testing.T) {
|
|
attempt := leaseAttempt()
|
|
first, second := AssessmentDigest{1}, AssessmentDigest{2}
|
|
held := AssessmentWorkLease{}.Held(attempt, first, 1, leaseStart)
|
|
|
|
if other := held.ReleasedBy(second, leaseStart.Add(time.Second)); other != held {
|
|
t.Fatalf("another page changed the lease: %+v", other)
|
|
}
|
|
once := held.ReleasedBy(first, leaseStart.Add(time.Second))
|
|
if twice := once.ReleasedBy(first, leaseStart.Add(2*time.Second)); twice != once {
|
|
t.Fatalf("a second release changed the lease: %+v", twice)
|
|
}
|
|
var none AssessmentWorkLease
|
|
if got := none.ReleasedBy(first, leaseStart); got != none {
|
|
t.Fatalf("releasing a lease that does not exist changed it: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestWorkLeaseValidationRejectsIncompleteLeases(t *testing.T) {
|
|
attempt := leaseAttempt()
|
|
valid := AssessmentWorkLease{}.Held(attempt, AssessmentDigest{1}, 1, leaseStart)
|
|
for name, change := range map[string]func(*AssessmentWorkLease){
|
|
"no attempt": func(lease *AssessmentWorkLease) { lease.AttemptID = AssessmentAttemptID{} },
|
|
"no holder": func(lease *AssessmentWorkLease) { lease.Holder = AssessmentDigest{} },
|
|
"no key version": func(lease *AssessmentWorkLease) { lease.KeyVersion = 0 },
|
|
"renewed before held": func(lease *AssessmentWorkLease) { lease.RenewedAt = lease.AcquiredAt.Add(-time.Second) },
|
|
"released before held": func(lease *AssessmentWorkLease) {
|
|
lease.ReleasedAt = lease.AcquiredAt.Add(-time.Second)
|
|
},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
lease := valid
|
|
change(&lease)
|
|
if err := lease.Validate(); !errors.Is(err, ErrInvalidAssessmentWorkLease) {
|
|
t.Fatalf("Validate() error = %v, want ErrInvalidAssessmentWorkLease", err)
|
|
}
|
|
})
|
|
}
|
|
if AssessmentWorkLeaseRenewEvery*4 >= AssessmentWorkLeaseDuration {
|
|
t.Fatal("a page renews less than four times within a hold, so one slow timer would lose the session")
|
|
}
|
|
}
|