fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
ignore
1package analyze 2 3import ( 4"os" 5"path/filepath" 6"strconv" 7"testing" 8"time" 9 10"github.com/dundee/gdu/v5/internal/common" 11"github.com/dundee/gdu/v5/pkg/fs" 12"github.com/stretchr/testify/require" 13) 14 15type cancelRaceAnalyzer interface { 16 common.Analyzer 17IsCancelled ()bool 18} 19 20type cancelRaceAnalyzerFactory struct { 21name string 22new func (* testing.T )cancelRaceAnalyzer 23} 24 25func TestCancelPreemptiveAnalyzers (t * testing.T ) { 26root := createCancelRaceTree (t ) 27 28for _ ,factory := range cancelRaceAnalyzerFactories () { 29t .Run (factory .name ,func (t * testing.T ) { 30analyzer := factory .new (t ) 31analyzer .Cancel () 32 33dir := analyzer .AnalyzeDir (root ,func (string ,string )bool {return false },nil ) 34 35require .NotNil (t ,dir ) 36require .True (t ,analyzer .IsCancelled ()) 37var children int 38for range dir .GetFiles (fs .SortByName ,fs .SortAsc ) { 39children ++ 40 } 41require .Zero (t ,children ,"preemptive cancellation must not schedule child scans" ) 42 }) 43 } 44} 45 46func TestCancelWhileAnalyzingAnalyzers (t * testing.T ) { 47root := createCancelRaceTree (t ) 48blockedPath := filepath .Join (root ,"1" ) 49 50for _ ,factory := range cancelRaceAnalyzerFactories () { 51t .Run (factory .name ,func (t * testing.T ) { 52analyzer := factory .new (t ) 53started := make (chan struct {}) 54release := make (chan struct {}) 55 56ignore := func (_ string ,path string )bool { 57if path == blockedPath { 58close (started ) 59<- release 60 } 61return false 62 } 63 64result := make (chan fs.Item ,1 ) 65go func () { 66result <- analyzer .AnalyzeDir (root ,ignore ,nil ) 67 }() 68 69select { 70case <- started : 71case <- time .After (10 * time .Second ): 72t .Fatal ("analyzer did not reach the blocked root entry" ) 73 } 74analyzer .Cancel () 75close (release ) 76 77select { 78case dir := <- result : 79require .NotNil (t ,dir ) 80require .True (t ,analyzer .IsCancelled ()) 81require .NotContains (t ,itemNames (dir ),"1" ) 82case <- time .After (10 * time .Second ): 83t .Fatal ("analyzer did not finish after cancellation" ) 84 } 85 }) 86 } 87} 88 89func cancelRaceAnalyzerFactories () []cancelRaceAnalyzerFactory { 90return []cancelRaceAnalyzerFactory { 91 {name :"parallel" ,new :func (* testing.T )cancelRaceAnalyzer {return CreateAnalyzer () }}, 92 {name :"stable" ,new :func (* testing.T )cancelRaceAnalyzer {return CreateStableOrderAnalyzer () }}, 93 {name :"sequential" ,new :func (* testing.T )cancelRaceAnalyzer {return CreateSeqAnalyzer () }}, 94 {name :"stored" ,new :func (t * testing.T )cancelRaceAnalyzer { 95return CreateStoredAnalyzer (filepath .Join (t .TempDir (),"results.badger" )) 96 }}, 97 {name :"sqlite" ,new :func (t * testing.T )cancelRaceAnalyzer { 98analyzer ,err := CreateSqliteAnalyzer (filepath .Join (t .TempDir (),"results.sqlite" )) 99require .NoError (t ,err ) 100t .Cleanup (func () {require .NoError (t ,analyzer .storage .Close ()) }) 101return analyzer 102 }}, 103 {name :"top-dir" ,new :func (* testing.T )cancelRaceAnalyzer {return CreateTopDirAnalyzer () }}, 104 } 105} 106 107func createCancelRaceTree (t * testing.T )string { 108t .Helper () 109root := t .TempDir () 110for i := range 8 { 111top := filepath .Join (root ,strconv .Itoa (i )) 112require .NoError (t ,os .Mkdir (top ,0o700 )) 113for j := range 8 { 114nested := filepath .Join (top ,strconv .Itoa (j )) 115require .NoError (t ,os .Mkdir (nested ,0o700 )) 116require .NoError (t ,os .WriteFile (filepath .Join (nested ,"file" ), []byte ("data" ),0o600 )) 117 } 118 } 119return root 120}