fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
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// 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 ) { 92const fileCount = 128 93 94root := & Dir { 95File :& File {Name :"root" }, 96Files :make (fs.Files ,0 ,1 ), 97 } 98child := & Dir { 99File :& File {Name :"child" ,Parent :root }, 100Files :make (fs.Files ,0 ,fileCount ), 101 } 102root .AddFile (child ) 103for i := range fileCount { 104child .AddFile (& File { 105Name :fmt .Sprintf ("file-%03d" ,i ), 106Size :1 , 107Usage :1 , 108Parent :child , 109 }) 110 } 111root .UpdateStats (make (fs.HardLinkedItems )) 112 113doomed := make (fs.Files ,0 ,fileCount ) 114for item := range child .GetFilesLocked (fs .SortByName ,fs .SortAsc ) { 115doomed = append (doomed ,item ) 116 } 117 118stop := make (chan struct {}) 119var workers sync.WaitGroup 120 121workers .Add (1 ) 122go func () { 123defer workers .Done () 124for { 125select { 126case <- stop : 127return 128default : 129 } 130readLockedTree (root ) 131runtime .Gosched () 132 } 133 }() 134 135for _ ,item := range doomed { 136child .RemoveFile (item ) 137runtime .Gosched () 138 } 139close (stop ) 140workers .Wait () 141 142if got ,want := child .GetItemCount (),int64 (1 );got != want { 143t .Fatalf ("child item count = %d, want %d" ,got ,want ) 144 } 145if got ,want := root .GetItemCount (),int64 (2 );got != want { 146t .Fatalf ("root item count = %d, want %d" ,got ,want ) 147 } 148if got ,want := root .GetSize (),int64 (0 );got != want { 149t .Fatalf ("root size = %d, want %d" ,got ,want ) 150 } 151if got ,want := root .GetUsage (),int64 (0 );got != want { 152t .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 () 165for 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 () 172if child ,ok := item .(* Dir );ok { 173for 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}