fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildefix: StoreDir fixes (#644)35cba00

master
4.6 KiB183 linesraw
1package analyze
2
3import (
4	"fmt"
5	"runtime"
6	"sync"
7	"testing"
8	"time"
9
10	"github.com/dundee/gdu/v5/pkg/fs"
11)
12
13// TestPartialTreeLockedAccessors exercises the preview contract against the
14// same mutations a scan performs while constructing and updating directories.
15// The fixed round barriers make the stress repeatable without timing sleeps.
16func TestPartialTreeLockedAccessors(t *testing.T) {
17	const rounds = 256
18	const mutationsPerRound = 8
19
20	root := &Dir{
21		File:  &File{Name: "root"},
22		Files: make(fs.Files, 0, rounds),
23	}
24	analyzer := CreateAnalyzer()
25	analyzer.setCurrentDir(root)
26
27	for round := range rounds {
28		start := make(chan struct{})
29		var workers sync.WaitGroup
30		workers.Add(2)
31
32		go func(round int) {
33			defer workers.Done()
34			<-start
35
36			child := &Dir{
37				File: &File{
38					Name:   fmt.Sprintf("dir-%03d", round),
39					Mtime:  time.Unix(int64(round), 0),
40					Parent: root,
41				},
42				Files: make(fs.Files, 0, mutationsPerRound),
43			}
44			root.AddFile(child)
45			for mutation := range mutationsPerRound {
46				child.AddFile(&File{
47					Name:   fmt.Sprintf("file-%03d-%02d", round, mutation),
48					Size:   int64(mutation + 1),
49					Usage:  int64(mutation + 1),
50					Mli:    uint64(round + 1),
51					Mtime:  time.Unix(int64(round), int64(mutation)),
52					Parent: child,
53				})
54				child.SetFlag('!')
55				child.UpdateStats(make(fs.HardLinkedItems))
56				root.UpdateStats(make(fs.HardLinkedItems))
57				runtime.Gosched()
58			}
59		}(round)
60
61		// This models a preview-triggered stats update overlapping the analyzer
62		// worker's own update traversal on the same partially built tree.
63		go func() {
64			defer workers.Done()
65			<-start
66			for range mutationsPerRound {
67				root.UpdateStats(make(fs.HardLinkedItems))
68				runtime.Gosched()
69			}
70		}()
71
72		close(start)
73		current, ok := analyzer.GetCurrentDir().(*Dir)
74		if !ok {
75			t.Fatal("current directory unavailable during partial scan")
76		}
77		readLockedTree(current)
78		workers.Wait()
79	}
80
81	root.UpdateStats(make(fs.HardLinkedItems))
82	if got, want := root.GetItemCount(), int64(1+rounds*(mutationsPerRound+1)); got != want {
83		t.Fatalf("partial tree stats = %d, want %d", got, want)
84	}
85}
86
87// TestRemoveFileUpdatesAncestorsUnderTheirOwnLock removes files from a child
88// directory while the root is read through its synchronized accessors. The
89// removal walks up the tree subtracting from every ancestor, so under -race
90// this catches ancestor stats being written while only the child is locked.
91func TestRemoveFileUpdatesAncestorsUnderTheirOwnLock(t *testing.T) {
92	const fileCount = 128
93
94	root := &Dir{
95		File:  &File{Name: "root"},
96		Files: make(fs.Files, 0, 1),
97	}
98	child := &Dir{
99		File:  &File{Name: "child", Parent: root},
100		Files: make(fs.Files, 0, fileCount),
101	}
102	root.AddFile(child)
103	for i := range fileCount {
104		child.AddFile(&File{
105			Name:   fmt.Sprintf("file-%03d", i),
106			Size:   1,
107			Usage:  1,
108			Parent: child,
109		})
110	}
111	root.UpdateStats(make(fs.HardLinkedItems))
112
113	doomed := make(fs.Files, 0, fileCount)
114	for item := range child.GetFilesLocked(fs.SortByName, fs.SortAsc) {
115		doomed = append(doomed, item)
116	}
117
118	stop := make(chan struct{})
119	var workers sync.WaitGroup
120
121	workers.Add(1)
122	go func() {
123		defer workers.Done()
124		for {
125			select {
126			case <-stop:
127				return
128			default:
129			}
130			readLockedTree(root)
131			runtime.Gosched()
132		}
133	}()
134
135	for _, item := range doomed {
136		child.RemoveFile(item)
137		runtime.Gosched()
138	}
139	close(stop)
140	workers.Wait()
141
142	if got, want := child.GetItemCount(), int64(1); got != want {
143		t.Fatalf("child item count = %d, want %d", got, want)
144	}
145	if got, want := root.GetItemCount(), int64(2); got != want {
146		t.Fatalf("root item count = %d, want %d", got, want)
147	}
148	if got, want := root.GetSize(), int64(0); got != want {
149		t.Fatalf("root size = %d, want %d", got, want)
150	}
151	if got, want := root.GetUsage(), int64(0); got != want {
152		t.Fatalf("root usage = %d, want %d", got, want)
153	}
154}
155
156// readLockedTree mirrors preview traversal: every mutable directory is read
157// through GetFilesLocked and its synchronized scalar accessors.
158func readLockedTree(root *Dir) {
159	_ = root.GetName()
160	_ = root.GetFlag()
161	_ = root.GetSize()
162	_ = root.GetUsage()
163	_ = root.GetMtime()
164	_ = root.GetItemCount()
165	for item := range root.GetFilesLocked(fs.SortBySize, fs.SortDesc) {
166		_ = item.GetName()
167		_ = item.GetFlag()
168		_ = item.GetSize()
169		_ = item.GetUsage()
170		_ = item.GetMtime()
171		_ = item.GetItemCount()
172		if child, ok := item.(*Dir); ok {
173			for nested := range child.GetFilesLocked(fs.SortByName, fs.SortAsc) {
174				_ = nested.GetName()
175				_ = nested.GetFlag()
176				_ = nested.GetSize()
177				_ = nested.GetUsage()
178				_ = nested.GetMtime()
179				_ = nested.GetItemCount()
180			}
181		}
182	}
183}