Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
bc8ec89
test: add unit tests for WatchManifest in internal/pkg/platform
mwbrooks Nov 23, 2026
c7a650c
test: check WatchManifest blocks until the context ends
mwbrooks Nov 23, 2026
9dd1b5d
Merge branch 'main' into mwbrooks-test-watch-manifest
mwbrooks Nov 23, 2026
644185f
Merge branch 'main' into mwbrooks-test-watch-manifest
mwbrooks Nov 23, 2026
2234913
fix: stop the manifest file watcher when the context ends
mwbrooks Nov 23, 2026
d179e7f
Merge remote-tracking branch 'origin/mwbrooks-test-watch-manifest' in…
mwbrooks Nov 23, 2026
170f9e0
test: cover the drain in closeWatcherOnDone with an unread event
mwbrooks Nov 23, 2026
c82133c
Merge remote-tracking branch 'origin/main' into mwbrooks-fix-close-ma…
mwbrooks Nov 23, 2026
911d6dd
test: set the watch options as a pointer in Test_LocalServer_WatchMan…
mwbrooks Nov 23, 2026
51c0b4d
Merge remote-tracking branch 'origin/main' into mwbrooks-test-watch-m…
mwbrooks Nov 23, 2026
731c506
test: set watch options per case now that a missing watch config show…
mwbrooks Nov 23, 2026
1216e86
Merge remote-tracking branch 'origin/mwbrooks-test-watch-manifest' in…
mwbrooks Nov 23, 2026
0edd45f
Merge remote-tracking branch 'origin/main' into mwbrooks-fix-close-ma…
mwbrooks Nov 23, 2026
928e415
test: name the closeWatcherOnDone case for the unread event it drains
mwbrooks Nov 24, 2026
0d08bb6
Merge branch 'main' into mwbrooks-fix-close-manifest-watcher
mwbrooks Nov 24, 2026
4d6148f
Merge remote-tracking branch 'origin/main' into mwbrooks-fix-close-ma…
mwbrooks Nov 24, 2026
2cf3e8d
Merge branch 'main' into mwbrooks-fix-close-manifest-watcher
mwbrooks Nov 24, 2026
8c1d82d
Merge branch 'main' into mwbrooks-fix-close-manifest-watcher
mwbrooks Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/pkg/platform/localserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,25 @@ func (r *LocalServer) WatchManifest(ctx context.Context, auth types.SlackAuth, a
// Begin watching for manifest file changes
go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app)

go closeWatcherOnDone(ctx, w)

return w.Start(time.Millisecond * 100)
}

func closeWatcherOnDone(ctx context.Context, w *watcher.Watcher) {
<-ctx.Done()
w.Wait()
go w.Close()
for {
select {
case <-w.Event:
case <-w.Error:
case <-w.Closed:
return
}
}
Comment on lines +411 to +418

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧪 issue (blocking): Nothing tests the drain loop: swapping it for a plain w.Close() still passes Test_LocalServer_WatchManifest. Could we add a test with one unread event pending? This one passes here and hangs without the drain (needs path/filepath):

func Test_closeWatcherOnDone_drainsPendingEvents(t *testing.T) {
	dir := t.TempDir()
	manifest := filepath.Join(dir, "manifest.json")
	require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600))
	w := watcher.New()
	require.NoError(t, w.AddRecursive(dir))
	later := time.Now().Add(time.Hour)
	require.NoError(t, os.Chtimes(manifest, later, later))
	started := make(chan error, 1)
	go func() { started <- w.Start(time.Millisecond) }()
	ctx, cancel := context.WithCancel(t.Context())
	cancel()
	time.Sleep(50 * time.Millisecond)
	done := make(chan struct{})
	go func() { closeWatcherOnDone(ctx, w); close(done) }()
	select {
	case <-done:
	case <-time.After(time.Second):
		require.FailNow(t, "closeWatcherOnDone did not return with an unread event")
	}
	require.NoError(t, <-started)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in commit 170f9e0, thanks for the test! 🙇

}

// watchManifestEvents reinstalls the app on each manifest change until the context is canceled or the watcher closes
func (r *LocalServer) watchManifestEvents(ctx context.Context, w *watcher.Watcher, isRemoteManifest bool, auth types.SlackAuth, app types.App) {
for {
Expand Down
41 changes: 41 additions & 0 deletions internal/pkg/platform/localserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -515,6 +516,7 @@ func Test_LocalServer_WatchManifest(t *testing.T) {
manifestSource config.ManifestSource
manifestErr error
watch *hooks.WatchOpts
watchManifest bool
expectedErr error
expectedOutput []string
}{
Expand All @@ -538,6 +540,11 @@ func Test_LocalServer_WatchManifest(t *testing.T) {
watch: &hooks.WatchOpts{},
expectedOutput: []string{"Manifest source is remote", "Manifest watching is not enabled"},
},
"watches manifest paths until the context ends": {
manifestSource: config.ManifestSourceRemote,
watchManifest: true,
expectedOutput: []string{"Watching manifest changes to file paths matching"},
},
} {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context()))
Expand All @@ -551,6 +558,14 @@ func Test_LocalServer_WatchManifest(t *testing.T) {
clients := shared.NewClientFactory(clientsMock.MockClientFactory())
server := LocalServer{clients: clients}
server.cliConfig.Config.Watch = tc.watch
if tc.watchManifest {
server.cliConfig.Config.Watch = &hooks.WatchOpts{
Manifest: &hooks.ManifestWatchOpts{
Paths: []string{t.TempDir()},
FilterRegex: `manifest\.json$`,
},
}
}
errChan := make(chan error, 1)
go func() {
errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{})
Expand Down Expand Up @@ -583,6 +598,32 @@ func Test_LocalServer_WatchManifest(t *testing.T) {
}
}

func Test_closeWatcherOnDone_drainsAnUnreadEvent(t *testing.T) {
dir := t.TempDir()
manifest := filepath.Join(dir, "manifest.json")
require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600))
w := watcher.New()
require.NoError(t, w.AddRecursive(dir))
later := time.Now().Add(time.Hour)
require.NoError(t, os.Chtimes(manifest, later, later))
started := make(chan error, 1)
go func() { started <- w.Start(time.Millisecond) }()
ctx, cancel := context.WithCancel(t.Context())
cancel()
time.Sleep(50 * time.Millisecond)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 question (non-blocking): On a slow runner Start may not have reached its blocked send within 50ms. The test would then pass even with a plain Close. It held up 200/200 locally, so this is only something to keep in mind if you see a cheap way to wait on the pending event.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! The event channel is unbuffered, so the test can't see the pending send without reading it. I'll keep the sleep for now and revisit if it flakes in CI.

done := make(chan struct{})
go func() {
closeWatcherOnDone(ctx, w)
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
require.FailNow(t, "closeWatcherOnDone did not return with an unread event")
}
require.NoError(t, <-started)
}

func Test_LocalServer_watchManifestEvents(t *testing.T) {
for name, tc := range map[string]struct {
cancel bool
Expand Down
Loading