From fe091250bdb2cb6f1033b781a40483dccbb2e71e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 10 Sep 2025 10:17:11 -0400 Subject: [PATCH] Moved to the new go owner on my gittea so that the url to use the code is easier. --- Makefile | 2 +- go.mod | 2 +- main.go | 10 ++++++- tools/cmd/tester/main.go | 57 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 63387fa..cdfa080 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ build: build_tester go build . test: - go test lcthw.dev/learn-code-the-hard-way/ozai/tests -c -o runtests$(GO_IS_STUPID_EXE) + go test lcthw.dev/go/ozai/tests -c -o runtests$(GO_IS_STUPID_EXE) ./runtests$(GO_IS_STUPID_EXE) build_tester: diff --git a/go.mod b/go.mod index c518542..e756b37 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module lcthw.dev/learn-code-the-hard-way/ozai +module lcthw.dev/go/ozai go 1.24.2 diff --git a/main.go b/main.go index 0a55c58..d78a3e4 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,11 @@ import ( "os/exec" "log" "io" + "time" "os" "errors" "sync" - "lcthw.dev/learn-code-the-hard-way/ozai/config" + "lcthw.dev/go/ozai/config" ) func LaunchLogger(in io.Reader, out io.Writer, err error) { @@ -51,6 +52,13 @@ func main() { cmd.Run() fmt.Println("WAITING for", name) + go func() { + fmt.Println("killer runs") + time.Sleep(4 * time.Second) + fmt.Println("!!!!!!!!!!!!!!!!!!! TIME TO DIE!!!!!") + cmd.Process.Signal(os.Interrupt) + }() + cmd.Wait() fmt.Println("PROC", name, "EXITED SEND QUIT") } diff --git a/tools/cmd/tester/main.go b/tools/cmd/tester/main.go index fda83e2..7c04697 100644 --- a/tools/cmd/tester/main.go +++ b/tools/cmd/tester/main.go @@ -4,8 +4,60 @@ import ( "fmt" "time" "flag" + "sync" ) +func Worker(name string, done chan<- int, stop <-chan int) { + for i := 0; i < 10; i++ { + select { + case <-stop: + fmt.Println("Worker stopped:", name) + return + default: + time.Sleep(1 * time.Second) + fmt.Println("TESTER #", name) + } + } + + done<- 1 +} + +func Supervisor(id int, count int) { + stop := make(chan int) + var wg sync.WaitGroup + + for i := 0; i < count; i++ { + wg.Add(1) + go func() { + defer wg.Done() + + for { + workerDone := make(chan int) + go Worker(fmt.Sprintf("%d:%d", id, i), workerDone, stop) + + + select { + case <-workerDone: + fmt.Println("Supervisor: Worker Done") + time.Sleep(500 * time.Millisecond) + case <-stop: + fmt.Println("Supervisor: Stop") + return + } + } + }() + } + + go func() { + // test shutdown + time.Sleep(10000 * time.Millisecond) + close(stop) + }() + + wg.Wait() +} + + func main() { count := flag.Int("count", 10, "Seconds to wait") id := flag.Int("id", 1, "Tester ID to use.") @@ -13,8 +65,5 @@ func main() { fmt.Println(">>> TESTER STARTS, id=", *id, "count=", *count) - for i := 0; i < *count; i++ { - time.Sleep(1 * time.Second) - fmt.Println("TESTER #", *id) - } + Supervisor(*id, *count) }