devbox/internal/syncer/fsync_test.go
shoemoney 4b6a4b97b2 harden(durability): fsync atomic writes before publishing the rename 💽
A power loss between rename and the data hitting disk could leave a truncated/
empty file despite the atomic rename — breaking 'never lose a byte'. Now both
atomic-write paths f.Sync() the temp file before rename and best-effort fsync
the parent dir after (so the rename entry is durable too):
- internal/syncer/pull.go atomicWrite (pulled file content)
- internal/hub/blobstore Disk.Put (stored blobs)
Dir fsync is best-effort (unsupported on Windows). [fanned-out subagent: harden-fsync]
2026-06-23 02:59:26 -05:00

26 lines
742 B
Go

package syncer
import (
"os"
"path/filepath"
"testing"
)
// TestAtomicWriteWithSync proves the fsync durability path doesn't break a write:
// the file lands with the exact content and mode, and returns no error. We can't
// simulate a power loss, so this is a regression guard against the sync calls
// corrupting or failing an ordinary write.
func TestAtomicWriteWithSync(t *testing.T) {
dst := filepath.Join(t.TempDir(), "hello.txt")
want := []byte("never lose a byte")
if err := atomicWrite(dst, want, 0o644); err != nil {
t.Fatalf("atomicWrite: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatalf("read back: %v", err)
}
if string(got) != string(want) {
t.Fatalf("content = %q, want %q", got, want)
}
}