Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Make less flakey
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Jun 7, 2016
1 parent 644a008 commit ade718b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 92 deletions.
54 changes: 1 addition & 53 deletions process/process_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,13 @@
package process

import (
"os/exec"
"runtime"
"testing"
"time"

"github.com/keybase/go-updater/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindProcessWait(t *testing.T) {
procPath := procPath(t, "testFindProcessWait")
cmd := exec.Command(procPath, "sleep")
defer cleanupProc(cmd, procPath)

// Ensure it's not already running
procs, err := FindProcesses(NewMatcher(procPath, PathEqual, testLog), time.Millisecond, 0, testLog)
require.NoError(t, err)
require.Equal(t, 0, len(procs))

go func() {
time.Sleep(10 * time.Millisecond)
startErr := cmd.Start()
require.NoError(t, startErr)
}()

// Wait up to second for process to be running
procs, err = FindProcesses(NewMatcher(procPath, PathEqual, testLog), time.Second, 10*time.Millisecond, testLog)
require.NoError(t, err)
require.True(t, len(procs) == 1)
}

func TestTerminateAll(t *testing.T) {
procPath := procPath(t, "testTerminateAll")
defer util.RemoveFileAtPath(procPath)
start := func() int {
cmd := exec.Command(procPath, "sleep")
err := cmd.Start()
require.NoError(t, err)
require.NotNil(t, cmd.Process)
return cmd.Process.Pid
}

pids := []int{}
pids = append(pids, start())
pids = append(pids, start())
matcher := NewMatcher(procPath, PathEqual, testLog)
TerminateAll(matcher, time.Millisecond, testLog)
assertTerminated(t, pids[0], "signal: terminated")
assertTerminated(t, pids[1], "signal: terminated")
}

func TestFindProcessTest(t *testing.T) {
if runtime.GOOS == "linux" {
t.Skip("Unsupported until we have process path on linux")
}
pid, path := process(t)
procs, err := FindProcesses(NewMatcher(path, PathEqual, testLog), 0, 0, testLog)
require.NoError(t, err)
require.Equal(t, 1, len(procs))
assert.Equal(t, pid, procs[0].Pid())
testTerminateAll(t, procPath, "signal: terminated")
}
50 changes: 44 additions & 6 deletions process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,50 @@ func TestTerminateAllFn(t *testing.T) {
terminateAll(fn, matchAll, time.Millisecond, testLog)
}

// process returns this test process' path that is running
func process(t *testing.T) (int, string) {
pid := os.Getpid()
proc, err := findProcessWithPID(pid)
func startProcess(t *testing.T, path string, testCommand string) (string, int, *exec.Cmd) {
cmd := exec.Command(path, testCommand)
err := cmd.Start()
require.NoError(t, err)
path, err := proc.Path()
require.NotNil(t, cmd.Process)
return path, cmd.Process.Pid, cmd
}

func testTerminateAll(t *testing.T, path string, status string) {
path, pid1, cmd1 := startProcess(t, path, "sleep")
defer cleanupProc(cmd1, "")
_, pid2, cmd2 := startProcess(t, path, "sleep")
defer cleanupProc(cmd2, "")

time.Sleep(10 * time.Millisecond)

terminatePids := TerminateAll(NewMatcher(path, PathEqual, testLog), time.Millisecond, testLog)
assert.Contains(t, terminatePids, pid1)
assert.Contains(t, terminatePids, pid2)
assertTerminated(t, pid1, status)
assertTerminated(t, pid2, status)
}

func TestFindProcessWait(t *testing.T) {
if runtime.GOOS == "linux" {
t.Skip("Unsupported until we have process path on linux")
}
procPath := procPath(t, "testFindProcessWait")
cmd := exec.Command(procPath, "sleep")
defer cleanupProc(cmd, procPath)

// Ensure it's not already running
procs, err := FindProcesses(NewMatcher(procPath, PathEqual, testLog), time.Millisecond, 0, testLog)
require.NoError(t, err)
require.Equal(t, 0, len(procs))

go func() {
time.Sleep(10 * time.Millisecond)
startErr := cmd.Start()
require.NoError(t, startErr)
}()

// Wait up to second for process to be running
procs, err = FindProcesses(NewMatcher(procPath, PathEqual, testLog), time.Second, 10*time.Millisecond, testLog)
require.NoError(t, err)
return pid, path
require.True(t, len(procs) == 1)
}
37 changes: 4 additions & 33 deletions process/process_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,13 @@
package process

import (
"os"
"os/exec"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/keybase/go-updater/util"
)

func startProcess(t *testing.T, testCommand string) (string, int, *exec.Cmd) {
path := filepath.Join(os.Getenv("GOPATH"), "bin", "test.exe")
cmd := exec.Command(path, testCommand)
err := cmd.Start()
require.NoError(t, err)
require.NotNil(t, cmd.Process)
return path, cmd.Process.Pid, cmd
}

func TestTerminateAll(t *testing.T) {
pids := []int{}
path, pid1, cmd1 := startProcess(t, "sleep")
defer cleanupProc(cmd1, "")
_, pid2, cmd2 := startProcess(t, "sleep")
defer cleanupProc(cmd2, "")
pids = append(pids, pid1, pid2)
TerminateAll(NewMatcher(path, PathEqual, testLog), time.Millisecond, testLog)
assertTerminated(t, pids[0], "exit status 1")
assertTerminated(t, pids[1], "exit status 1")
}

func TestFindProcessTest(t *testing.T) {
path, _, cmd := startProcess(t, "sleep")
defer cleanupProc(cmd, "")
procs, err := FindProcesses(NewMatcher(path, PathEqual, testLog), time.Second, 20*time.Millisecond, testLog)
require.NoError(t, err)
// TODO: Fix flakiness where we might have more than 1 process here
require.True(t, len(procs) >= 1)
//assert.Equal(t, pid, procs[0].Pid())
procPath := procPath(t, "testTerminateAll")
defer util.RemoveFileAtPath(procPath)
testTerminateAll(t, procPath, "exit status 1")
}

0 comments on commit ade718b

Please sign in to comment.