A kind of Augmented Reality programming game that makes you a better programmer.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ttarpit/tools/cmd/tester/main.go

78 lines
1.4 KiB

package main
import (
"fmt"
"time"
"os"
"flag"
"sync"
"math/rand"
)
func Worker(name string, count int, done chan<- int, stop <-chan int) {
for i := 0; i < count; i++ {
select {
case <-stop:
fmt.Println("Worker stopped:", name)
done<- 1
return
default:
time.Sleep(1 * time.Second)
if rand.Int() % 2 == 0 {
fmt.Println("TESTER #", name)
} else {
os.Stderr.Write([]byte(fmt.Sprintf("TESTER ERR # %s\n", name)))
}
}
}
done<- 1
}
func Supervisor(id int, count int) {
stop := make(chan int)
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
workerDone := make(chan int)
go Worker(fmt.Sprintf("%d:%d", id, i), count, 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.")
flag.Parse()
fmt.Println(">>> TESTER STARTS, id=", *id, "count=", *count)
Supervisor(*id, *count)
}