MCPcopy
hub / github.com/syncthing/syncthing / TestStopWalk

Function TestStopWalk

lib/scanner/walk_test.go:660–727  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

658}
659
660func TestStopWalk(t *testing.T) {
661 // Create tree that is 100 levels deep, with each level containing 100
662 // files (each 1 MB) and 100 directories (in turn containing 100 files
663 // and 100 directories, etc). That is, in total > 100^100 files and as
664 // many directories. It'll take a while to scan, giving us time to
665 // cancel it and make sure the scan stops.
666
667 // Use an errorFs as the backing fs for the rest of the interface
668 // The way we get it is a bit hacky tho.
669 errorFs := fs.NewFilesystem(fs.FilesystemType("error"), ".")
670 fs := fs.NewWalkFilesystem(&infiniteFS{errorFs, 100, 100, 1e6})
671
672 const numHashers = 4
673 ctx, cancel := context.WithCancel(context.Background())
674 cfg, cfgCancel := testConfig()
675 defer cfgCancel()
676 cfg.Filesystem = fs
677 cfg.Hashers = numHashers
678 cfg.ProgressTickIntervalS = -1 // Don't attempt to build the full list of files before starting to scan...
679 fchan := Walk(ctx, cfg)
680
681 // Receive a few entries to make sure the walker is up and running,
682 // scanning both files and dirs. Do some quick sanity tests on the
683 // returned file entries to make sure we are not just reading crap from
684 // a closed channel or something.
685 dirs := 0
686 files := 0
687 for {
688 res := <-fchan
689 if res.Err != nil {
690 t.Errorf("Error while scanning %v: %v", res.Err, res.Path)
691 }
692 f := res.File
693 t.Log("Scanned", f)
694 if f.IsDirectory() {
695 if f.Name == "" || f.Permissions == 0 {
696 t.Error("Bad directory entry", f)
697 }
698 dirs++
699 } else {
700 if f.Name == "" || len(f.Blocks) == 0 || f.Permissions == 0 {
701 t.Error("Bad file entry", f)
702 }
703 files++
704 }
705 if dirs > 5 && files > 5 {
706 break
707 }
708 }
709
710 // Cancel the walker.
711 cancel()
712
713 // Empty out any waiting entries and wait for the channel to close.
714 // Count them, they should be zero or very few - essentially, each
715 // hasher has the choice of returning a fully handled entry or
716 // cancelling, but they should not start on another item. The scan
717 // goroutine can also have one directory entry in-flight on finishedChan

Callers

nothing calls this directly

Calls 5

testConfigFunction · 0.85
WalkFunction · 0.85
LogMethod · 0.65
ErrorMethod · 0.65
IsDirectoryMethod · 0.45

Tested by

no test coverage detected