diff --git a/seaweed-block/core/frontend/iscsi/legacy_write_budget_test.go b/seaweed-block/core/frontend/iscsi/legacy_write_budget_test.go index 75a27803a9..440566808c 100644 --- a/seaweed-block/core/frontend/iscsi/legacy_write_budget_test.go +++ b/seaweed-block/core/frontend/iscsi/legacy_write_budget_test.go @@ -2,9 +2,11 @@ package iscsi import ( "context" - "github.com/seaweedfs/seaweed-block/core/frontend" "testing" + "testing/synctest" "time" + + "github.com/seaweedfs/seaweed-block/core/frontend" ) type serialBudgetBackend struct { @@ -22,11 +24,16 @@ func (b *serialBudgetBackend) WriteCommandContext(ctx context.Context, _ time.Ti return context.WithTimeout(ctx, 25*time.Second) } func TestSerialISCSIWriteKeepsLegacyBudget(t *testing.T) { - b := &serialBudgetBackend{} - h := &SCSIHandler{backend: b, blockSize: 4096, volumeSize: 4096} - start := time.Now() - r := h.doWrite(context.Background(), 0, 1, make([]byte, 4096)) - if r.Status != StatusGood || !b.legacy || b.deadline.IsZero() || b.deadline.After(start.Add(4*time.Second)) { - t.Fatalf("serial write status=%d legacy=%v deadline=%v", r.Status, b.legacy, b.deadline) - } + // Budget creation happens after doWrite is called. Freeze time so that + // scheduling between this timestamp and WithTimeout cannot make the correct + // four-second budget look longer than four seconds. + synctest.Test(t, func(t *testing.T) { + b := &serialBudgetBackend{} + h := &SCSIHandler{backend: b, blockSize: 4096, volumeSize: 4096} + start := time.Now() + r := h.doWrite(context.Background(), 0, 1, make([]byte, 4096)) + if r.Status != StatusGood || !b.legacy || b.deadline.IsZero() || b.deadline.After(start.Add(4*time.Second)) { + t.Fatalf("serial write status=%d legacy=%v deadline=%v", r.Status, b.legacy, b.deadline) + } + }) }