test: use a fixed clock for legacy iSCSI budget

This commit is contained in:
pingqiu committed 2026-12-02 07:57:59 -08:00
1 parent 9f3afc3c71
commit 39f9f1878b
1 file changed
+15 -8
@@ -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)
}
})
}