fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
subvolume
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 ) { 17const rounds = 256 18const mutationsPerRound = 8 19 20root := & Dir { 21File :& File {Name :"root" }, 22Files :make (fs.Files ,0 ,rounds ), 23 } 24analyzer := CreateAnalyzer () 25analyzer .setCurrentDir (root ) 26 27for round := range rounds { 28start := make (chan struct {}) 29var workers sync.WaitGroup 30workers .Add (2 ) 31 32go func (round int ) { 33defer workers .Done () 34<- start 35 36child := & Dir { 37File :& File { 38Name :fmt .Sprintf ("dir-%03d" ,round ), 39Mtime :time .Unix (int64 (round ),0 ), 40Parent :root , 41 }, 42Files :make (fs.Files ,0 ,mutationsPerRound ), 43 } 44root .AddFile (child ) 45for mutation := range mutationsPerRound { 46child .AddFile (& File { 47Name :fmt .Sprintf ("file-%03d-%02d" ,round ,mutation ), 48Size :int64 (mutation + 1 ), 49Usage :int64 (mutation + 1 ), 50Mli :uint64 (round + 1 ), 51Mtime :time .Unix (int64 (round ),int64 (mutation )), 52Parent :child , 53 }) 54child .SetFlag ('!' ) 55child .UpdateStats (make (fs.HardLinkedItems )) 56root .UpdateStats (make (fs.HardLinkedItems )) 57runtime .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. 63go func () { 64defer workers .Done () 65<- start 66for range mutationsPerRound { 67root .UpdateStats (make (fs.HardLinkedItems )) 68runtime .Gosched () 69 } 70 }() 71 72close (start ) 73current ,ok := analyzer .GetCurrentDir ().(* Dir ) 74if ! ok { 75t .Fatal ("current directory unavailable during partial scan" ) 76 } 77readLockedTree (current ) 78workers .Wait () 79 } 80 81root .UpdateStats (make (fs.HardLinkedItems )) 82if got ,want := root .GetItemCount (),int64 (1 + rounds * (mutationsPerRound + 1 ));got != want { 83t .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 () 96for 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 () 103if child ,ok := item .(* Dir );ok { 104for 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}