Skip to content

Commit

Permalink
fix, speed up and clean up child tests
Browse files Browse the repository at this point in the history
Small issue with subshells sending text to STDERR about the signal
received, breaking the text check (result of sending signals to process
group). Changed them to only check STDOUT, where the test text is sent.

Previously used a sleep time of 500 milliseconds when 50 worked fine.
This sped up the child tests a lot.

Everything else are cleaning up lots of useless checks, hardcoded times,
and replace subshell test calls use of bash with standard /bin/sh.
  • Loading branch information
eikenb committed Jul 28, 2021
1 parent 5717544 commit 7d17648
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions child/child_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/go-gatedio"
)

const fileWaitSleepDelay = 500 * time.Millisecond
const fileWaitSleepDelay = 50 * time.Millisecond

func testChild(t *testing.T) *Child {
c, err := New(&NewInput{
Expand Down Expand Up @@ -207,11 +207,11 @@ func TestSignal(t *testing.T) {
t.Parallel()

c := testChild(t)
c.command = "bash"
c.args = []string{"-c", "trap 'echo one; exit' SIGUSR1; while true; do sleep 0.2; done"}
c.command = "sh"
c.args = []string{"-c", "trap 'echo one; exit' USR1; while true; do sleep 0.2; done"}

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out
c.stdout = out

if err := c.Start(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -248,12 +248,12 @@ func TestReload_signal(t *testing.T) {
t.Parallel()

c := testChild(t)
c.command = "bash"
c.args = []string{"-c", "trap 'echo one; exit' SIGUSR1; while true; do sleep 0.2; done"}
c.command = "sh"
c.args = []string{"-c", "trap 'echo one; exit' USR1; while true; do sleep 0.2; done"}
c.reloadSignal = syscall.SIGUSR1

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out
c.stdout = out

if err := c.Start(); err != nil {
t.Fatal(err)
Expand All @@ -280,14 +280,11 @@ func TestReload_noSignal(t *testing.T) {
t.Parallel()

c := testChild(t)
c.command = "bash"
c.command = "sh"
c.args = []string{"-c", "while true; do sleep 0.2; done"}
c.killTimeout = 10 * time.Millisecond
c.reloadSignal = nil

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out

if err := c.Start(); err != nil {
t.Fatal(err)
}
Expand All @@ -309,9 +306,6 @@ func TestReload_noSignal(t *testing.T) {
// Get the new pid
npid := c.cmd.Process.Pid

// Stop the child now
c.Stop()

if opid == npid {
t.Error("expected new process to restart")
}
Expand All @@ -331,12 +325,12 @@ func TestKill_signal(t *testing.T) {
t.Parallel()

c := testChild(t)
c.command = "bash"
c.args = []string{"-c", "trap 'echo one; exit' SIGUSR1; while true; do sleep 0.2; done"}
c.command = "sh"
c.args = []string{"-c", "trap 'echo one; exit' USR1; while true; do sleep 0.2; done"}
c.killSignal = syscall.SIGUSR1

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out
c.stdout = out

if err := c.Start(); err != nil {
t.Fatal(err)
Expand All @@ -361,14 +355,11 @@ func TestKill_noSignal(t *testing.T) {
t.Parallel()

c := testChild(t)
c.command = "bash"
c.command = "sh"
c.args = []string{"-c", "while true; do sleep 0.2; done"}
c.killTimeout = 20 * time.Millisecond
c.killSignal = nil

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out

if err := c.Start(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -398,14 +389,14 @@ func TestKill_noProcess(t *testing.T) {
func TestStop_noWaitForSplay(t *testing.T) {
t.Parallel()
c := testChild(t)
c.command = "bash"
c.args = []string{"-c", "trap 'echo one; exit' SIGUSR1; while true; do sleep 0.2; done"}
c.command = "sh"
c.args = []string{"-c", "trap 'echo one; exit' USR1; while true; do sleep 0.2; done"}
c.splay = 100 * time.Second
c.reloadSignal = nil
c.killSignal = syscall.SIGUSR1

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out
c.stdout = out

if err := c.Start(); err != nil {
t.Fatal(err)
Expand All @@ -423,23 +414,20 @@ func TestStop_noWaitForSplay(t *testing.T) {
t.Errorf("expected %q to be %q", out.String(), expected)
}

if killEndTime.Sub(killStartTime) > 500*time.Millisecond {
if killEndTime.Sub(killStartTime) > fileWaitSleepDelay {
t.Error("expected not to wait for splay")
}
}

func TestStop_childAlreadyDead(t *testing.T) {
t.Parallel()
c := testChild(t)
c.command = "bash"
c.command = "sh"
c.args = []string{"-c", "exit 1"}
c.splay = 100 * time.Second
c.reloadSignal = nil
c.killSignal = syscall.SIGTERM

out := gatedio.NewByteBuffer()
c.stdout, c.stderr = out, out

if err := c.Start(); err != nil {
t.Fatal(err)
}
Expand All @@ -451,7 +439,7 @@ func TestStop_childAlreadyDead(t *testing.T) {
c.Stop()
killEndTime := time.Now()

if killEndTime.Sub(killStartTime) > 500*time.Millisecond {
if killEndTime.Sub(killStartTime) > fileWaitSleepDelay {
t.Error("expected not to wait for splay")
}
}
Expand Down

0 comments on commit 7d17648

Please sign in to comment.