fork/gdu

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

ShivamB25feat: stop scanning and keep partial results (#606)99a7f2a

subvolume
2.9 KiB114 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// readLockedTree mirrors preview traversal: every mutable directory is read
88// through GetFilesLocked and its synchronized scalar accessors.
89func readLockedTree(root *Dir) {
90	_ = root.GetName()
91	_ = root.GetFlag()
92	_ = root.GetSize()
93	_ = root.GetUsage()
94	_ = root.GetMtime()
95	_ = root.GetItemCount()
96	for item := range root.GetFilesLocked(fs.SortBySize, fs.SortDesc) {
97		_ = item.GetName()
98		_ = item.GetFlag()
99		_ = item.GetSize()
100		_ = item.GetUsage()
101		_ = item.GetMtime()
102		_ = item.GetItemCount()
103		if child, ok := item.(*Dir); ok {
104			for nested := range child.GetFilesLocked(fs.SortByName, fs.SortAsc) {
105				_ = nested.GetName()
106				_ = nested.GetFlag()
107				_ = nested.GetSize()
108				_ = nested.GetUsage()
109				_ = nested.GetMtime()
110				_ = nested.GetItemCount()
111			}
112		}
113	}
114}