improved tests, go module

master
blob42 1 year ago
parent 31be968b7b
commit 6c924ec8bc

@ -0,0 +1,3 @@
module git.blob42.xyz/blob42/gum
go 1.21.1

@ -9,7 +9,7 @@ import (
"strings"
)
var idGen = IdGenerator()
var idGenerator = genId()
type WorkUnit interface {
Run(UnitManager)
@ -122,15 +122,19 @@ func (m *Manager) Run() {
}
}
func (m *Manager) ShutdownOn(sig os.Signal) {
signal.Notify(m.signalIn, sig)
func (m *Manager) ShutdownOn(sig ...os.Signal) {
m.shutdownSigs = append(m.shutdownSigs, sig)
for _, s := range sig {
log.Printf("Registering shutdown signal: %s\n", s)
signal.Notify(m.signalIn, s)
}
m.shutdownSigs = append(m.shutdownSigs, sig...)
}
type IDGenerator func(string) int
func IdGenerator() IDGenerator {
func genId() IDGenerator {
ids := make(map[string]int)
return func(unit string) int {
@ -152,7 +156,7 @@ func (m *Manager) AddUnit(unit WorkUnit) {
unitType := reflect.TypeOf(unit)
unitName := strings.Split(unitType.String(), ".")[1]
unitId := idGen(unitName)
unitId := idGenerator(unitName)
unitName += strconv.Itoa(unitId)
log.Println("Adding unit ", unitName)

@ -19,6 +19,7 @@ func (w *Worker) Run(um UnitManager) {
// Worker's loop
for {
select {
case <-ticker.C:
log.Println("tick")
@ -36,14 +37,15 @@ func (w *Worker) Run(um UnitManager) {
func (w *Worker) Shutdown() {
// Do shutdown procedure for worker
return
}
func NewWorker() *Worker {
return &Worker{}
}
func DoRunMain(pid chan int, quit chan<- bool) {
func DoRun(pid chan int,
quit chan<- bool,
signals ...os.Signal) {
pid <- os.Getpid()
@ -51,7 +53,7 @@ func DoRunMain(pid chan int, quit chan<- bool) {
manager := NewManager()
// Shutdown all units on SIGINT
manager.ShutdownOn(os.Interrupt)
manager.ShutdownOn(signals...)
// NewWorker returns a type implementing WorkUnit interface unit :=
worker1 := NewWorker()
@ -69,14 +71,25 @@ func DoRunMain(pid chan int, quit chan<- bool) {
}
func TestRunMain(t *testing.T) {
signals := map[string]os.Signal{
"interrupt": os.Interrupt,
}
mainPid := make(chan int, 1)
quit := make(chan bool)
go DoRunMain(mainPid, quit)
time.Sleep(3 * time.Second)
syscall.Kill(<-mainPid, syscall.SIGINT)
<-quit
for name, sig := range signals {
t.Logf("Testing signal: %s\n", name)
go DoRun(mainPid, quit, sig)
time.Sleep(3 * time.Second)
syssig, ok := sig.(syscall.Signal)
if !ok {
t.Fatalf("Could not convert os.Signal to syscall.Signal")
}
syscall.Kill(<-mainPid, syssig)
<-quit
}
}

Loading…
Cancel
Save