Skip to content

Commit

Permalink
modified: a_test.go
Browse files Browse the repository at this point in the history
	modified:   daemon.go
	modified:   daemonrun_test.go
	modified:   signals_test.go
	modified:   supervisor.go
  • Loading branch information
nbari committed Aug 7, 2016
1 parent e5554f6 commit df259c3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
5 changes: 5 additions & 0 deletions a_test.go
Expand Up @@ -49,6 +49,11 @@ func (self *catchSignals) Kill() (err error) {
if err != nil {
return
}
// to avoid zombie
_, err = self.Process.Wait()
if err != nil {
return
}
return
}

Expand Down
7 changes: 4 additions & 3 deletions daemon.go
Expand Up @@ -69,11 +69,13 @@ func (self *Daemon) Run() {
uid, err := strconv.Atoi(self.user.Uid)
if err != nil {
self.Control.state <- err
return
}

gid, err := strconv.Atoi(self.user.Gid)
if err != nil {
self.Control.state <- err
return
}

// https://golang.org/pkg/syscall/#SysProcAttr
Expand Down Expand Up @@ -156,11 +158,10 @@ func New(cfg *Config) (*Daemon, error) {
supDir = filepath.Join(d, "supervise")
}

// buffer because goroutine might write before main goroutine starts
control := &Control{
fifo: make(chan Return, 1),
fifo: make(chan Return),
quit: make(chan struct{}),
state: make(chan error, 1),
state: make(chan error),
}

// if ctrl create supervise dir
Expand Down
4 changes: 2 additions & 2 deletions daemonrun_test.go
Expand Up @@ -48,9 +48,9 @@ func TestDaemonRun(t *testing.T) {
d := &Daemon{
Config: cfg,
Control: &Control{
fifo: make(chan Return, 1),
fifo: make(chan Return),
quit: make(chan struct{}),
state: make(chan error, 1),
state: make(chan error),
},
Forker: &myFork{},
Logger: &LogWriter{
Expand Down
71 changes: 59 additions & 12 deletions signals_test.go
@@ -1,8 +1,8 @@
package immortal

import (
"io/ioutil"
"log"
// "io/ioutil"
// "log"
"os"
"os/signal"
"path/filepath"
Expand All @@ -17,12 +17,16 @@ func TestHelperProcessSignals(*testing.T) {
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
os.Exit(0)
select {
case <-c:
os.Exit(0)
case <-time.After(30 * time.Second):
os.Exit(1)
}
}

func TestSignals(t *testing.T) {
log.SetOutput(ioutil.Discard)
// log.SetOutput(ioutil.Discard)
base := filepath.Base(os.Args[0]) // "exec.test"
dir := filepath.Dir(os.Args[0]) // "/tmp/go-buildNNNN/os/exec/_test"
if dir == "." {
Expand All @@ -42,14 +46,14 @@ func TestSignals(t *testing.T) {
Child: filepath.Join(parentDir, "child.pid"),
},
}
c := make(chan os.Signal, 1)
c := make(chan os.Signal)
wait := make(chan struct{})
d := &Daemon{
Config: cfg,
Control: &Control{
fifo: make(chan Return, 1),
fifo: make(chan Return),
quit: make(chan struct{}),
state: make(chan error, 1),
state: make(chan error),
},
Forker: &myFork{},
Logger: &LogWriter{
Expand Down Expand Up @@ -172,12 +176,55 @@ func TestSignals(t *testing.T) {
for old_pid == d.process.GetPid() {
time.Sleep(time.Second)
}
// create zombie
println(d.process.GetPid())
d.Control.fifo <- Return{err: nil, msg: "d"}
for {

// test down
d.Control.fifo <- Return{err: nil, msg: "down"}
for sup.IsRunning(d.process.GetPid()) {
// waiting for process to exit
}

// test up
// bring up the service (new pid expected)
d.Control.fifo <- Return{err: nil, msg: "up"}
for !sup.IsRunning(d.process.GetPid()) {
}
d.Control.fifo <- Return{err: nil, msg: "once"}
for d.count_defer != 1 {
}
expect(t, d.count, uint32(1), "in 194")
expect(t, d.count_defer, uint32(1), "in 195")

// save old pid
old_pid = d.process.GetPid()
// send kill (should not start)
d.Control.fifo <- Return{err: nil, msg: "k"}
for sup.IsRunning(d.process.GetPid()) {
}
expect(t, old_pid, d.process.GetPid(), "in 203")
expect(t, false, sup.IsRunning(d.process.GetPid()), "in 204")

// test up
// bring up the service (new pid expected)
d.Control.fifo <- Return{err: nil, msg: "up"}
for !sup.IsRunning(d.process.GetPid()) {
}
old_pid = d.process.GetPid()

// send kill (should re-start, and get new pid)
d.Control.fifo <- Return{err: nil, msg: "k"}
for sup.IsRunning(d.process.GetPid()) {
}
select {
case <-wait:
case <-time.After(1 * time.Second):
t.Fatal("timeout waiting for pid")
}
for old_pid == d.process.GetPid() {
}
expect(t, true, sup.IsRunning(d.process.GetPid()))

// quit
d.Control.fifo <- Return{err: nil, msg: "exit"}
}

func waitSig(t *testing.T, c <-chan os.Signal, sig os.Signal) {
Expand Down
7 changes: 5 additions & 2 deletions supervisor.go
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -84,10 +85,12 @@ func Supervise(s Supervisor, d *Daemon) {
return
case state := <-d.Control.state:
if state != nil {
if state.Error() == "EXIT" {
if exitError, ok := state.(*exec.ExitError); ok {
log.Print(exitError)
} else if state.Error() == "EXIT" {
log.Printf("PID: %d Exited", d.process.GetPid())
} else {
log.Print(state.Error())
log.Print(state)
}
}

Expand Down

0 comments on commit df259c3

Please sign in to comment.