返回 DeepSeek-Reasonix
observation_review_test.go
根目录 / internal / fileops / observation_review_test.go
1 package fileops
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8 )
9
10 func TestMutationLockSurvivesReplacement(t *testing.T) {
11 path := filepath.Join(t.TempDir(), "file")
12 if err := os.WriteFile(path, []byte("old"), 0o600); err != nil {
13 t.Fatal(err)
14 }
15 info, _ := os.Stat(path)
16 unlock := Lock(DiskTarget(path, info))
17 if err := os.Rename(path, path+".old"); err != nil {
18 unlock()
19 t.Fatal(err)
20 }
21 if err := os.WriteFile(path, []byte("new"), 0o600); err != nil {
22 unlock()
23 t.Fatal(err)
24 }
25 info, _ = os.Stat(path)
26 started, acquired := make(chan struct{}), make(chan struct{})
27 go func() {
28 close(started)
29 release := Lock(DiskTarget(path, info))
30 release()
31 close(acquired)
32 }()
33 <-started
34 select {
35 case <-acquired:
36 unlock()
37 t.Fatal("replacement bypassed the in-flight mutation lock")
38 case <-time.After(50 * time.Millisecond):
39 }
40 unlock()
41 select {
42 case <-acquired:
43 case <-time.After(5 * time.Second):
44 t.Fatal("replacement lock did not release")
45 }
46 }
47
48 func TestLiveObservationCloneIsIndependent(t *testing.T) {
49 first := NewStore()
50 target := OverlayTarget("file")
51 first.ObservePresent(target, "v1")
52 second := first.Clone()
53 first.Forget(target)
54 if got := second.Get(target); got.Kind != Present || got.Version != "v1" {
55 t.Fatalf("live transfer lost observation: %+v", got)
56 }
57 second.ObservePresent(target, "v2")
58 if got := first.Get(target); got.Kind != Unseen {
59 t.Fatalf("replacement runtime mutated old runtime: %+v", got)
60 }
61 }
62
63 type linuxStatInfo struct {
64 os.FileInfo
65 meta any
66 }
67
68 func (s linuxStatInfo) Sys() any { return s.meta }
69
70 func TestDiskVersionIncludesLinuxCtim(t *testing.T) {
71 info, err := os.Stat(t.TempDir())
72 if err != nil {
73 t.Fatal(err)
74 }
75 // Linux syscall.Stat_t uses Ctim, while Darwin uses Ctimespec. Keep this
76 // contract runnable on every host, with identical size, mode and mtime.
77 type timespec struct{ Sec, Nsec int64 }
78 type stat struct{ Ctim timespec }
79 before := linuxStatInfo{info, stat{timespec{1, 10}}}
80 after := linuxStatInfo{info, stat{timespec{1, 11}}}
81 if DiskVersion(before) == DiskVersion(after) {
82 t.Fatal("Linux Ctim nanosecond change was omitted from the version")
83 }
84 }
85
85 lines GO