MCPcopy
hub / github.com/cortesi/devd / TestRouteWatch

Function TestRouteWatch

watch_test.go:38–94  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

36}
37
38func TestRouteWatch(t *testing.T) {
39 logger := termlog.NewLog()
40 logger.Quiet()
41
42 tmpFolder, err := ioutil.TempDir("", "")
43 if err != nil {
44 t.Error(err)
45 }
46 defer os.RemoveAll(tmpFolder)
47
48 // Ensure that using . for the path works:
49 os.Chdir(tmpFolder)
50 routes := make(RouteCollection)
51 routes.Add(".", nil)
52
53 changedFiles := make(map[string]int)
54 ch := make(chan []string, 1)
55 var wg sync.WaitGroup
56 go func() {
57 for {
58 data, more := <-ch
59 if more {
60 t.Log("received notification for changed file(s):", strings.Join(data, ", "))
61 for i := range data {
62 changedFiles[data[i]] = 1
63 wg.Done()
64 }
65 } else {
66 t.Log("No more changes are expected")
67 return
68 }
69 }
70 }()
71 watchers := make([]*moddwatch.Watcher, len(routes))
72 i := 0
73 for r := range routes {
74 watcher, err := routes[r].Watch(ch, nil, logger)
75 watchers[i] = watcher
76 if err != nil {
77 t.Error(err)
78 }
79 i++
80 }
81 addTempFile(&wg, t, tmpFolder, "a.txt", "foo\n")
82 addTempFile(&wg, t, tmpFolder, "c.txt", "bar\n")
83 addTempFile(&wg, t, tmpFolder, "another.file.txt", "bar\n")
84 waitTimeout(&wg, 5*time.Second)
85
86 for _, v := range watchers {
87 v.Stop()
88 }
89
90 close(ch)
91 if len(changedFiles) != 3 {
92 t.Error("The watch should have been notified about 3 changed files")
93 }
94}

Callers

nothing calls this directly

Calls 4

addTempFileFunction · 0.85
waitTimeoutFunction · 0.85
AddMethod · 0.80
WatchMethod · 0.65

Tested by

no test coverage detected