fork/gdu
git clone https://git.julia.blue/fork/gdu
99a7f2a
master
1package tui 2 3import ( 4"bytes" 5"os" 6"sync" 7"testing" 8"time" 9 10"github.com/dundee/gdu/v5/internal/common" 11"github.com/dundee/gdu/v5/internal/testanalyze" 12"github.com/dundee/gdu/v5/internal/testapp" 13"github.com/dundee/gdu/v5/pkg/fs" 14"github.com/gdamore/tcell/v2" 15"github.com/rivo/tview" 16"github.com/stretchr/testify/require" 17) 18 19type cancelRaceApp struct { 20* testapp.MockedApp 21updates chan func () 22mu sync.Mutex 23stops int 24} 25 26func newCancelRaceApp ()* cancelRaceApp { 27return & cancelRaceApp { 28MockedApp :& testapp.MockedApp {}, 29updates :make (chan func (),8 ), 30 } 31} 32 33func (app * cancelRaceApp )QueueUpdateDraw (update func ())* tview.Application { 34app .updates <- update 35return nil 36} 37 38func (app * cancelRaceApp )Stop () { 39app .mu .Lock () 40app .stops ++ 41app .mu .Unlock () 42} 43 44func (app * cancelRaceApp )stopCount ()int { 45app .mu .Lock () 46defer app .mu .Unlock () 47return app .stops 48} 49 50func (app * cancelRaceApp )nextUpdate (t * testing.T )func () { 51t .Helper () 52select { 53case update := <- app .updates : 54return update 55case <- time .After (2 * time .Second ): 56t .Fatal ("timed out waiting for queued UI update" ) 57return nil 58 } 59} 60 61type failOnceSignalScreen struct { 62 tcell.Screen 63attempted chan struct {} 64fail bool 65} 66 67func (screen * failOnceSignalScreen )PostEvent (event tcell.Event )error { 68if screen .fail { 69screen .fail = false 70close (screen .attempted ) 71return tcell .ErrEventQFull 72 } 73return screen .Screen .PostEvent (event ) 74} 75 76type signalLifecycleApp struct { 77* testapp.MockedApp 78signalAttempted <- chan struct {} 79} 80 81func (app * signalLifecycleApp )Run ()error { 82<- app .signalAttempted 83return nil 84} 85 86type cancelRaceAnalyzer struct { 87* testanalyze.MockedAnalyzer 88started chan struct {} 89release chan struct {} 90cancelled chan struct {} 91done common.SignalGroup 92cancelMu sync.Mutex 93cancels int 94once sync.Once 95} 96 97func newCancelRaceAnalyzer ()* cancelRaceAnalyzer { 98progressDone := make (common.SignalGroup ) 99progressDone .Broadcast () 100return & cancelRaceAnalyzer { 101MockedAnalyzer :& testanalyze.MockedAnalyzer {}, 102started :make (chan struct {}), 103release :make (chan struct {}), 104cancelled :make (chan struct {}), 105done :progressDone , 106 } 107} 108 109func (a * cancelRaceAnalyzer )AnalyzeDir ( 110path string ,ignore common.ShouldDirBeIgnored ,fileTypeFilter common.ShouldFileBeIgnored , 111) fs.Item { 112close (a .started ) 113select { 114case <- a .cancelled : 115case <- a .release : 116 } 117return a .MockedAnalyzer .AnalyzeDir (path ,ignore ,fileTypeFilter ) 118} 119 120func (a * cancelRaceAnalyzer )Cancel () { 121a .cancelMu .Lock () 122a .cancels ++ 123a .cancelMu .Unlock () 124a .once .Do (func () {close (a .cancelled ) }) 125} 126 127func (a * cancelRaceAnalyzer )cancelCount ()int { 128a .cancelMu .Lock () 129defer a .cancelMu .Unlock () 130return a .cancels 131} 132 133func (a * cancelRaceAnalyzer )GetDone () common.SignalGroup { 134return a .done 135} 136 137func newCancelRaceUI (app * cancelRaceApp ,analyzer * cancelRaceAnalyzer )* UI { 138screen := testapp .CreateSimScreen () 139ui := CreateUI (app ,screen ,& bytes.Buffer {},true ,true ,false ,false ) 140ui .Analyzer = analyzer 141ui .done = make (chan struct {}) 142return ui 143} 144 145func drainScanUpdates (t * testing.T ,app * cancelRaceApp ,ui * UI ) { 146t .Helper () 147// One update finalizes progress and one applies the completed scan. The 148// progress channel is already closed, so the first update is deterministic. 149first := app .nextUpdate (t ) 150second := app .nextUpdate (t ) 151first () 152second () 153require .False (t ,ui .scanning ) 154require .False (t ,ui .pages .HasPage ("progress" )) 155} 156 157func TestAnalyzePathImmediateCtrlCIsNotLost (t * testing.T ) { 158app := newCancelRaceApp () 159analyzer := newCancelRaceAnalyzer () 160ui := newCancelRaceUI (app ,analyzer ) 161 162require .NoError (t ,ui .AnalyzePath ("test_dir" ,nil )) 163// Ctrl-C is intentionally delivered immediately, before AnalyzeDir is 164// guaranteed to have started. Cancel must still be observed by the double. 165require .Nil (t ,ui .keyPressed (tcell .NewEventKey (tcell .KeyCtrlC ,0 ,0 ))) 166_ = ui .keyPressed (tcell .NewEventKey (tcell .KeyCtrlC ,0 ,0 )) 167 168select { 169case <- analyzer .started : 170case <- time .After (2 * time .Second ): 171t .Fatal ("AnalyzeDir did not start" ) 172 } 173select { 174case <- ui .done : 175case <- time .After (2 * time .Second ): 176t .Fatal ("scan did not complete after cancellation" ) 177 } 178drainScanUpdates (t ,app ,ui ) 179 180require .Equal (t ,1 ,analyzer .cancelCount ()) 181require .Equal (t ,0 ,app .stopCount ()) 182require .Equal (t ,"test_dir" ,ui .currentDir .GetName ()) 183} 184 185func TestCtrlCBeforeQueuedCompletionKeepsResults (t * testing.T ) { 186app := newCancelRaceApp () 187analyzer := newCancelRaceAnalyzer () 188ui := newCancelRaceUI (app ,analyzer ) 189 190require .NoError (t ,ui .AnalyzePath ("test_dir" ,nil )) 191close (analyzer .release ) 192select { 193case <- ui .done : 194case <- time .After (2 * time .Second ): 195t .Fatal ("scan did not complete" ) 196 } 197 198// The completion draw is queued, but has not run yet. Cancellation must 199// not discard the result or make the completion callback unsafe. 200require .Nil (t ,ui .keyPressed (tcell .NewEventKey (tcell .KeyCtrlC ,0 ,0 ))) 201drainScanUpdates (t ,app ,ui ) 202 203require .Equal (t ,1 ,analyzer .cancelCount ()) 204require .Equal (t ,"test_dir" ,ui .currentDir .GetName ()) 205require .Equal (t ,4 ,ui .table .GetRowCount ()) 206} 207 208func TestRepeatedSignalCtrlCIsIdempotent (t * testing.T ) { 209app := newCancelRaceApp () 210analyzer := newCancelRaceAnalyzer () 211ui := newCancelRaceUI (app ,analyzer ) 212 213require .NoError (t ,ui .AnalyzePath ("test_dir" ,nil )) 214ui .keyPressed (signalEvent (os .Interrupt )) 215ui .keyPressed (signalEvent (os .Interrupt )) 216require .Equal (t ,1 ,analyzer .cancelCount ()) 217require .Equal (t ,0 ,app .stopCount ()) 218 219close (analyzer .release ) 220select { 221case <- ui .done : 222case <- time .After (2 * time .Second ): 223t .Fatal ("scan did not complete" ) 224 } 225drainScanUpdates (t ,app ,ui ) 226 227// Once the completion callback has marked the scan finished, SIGINT takes 228// the normal quit path. 229ui .keyPressed (signalEvent (os .Interrupt )) 230require .Equal (t ,1 ,app .stopCount ()) 231} 232 233func TestSignalLoopQueuesScanCancellation (t * testing.T ) { 234app := newCancelRaceApp () 235analyzer := & testanalyze.MockedAnalyzer {} 236screen := testapp .CreateSimScreen () 237require .NoError (t ,screen .Init ()) 238defer screen .Fini () 239ui := CreateUI (app ,screen ,& bytes.Buffer {},true ,true ,false ,false ) 240ui .Analyzer = analyzer 241ui .progress = tview .NewTextView () 242ui .scanning = true 243 244signals := make (chan os.Signal ,1 ) 245signals <- os .Interrupt 246close (signals ) 247ui .handleSignals (signals ,make (chan struct {})) 248 249event ,ok := screen .PollEvent ().(* tcell.EventKey ) 250require .True (t ,ok ) 251ui .keyPressed (event ) 252 253require .True (t ,analyzer .IsCancelled ()) 254require .True (t ,ui .scanCancelled ) 255require .Equal (t ,0 ,app .stopCount ()) 256} 257 258func TestSignalLoopRetriesFullEventQueue (t * testing.T ) { 259app := newCancelRaceApp () 260analyzer := & testanalyze.MockedAnalyzer {} 261simScreen := testapp .CreateSimScreen () 262require .NoError (t ,simScreen .Init ()) 263defer simScreen .Fini () 264screen := & failOnceSignalScreen { 265Screen :simScreen , 266attempted :make (chan struct {}), 267fail :true , 268 } 269ui := CreateUI (app ,screen ,& bytes.Buffer {},true ,true ,false ,false ) 270ui .Analyzer = analyzer 271ui .progress = tview .NewTextView () 272ui .scanning = true 273 274signals := make (chan os.Signal ,1 ) 275signals <- os .Interrupt 276close (signals ) 277done := make (chan struct {}) 278go func () { 279defer close (done ) 280ui .handleSignals (signals ,make (chan struct {})) 281 }() 282 283select { 284case <- screen .attempted : 285case <- time .After (time .Second ): 286t .Fatal ("signal event delivery was not attempted" ) 287 } 288select { 289case <- done : 290case <- time .After (time .Second ): 291t .Fatal ("signal event was not retried" ) 292 } 293 294event ,ok := simScreen .PollEvent ().(* tcell.EventKey ) 295require .True (t ,ok ) 296ui .keyPressed (event ) 297 298require .True (t ,analyzer .IsCancelled ()) 299require .True (t ,ui .scanCancelled ) 300require .Equal (t ,0 ,app .stopCount ()) 301}