Files
summercms/internal/dev/watch_test.go
Jakub Zych 270b7f1e87 feat(01-03): add summer dev watch rebuild loop
- Watch app sources with fsnotify, debounce, and one serialized build.App
- Restart the child only after a successful build and print rebuild latency
- Ignore generated app files and reap the child on cancellation
2026-09-16 13:59:43 +02:00

186 lines
4.8 KiB
Go

package dev
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync/atomic"
"syscall"
"testing"
"time"
)
func TestWatchRebuildsOnSourceEdit(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
var out bytes.Buffer
var builds atomic.Int32
var starts atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
builds.Add(1)
fmt.Fprintln(w, "built hello in 1ms")
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if builds.Load() != 1 {
t.Fatalf("initial builds = %d", builds.Load())
}
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte("package p\n// edited\n"), 0o644); err != nil {
t.Fatal(err)
}
waitUntil(t, "restart after edit", func() bool { return starts.Load() == 2 })
if builds.Load() != 2 {
t.Fatalf("builds after edit = %d", builds.Load())
}
if err := os.WriteFile(filepath.Join(dir, "plugins.gen.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n// generated\n"), 0o644); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
if starts.Load() != 2 || builds.Load() != 2 {
t.Fatalf("generated files triggered rebuild starts=%d builds=%d", starts.Load(), builds.Load())
}
got := out.String()
if strings.Count(got, "rebuild:") != 2 {
t.Fatalf("want 2 rebuild latency lines, got %q", got)
}
cancel()
select {
case err := <-done:
if err != nil && err != context.Canceled {
t.Fatalf("watch exit: %v", err)
}
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit on cancel")
}
}
func TestWatchKeepsChildOnBuildFailure(t *testing.T) {
dir := t.TempDir()
writeWatchApp(t, dir)
var out bytes.Buffer
var builds atomic.Int32
var starts atomic.Int32
var pid atomic.Int32
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
opts := Options{
Debounce: 40 * time.Millisecond,
Build: func(ctx context.Context, appDir string, w io.Writer) error {
n := builds.Add(1)
if n >= 2 {
return fmt.Errorf("compile failed")
}
return nil
},
Start: func(ctx context.Context, binPath string, stdout, stderr io.Writer) (*exec.Cmd, error) {
starts.Add(1)
cmd := exec.Command("sleep", "30")
if err := cmd.Start(); err != nil {
return nil, err
}
pid.Store(int32(cmd.Process.Pid))
return cmd, nil
},
}
done := make(chan error, 1)
go func() { done <- watch(ctx, dir, &out, opts) }()
waitUntil(t, "initial start", func() bool { return starts.Load() == 1 })
if err := os.WriteFile(filepath.Join(dir, "plugin.go"), []byte("package p\n// bad\n"), 0o644); err != nil {
t.Fatal(err)
}
waitUntil(t, "failed rebuild", func() bool { return builds.Load() >= 2 })
time.Sleep(80 * time.Millisecond)
if starts.Load() != 1 {
t.Fatalf("build failure restarted child: starts=%d", starts.Load())
}
if !strings.Contains(out.String(), "compile failed") {
t.Fatalf("missing build error: %s", out.String())
}
proc, err := os.FindProcess(int(pid.Load()))
if err != nil {
t.Fatal(err)
}
if err := proc.Signal(syscall.Signal(0)); err != nil {
t.Fatalf("child was reaped after failed build: %v", err)
}
cancel()
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("watch did not exit")
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if err := proc.Signal(syscall.Signal(0)); err != nil {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatal("child still running after cancel")
}
func writeWatchApp(t *testing.T, dir string) {
t.Helper()
write(t, filepath.Join(dir, "summer.yaml"), "module: example.com/app\nbinary: hello\nplugins:\n - id: a.b\n module: example.com/app/plugins/demo\n")
write(t, filepath.Join(dir, "plugin.go"), "package p\n")
write(t, filepath.Join(dir, "main.go"), "package main\n")
write(t, filepath.Join(dir, "plugins.gen.go"), "package main\n")
}
func write(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
func waitUntil(t *testing.T, name string, ok func() bool) {
t.Helper()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
if ok() {
return
}
time.Sleep(15 * time.Millisecond)
}
t.Fatalf("timed out waiting for %s", name)
}