fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
subvolume
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"testing" 7"time" 8 9"github.com/dundee/gdu/v5/internal/common" 10"github.com/dundee/gdu/v5/pkg/fs" 11"github.com/stretchr/testify/assert" 12) 13 14func TestAnalyzersCancelKeepsPartialResults (t * testing.T ) { 15root := createCancellationTree (t ) 16 17tests := []struct { 18name string 19analyzer common.Analyzer 20 }{ 21 {name :"parallel" ,analyzer :CreateAnalyzer ()}, 22 {name :"stable parallel" ,analyzer :CreateStableOrderAnalyzer ()}, 23 {name :"sequential" ,analyzer :CreateSeqAnalyzer ()}, 24 } 25 26for _ ,test := range tests { 27t .Run (test .name ,func (t * testing.T ) { 28dir := scanUntilCancelled (t ,test .analyzer ,root ) 29 30assert .True (t ,test .analyzer .(interface {IsCancelled ()bool }).IsCancelled ()) 31assert .Contains (t ,itemNames (dir ),"a-file" ) 32assert .NotContains (t ,itemNames (dir ),"b-dir" ) 33 }) 34 } 35} 36 37func TestParallelAnalyzersCancelQueuedDirectories (t * testing.T ) { 38root := createCancellationTree (t ) 39tests := []struct { 40name string 41factory func () common.Analyzer 42 }{ 43 {name :"parallel" ,factory :func () common.Analyzer {return CreateAnalyzer () }}, 44 {name :"stable parallel" ,factory :func () common.Analyzer {return CreateStableOrderAnalyzer () }}, 45 } 46 47for _ ,test := range tests { 48t .Run (test .name ,func (t * testing.T ) { 49initialSlots := len (concurrencyLimit ) 50for len (concurrencyLimit )< cap (concurrencyLimit ) { 51concurrencyLimit <- struct {}{} 52 } 53defer func () { 54for len (concurrencyLimit )> initialSlots { 55<- concurrencyLimit 56 } 57 }() 58 59analyzer := test .factory () 60result := make (chan fs.Item ,1 ) 61go func () { 62result <- analyzer .AnalyzeDir (root ,func (_ ,_ string )bool {return false },nil ) 63 }() 64 65assert .Eventually (t ,func ()bool { 66return analyzer .GetProgress ().CurrentItemName == root 67 },time .Second ,time .Millisecond ) 68analyzer .Cancel () 69<- concurrencyLimit 70 71select { 72case dir := <- result : 73assert .NotContains (t ,itemNames (dir ),"b-dir" ) 74case <- time .After (time .Second ): 75t .Fatal ("analyzer did not discard queued directory after cancellation" ) 76 } 77 }) 78 } 79} 80 81func TestStorageBackedAnalyzersCancelKeepsPartialResults (t * testing.T ) { 82root := createCancellationTree (t ) 83 84stored := CreateStoredAnalyzer (filepath .Join (t .TempDir (),"results.badger" )) 85dir := scanUntilCancelled (t ,stored ,root ) 86assert .True (t ,stored .IsCancelled ()) 87assert .Contains (t ,itemNames (dir ),"a-file" ) 88assert .NotContains (t ,itemNames (dir ),"b-dir" ) 89 90sqliteAnalyzer ,err := CreateSqliteAnalyzer (filepath .Join (t .TempDir (),"results.sqlite" )) 91assert .NoError (t ,err ) 92defer sqliteAnalyzer .storage .Close () 93dir = scanUntilCancelled (t ,sqliteAnalyzer ,root ) 94assert .True (t ,sqliteAnalyzer .IsCancelled ()) 95assert .Contains (t ,itemNames (dir ),"a-file" ) 96assert .NotContains (t ,itemNames (dir ),"b-dir" ) 97} 98 99func TestAnalyzeDirHonorsPreemptiveCancellation (t * testing.T ) { 100analyzer := CreateSeqAnalyzer () 101analyzer .Cancel () 102 103dir := analyzer .AnalyzeDir (createCancellationTree (t ),func (_ ,_ string )bool {return false },nil ) 104 105assert .True (t ,analyzer .IsCancelled ()) 106assert .NotContains (t ,itemNames (dir ),"a-file" ) 107assert .NotContains (t ,itemNames (dir ),"b-dir" ) 108} 109 110func TestResetProgressClearsPreviousCancellation (t * testing.T ) { 111analyzer := CreateSeqAnalyzer () 112analyzer .Cancel () 113analyzer .ResetProgress () 114 115dir := analyzer .AnalyzeDir (createCancellationTree (t ),func (_ ,_ string )bool {return false },nil ) 116 117assert .False (t ,analyzer .IsCancelled ()) 118assert .Contains (t ,itemNames (dir ),"a-file" ) 119assert .Contains (t ,itemNames (dir ),"b-dir" ) 120} 121 122func TestTopDirAnalyzerCancellationStopsActiveSubdirectory (t * testing.T ) { 123root := t .TempDir () 124assert .NoError (t ,os .WriteFile (filepath .Join (root ,"unscanned" ), []byte ("data" ),0o600 )) 125 126analyzer := CreateTopDirAnalyzer () 127t .Cleanup (analyzer .progressTicker .Stop ) 128analyzer .Cancel () 129result := & TopDir {} 130 131analyzer .processSubDir (root ,result ) 132 133size ,usage ,itemCount := result .GetUsage () 134assert .Zero (t ,size ) 135assert .Zero (t ,usage ) 136assert .Zero (t ,itemCount ) 137} 138 139func createCancellationTree (t * testing.T )string { 140t .Helper () 141root := t .TempDir () 142assert .NoError (t ,os .WriteFile (filepath .Join (root ,"a-file" ), []byte ("complete" ),0o600 )) 143assert .NoError (t ,os .Mkdir (filepath .Join (root ,"b-dir" ),0o700 )) 144assert .NoError (t ,os .WriteFile (filepath .Join (root ,"b-dir" ,"nested" ), []byte ("pending" ),0o600 )) 145return root 146} 147 148func scanUntilCancelled (t * testing.T ,analyzer common.Analyzer ,root string ) fs.Item { 149t .Helper () 150return analyzer .AnalyzeDir (root ,func (name ,_ string )bool { 151if name == "b-dir" { 152analyzer .Cancel () 153 } 154return false 155 },nil ) 156} 157 158func itemNames (dir fs.Item ) []string { 159var names []string 160for item := range dir .GetFiles (fs .SortByName ,fs .SortAsc ) { 161names = append (names ,item .GetName ()) 162 } 163return names 164}