test: cover payload opacity, source bounds and concurrent v3 cursor allocation

This commit is contained in:
pingqiu committed 2026-11-17 02:03:59 -08:00
1 parent 7fb5e8798c
commit 7e4fe4a2b9
2 files changed
+84 -4

No files matched your search

+14 -4
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math"
"os"
"sort"
"sync"
@@ -29,7 +30,6 @@ func (p *descriptorPublisherV3) walSnapshotV3() (descriptorV3, uint64, bool) {
type walWriterV3 struct {
mu sync.Mutex
pub *descriptorPublisherV3
fd *os.File
nonce [16]byte
head uint64
failed error
@@ -41,10 +41,13 @@ func newWALWriterV3(fd *os.File, pub *descriptorPublisherV3) (*walWriterV3, erro
if err := d.validate(pub.accept); err != nil {
return nil, err
}
if d.WALSize > uint64(math.MaxInt64)-d.WALOffset {
return nil, ErrCounterExhausted
}
if !ok || d.MigrationPhase != MigrationNormal {
return nil, errWALBindingV3
}
return &walWriterV3{pub: pub, fd: fd, nonce: d.Nonce, head: d.RetainedHead, writeAt: fd.WriteAt}, nil
return &walWriterV3{pub: pub, nonce: d.Nonce, head: d.RetainedHead, writeAt: fd.WriteAt}, nil
}
func (w *walWriterV3) append(e walEntryV3, durableLSN uint64) (uint64, error) {
@@ -161,11 +164,18 @@ type walScanResultV3 struct {
StopCause error
}
func scanWALV3(fd io.ReaderAt, d descriptorV3) (walScanResultV3, error) {
func scanWALV3(fd *os.File, d descriptorV3) (walScanResultV3, error) {
r := walScanResultV3{Nonce: d.Nonce}
if err := d.validate(nil); err != nil {
return r, err
}
info, err := fd.Stat()
if err != nil {
return r, err
}
if info.Size() < 0 || d.WALSize > uint64(info.Size()) || d.WALOffset > uint64(info.Size())-d.WALSize {
return r, io.ErrUnexpectedEOF
}
if d.MigrationPhase != MigrationNormal {
return r, ErrMigrationIncomplete
}
@@ -219,7 +229,7 @@ func scanWALV3(fd io.ReaderAt, d descriptorV3) (walScanResultV3, error) {
break
}
size := uint64(walEntryHeaderSizeV3) + n
if size > remaining || size > limit-cursor {
if (e.Type == walEntryPadding && size != remaining) || size > remaining || size > limit-cursor {
r.StopCause = errEntryTruncated
break
}
@@ -10,6 +10,7 @@ import (
"path/filepath"
"sync"
"testing"
"time"
)
// A real file with row-1 slots and row-3 publisher; no v2 constructor or
@@ -246,3 +247,72 @@ func TestWALIOV3_FloorAndPwriteFailureRefuse(t *testing.T) {
t.Fatal("failed append retried physical I/O")
}
}
func TestWALIOV3_EmbeddedRecordInPayloadIsNotScanned(t *testing.T) {
f, p, w := newWALIOTestV3(t, 0, 0)
d := p.current()
inner, err := encodeWALEntryV3(walEntryV3{LSN: 99, Type: walEntryBarrier, DurableLSN: 88, LogicalStart: 100, Nonce: d.Nonce}, d.BlockSize, d.VolumeSize/uint64(d.BlockSize))
if err != nil {
t.Fatal(err)
}
data := make([]byte, d.BlockSize)
copy(data[100:], inner)
if _, err := w.append(walEntryV3{LSN: 1, Type: walEntryWrite, LBA: 4, Length: d.BlockSize, Data: data}, 0); err != nil {
t.Fatal(err)
}
r, err := scanWALV3(f, d)
if err != nil {
t.Fatal(err)
}
if len(r.Records) != 1 || r.Frontier != 1 || r.HighestLSN != 1 || r.MaxDurableLSN != 0 || !bytes.Equal(r.Records[0].Entry.Data, data) {
t.Fatalf("embedded payload interpreted as WAL: %+v", r)
}
}
func TestWALIOV3_ConcurrentAppendsHaveUniqueCursors(t *testing.T) {
f, p, w := newWALIOTestV3(t, 0, 0)
d := p.current()
done := make(chan error, 40)
for n := uint64(1); n <= 40; n++ {
go func(lsn uint64) { _, err := w.append(walEntryV3{LSN: lsn, Type: walEntryBarrier}, 0); done <- err }(n)
}
deadline := time.NewTimer(5 * time.Second)
defer deadline.Stop()
for n := 0; n < 40; n++ {
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-deadline.C:
t.Fatal("bounded append completion timed out")
}
}
r, err := scanWALV3(f, d)
if err != nil {
t.Fatal(err)
}
if len(r.Records) != 40 || r.Frontier != 40 || r.End != 40*70 {
t.Fatalf("concurrent scan=%+v", r)
}
}
func TestWALIOV3_CounterRefusalBeforePwrite(t *testing.T) {
_, _, w := newWALIOTestV3(t, counterMaxV3-30, counterMaxV3-30)
calls := 0
w.writeAt = func(b []byte, off int64) (int, error) { calls++; return len(b), nil }
if _, err := w.append(walEntryV3{LSN: 1, Type: walEntryBarrier}, 0); !errors.Is(err, ErrCounterExhausted) || calls != 0 {
t.Fatalf("counter error=%v writes=%d", err, calls)
}
}
func TestWALIOV3_ScanRequiresActualSourceBounds(t *testing.T) {
f, p, _ := newWALIOTestV3(t, 0, 0)
d := p.current()
if err := f.Truncate(int64(d.WALOffset + walEntryPrefixSizeV3)); err != nil {
t.Fatal(err)
}
if _, err := scanWALV3(f, d); !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("truncated source bounds error=%v", err)
}
}