diff --git a/seaweed-block/core/hostlimit/tickets_test.go b/seaweed-block/core/hostlimit/tickets_test.go index c73647c4bc..1aac20bffe 100644 --- a/seaweed-block/core/hostlimit/tickets_test.go +++ b/seaweed-block/core/hostlimit/tickets_test.go @@ -106,7 +106,8 @@ func TestDeadEarlierTicketIsRemoved(t *testing.T) { func TestStalledEarlierTicketIsSkipped(t *testing.T) { c := testConfig(t) a, b := newTest(t, c), newTest(t, c) - b.testSkipAfter = 300 * time.Millisecond + const skip = 300 * time.Millisecond + b.testSkipAfter = skip now := HostNow() stalled, _ := a.TakeTicket(now - 2e9) defer stalled.Close() @@ -115,22 +116,34 @@ func TestStalledEarlierTicketIsSkipped(t *testing.T) { start := time.Now() var id uint64 var err error + var refused []time.Duration // when each refused attempt began, from the first + var passedAt time.Duration for { + at := time.Since(start) if id, err = admitWith(b, late); err == nil { + passedAt = at break } if !errors.Is(err, ErrNotHead) { t.Fatalf("refusal: %v", err) } + refused = append(refused, at.Round(time.Millisecond)) if time.Since(start) > 3*time.Second { t.Fatal("never passed the stalled ticket") } time.Sleep(20 * time.Millisecond) } - el := time.Since(start) b.Release(id) - if el < 300*time.Millisecond || el > 700*time.Millisecond { - t.Fatalf("passed the stalled ticket after %s; want the skip time plus a poll", el) + // The property, independent of how late a loaded host runs the polls: + // no attempt passes before the skip time, and the first attempt that + // begins after it passes (a small allowance for the skip judge's own + // clock); the attempts' start times are printed on failure. + const judge = 50 * time.Millisecond + if passedAt < skip { + t.Fatalf("passed the stalled ticket at %s, before the skip time %s (refused at %v)", passedAt.Round(time.Millisecond), skip, refused) + } + if n := len(refused); n > 0 && refused[n-1] >= skip+judge { + t.Fatalf("an attempt begun at %s, after the skip time %s, was refused (refused at %v, passed at %s)", refused[n-1], skip, refused, passedAt.Round(time.Millisecond)) } if late.Skipped() != 1 { t.Fatalf("skipped %d tickets", late.Skipped()) @@ -211,14 +224,32 @@ func TestUrgentProceedsPastAStalledProbe(t *testing.T) { t.Fatal(err) } start := time.Now() - id, err := l.Acquire(context.Background(), Urgent, nil) - if err != nil { - t.Fatalf("urgent admission behind a stalled probe: %v", err) + type admitted struct { + id uint64 + err error + el time.Duration } - el := time.Since(start) - l.Release(id) - if el < urgentHoldWait || el > urgentHoldWait+300*time.Millisecond { - t.Fatalf("urgent admission took %s; want about %s", el, urgentHoldWait) + got := make(chan admitted, 1) + go func() { + id, err := l.Acquire(context.Background(), Urgent, nil) + got <- admitted{id, err, time.Since(start)} + }() + // Blocked behind the stalled probe, the admission would not return while + // the probe is held; bounded by its hold wait, it returns after about + // urgentHoldWait. The outer bound is ten hold waits, so a loaded host's + // scheduling does not fail it, while a block still does. + var a admitted + select { + case a = <-got: + case <-time.After(10 * urgentHoldWait): + t.Fatalf("urgent admission still waiting %s behind a stalled probe; want about %s", 10*urgentHoldWait, urgentHoldWait) + } + if a.err != nil { + t.Fatalf("urgent admission behind a stalled probe: %v", a.err) + } + l.Release(a.id) + if a.el < urgentHoldWait { + t.Fatalf("urgent admission took %s, less than its hold wait %s", a.el, urgentHoldWait) } if st := l.Snapshot().(Stats); st.UrgentPendingUnheld != 1 { t.Fatalf("UrgentPendingUnheld = %d", st.UrgentPendingUnheld)