Skip to content

Instantly share code, notes, and snippets.

@augustoroman
Created December 2, 2013 08:49
Show Gist options
  • Select an option

  • Save augustoroman/7746767 to your computer and use it in GitHub Desktop.

Select an option

Save augustoroman/7746767 to your computer and use it in GitHub Desktop.
gobs/cmd does not play well with signal handling. TestSignal works, but TestSignalInCmd fails almost every time.
package cmd_signal_test
import (
"github.com/gobs/cmd"
"os"
"os/signal"
"testing"
"time"
)
type hardWorker struct {
c chan os.Signal
}
func newHardWorker() *hardWorker {
w := &hardWorker{c: make(chan os.Signal, 1)}
signal.Notify(w.c, os.Interrupt)
return w
}
func (w *hardWorker) cleanup() { signal.Stop(w.c) }
func (w *hardWorker) workReallyHard() { <-w.c }
func interrupt() {
p, err := os.FindProcess(os.Getpid())
if err != nil {
panic(err)
}
p.Signal(os.Interrupt)
}
func TestSignal(t *testing.T) {
w := newHardWorker()
defer w.cleanup()
done := make(chan bool)
go func() {
w.workReallyHard()
done <- true
}()
interrupt()
<-done
}
func TestSignalInCmd(t *testing.T) {
commander := &cmd.Cmd{EnableShell: true}
commander.Init()
w := newHardWorker()
defer w.cleanup()
commander.Add(cmd.Command{
Name: "work",
Help: "Work",
Call: func(string) bool { w.workReallyHard(); return true },
})
done := make(chan bool)
go func() {
commander.CmdLoop()
done <- true
}()
go func() { commander.OneCmd("work") }()
time.Sleep(100 * time.Millisecond)
interrupt()
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment