MCPcopy
hub / github.com/docker/compose / TestDontWatchEachFile

Function TestDontWatchEachFile

pkg/watch/watcher_naive_test.go:32–120  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30)
31
32func TestDontWatchEachFile(t *testing.T) {
33 if runtime.GOOS != "linux" {
34 t.Skip("This test uses linux-specific inotify checks")
35 }
36
37 // fsnotify is not recursive, so we need to watch each directory
38 // you can watch individual files with fsnotify, but that is more prone to exhaust resources
39 // this test uses a Linux way to get the number of watches to make sure we're watching
40 // per-directory, not per-file
41 f := newNotifyFixture(t)
42
43 watched := f.TempDir("watched")
44
45 // there are a few different cases we want to test for because the code paths are slightly
46 // different:
47 // 1) initial: data there before we ever call watch
48 // 2) inplace: data we create while the watch is happening
49 // 3) staged: data we create in another directory and then atomically move into place
50
51 // initial
52 f.WriteFile(f.JoinPath(watched, "initial.txt"), "initial data")
53
54 initialDir := f.JoinPath(watched, "initial_dir")
55 if err := os.Mkdir(initialDir, 0o777); err != nil {
56 t.Fatal(err)
57 }
58
59 for i := range 100 {
60 f.WriteFile(f.JoinPath(initialDir, fmt.Sprintf("%d", i)), "initial data")
61 }
62
63 f.watch(watched)
64 f.fsync()
65 if len(f.events) != 0 {
66 t.Fatalf("expected 0 initial events; got %d events: %v", len(f.events), f.events)
67 }
68 f.events = nil
69
70 // inplace
71 inplace := f.JoinPath(watched, "inplace")
72 if err := os.Mkdir(inplace, 0o777); err != nil {
73 t.Fatal(err)
74 }
75 f.WriteFile(f.JoinPath(inplace, "inplace.txt"), "inplace data")
76
77 inplaceDir := f.JoinPath(inplace, "inplace_dir")
78 if err := os.Mkdir(inplaceDir, 0o777); err != nil {
79 t.Fatal(err)
80 }
81
82 for i := range 100 {
83 f.WriteFile(f.JoinPath(inplaceDir, fmt.Sprintf("%d", i)), "inplace data")
84 }
85
86 f.fsync()
87 if len(f.events) < 100 {
88 t.Fatalf("expected >100 inplace events; got %d events: %v", len(f.events), f.events)
89 }

Callers

nothing calls this directly

Calls 7

newNotifyFixtureFunction · 0.85
inotifyNodesFunction · 0.85
TempDirMethod · 0.80
WriteFileMethod · 0.80
JoinPathMethod · 0.80
fsyncMethod · 0.80
watchMethod · 0.45

Tested by

no test coverage detected