fork/gdu
git clone https://git.julia.blue/fork/gdu
35cba00
master
1package analyze 2 3import ( 4"runtime" 5"sync" 6"testing" 7"time" 8 9"github.com/dundee/gdu/v5/internal/testdir" 10"github.com/dundee/gdu/v5/pkg/fs" 11"github.com/stretchr/testify/assert" 12) 13 14// TestStoredDirUpdateStatsLockedAccessors exercises the same reader/writer mix 15// on a StoredDir that the web UI and TUI produce: stats updates overlapping 16// readers that go through the synchronized accessors. Run with -race to catch 17// any field written outside Dir.m. 18func TestStoredDirUpdateStatsLockedAccessors (t * testing.T ) { 19fin := testdir .CreateTestDir () 20defer fin () 21 22analyzer := CreateStoredAnalyzer (t .TempDir ()) 23dir := analyzer .AnalyzeDir ( 24"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 25 ).(* StoredDir ) 26analyzer .GetDone ().Wait () 27 28// Hold the database open for the whole test so every worker shares one 29// badger instance instead of opening and closing it per call. 30closeFn := DefaultStorage .Open () 31defer closeFn () 32 33// updateStats is idempotent over a static tree, so the concurrent run must 34// converge on the values a single-threaded run produces. 35dir .UpdateStats (make (fs.HardLinkedItems )) 36wantCount ,wantSize ,wantUsage := dir .GetItemCount (),dir .GetSize (),dir .GetUsage () 37 38const workers = 4 39const rounds = 25 40 41var wg sync.WaitGroup 42for range workers { 43wg .Add (1 ) 44go func () { 45defer wg .Done () 46for range rounds { 47dir .UpdateStats (make (fs.HardLinkedItems )) 48runtime .Gosched () 49 } 50 }() 51 52wg .Add (1 ) 53go func () { 54defer wg .Done () 55for range rounds { 56readLockedStoredDir (dir ) 57runtime .Gosched () 58 } 59 }() 60 } 61wg .Wait () 62 63assert .Equal (t ,wantCount ,dir .GetItemCount ()) 64assert .Equal (t ,wantSize ,dir .GetSize ()) 65assert .Equal (t ,wantUsage ,dir .GetUsage ()) 66} 67 68// TestStoredDirOpensStorageOnDemandConcurrently drives the same workload with 69// no pinned database handle, so every operation has to open storage itself. 70// Reference counting must let those overlapping opens share one badger 71// instance instead of tripping its exclusive directory lock. 72func TestStoredDirOpensStorageOnDemandConcurrently (t * testing.T ) { 73fin := testdir .CreateTestDir () 74defer fin () 75 76analyzer := CreateStoredAnalyzer (t .TempDir ()) 77dir := analyzer .AnalyzeDir ( 78"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 79 ).(* StoredDir ) 80analyzer .GetDone ().Wait () 81 82assert .False (t ,DefaultStorage .IsOpen (),"the analyzer should have closed storage on the way out" ) 83 84var wg sync.WaitGroup 85for range 4 { 86wg .Add (1 ) 87go func () { 88defer wg .Done () 89for range 5 { 90dir .UpdateStats (make (fs.HardLinkedItems )) 91readLockedStoredDir (dir ) 92runtime .Gosched () 93 } 94 }() 95 } 96wg .Wait () 97 98assert .False (t ,DefaultStorage .IsOpen (),"every reference must be released again" ) 99assert .Positive (t ,dir .GetSize ()) 100} 101 102// TestStoredDirGetFilesUnderCallerHeldReadLock pins the caller-locked half of 103// the fs.Item contract: tui.(*UI).showDir holds RLock across the whole 104// GetFiles iteration, so loading children lazily must not re-enter Dir.m. 105// sync.RWMutex is not reentrant, so a write lock there self-deadlocks, and 106// even a read lock deadlocks once a writer has queued behind the caller. 107func TestStoredDirGetFilesUnderCallerHeldReadLock (t * testing.T ) { 108fin := testdir .CreateTestDir () 109defer fin () 110 111analyzer := CreateStoredAnalyzer (t .TempDir ()) 112dir := analyzer .AnalyzeDir ( 113"test_dir" ,func (_ ,_ string )bool {return false },func (_ string )bool {return false }, 114 ).(* StoredDir ) 115analyzer .GetDone ().Wait () 116 117dir .UpdateStats (make (fs.HardLinkedItems )) 118 119done := make (chan int ,1 ) 120go func () { 121unlock := dir .RLock () 122defer unlock () 123 124// Queue a writer on the very mutex the caller holds for reading, so 125// the read-lock variant of the bug is covered too. 126queued := make (chan struct {}) 127go func () { 128close (queued ) 129dir .SetFlag ('!' ) 130 }() 131<- queued 132time .Sleep (50 * time .Millisecond ) 133 134var count int 135for range dir .GetFiles (fs .SortByName ,fs .SortAsc ) { 136count ++ 137 } 138done <- count 139 }() 140 141select { 142case count := <- done : 143assert .Positive (t ,count ) 144case <- time .After (30 * time .Second ): 145t .Fatal ("GetFiles deadlocked while the caller held the directory read lock" ) 146 } 147} 148 149// readLockedStoredDir mirrors a UI traversal: every scalar accessor plus the 150// file listing, which loads children back out of storage. 151func readLockedStoredDir (dir * StoredDir ) { 152_ = dir .GetName () 153_ = dir .GetFlag () 154_ = dir .GetSize () 155_ = dir .GetUsage () 156_ = dir .GetMtime () 157_ = dir .GetItemCount () 158for item := range dir .GetFiles (fs .SortBySize ,fs .SortDesc ) { 159_ = item .GetName () 160_ = item .GetFlag () 161_ = item .GetSize () 162_ = item .GetUsage () 163_ = item .GetMtime () 164_ = item .GetItemCount () 165 } 166} 167 168// TestStorageOpenIsReferenceCounted covers the storage contract StoredDir 169// relies on: badger locks its directory, so overlapping Open calls must share 170// one instance and only the last closer may actually close it. 171func TestStorageOpenIsReferenceCounted (t * testing.T ) { 172storage := NewStorage (t .TempDir (),"/some/path" ) 173 174outer := storage .Open () 175assert .True (t ,storage .IsOpen ()) 176 177inner := storage .Open () 178inner () 179assert .True (t ,storage .IsOpen (),"inner closer must not close the shared database" ) 180 181outer () 182assert .False (t ,storage .IsOpen ()) 183 184// closers are idempotent 185outer () 186inner () 187assert .False (t ,storage .IsOpen ()) 188} 189 190// TestStorageOpenConcurrent asserts concurrent Open/close pairs never trip 191// badger's exclusive directory lock, which would panic inside Open. 192func TestStorageOpenConcurrent (t * testing.T ) { 193storage := NewStorage (t .TempDir (),"/some/path" ) 194 195pin := storage .Open () 196 197var wg sync.WaitGroup 198for range 8 { 199wg .Add (1 ) 200go func () { 201defer wg .Done () 202for range 20 { 203closeFn := storage .Open () 204assert .True (t ,storage .IsOpen ()) 205closeFn () 206runtime .Gosched () 207 } 208 }() 209 } 210wg .Wait () 211 212assert .True (t ,storage .IsOpen ()) 213pin () 214assert .False (t ,storage .IsOpen ()) 215}