本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5KB

  1. package process
  2. import (
  3. "os/exec"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestManager_Add(t *testing.T) {
  9. pm := Manager{Processes: make(map[int64]*Process)}
  10. pid := pm.Add("foo", exec.Command("foo"))
  11. assert.Equal(t, int64(1), pid, "expected to get pid 1 got %d", pid)
  12. pid = pm.Add("bar", exec.Command("bar"))
  13. assert.Equal(t, int64(2), pid, "expected to get pid 2 got %d", pid)
  14. }
  15. func TestManager_Remove(t *testing.T) {
  16. pm := Manager{Processes: make(map[int64]*Process)}
  17. pid1 := pm.Add("foo", exec.Command("foo"))
  18. assert.Equal(t, int64(1), pid1, "expected to get pid 1 got %d", pid1)
  19. pid2 := pm.Add("bar", exec.Command("bar"))
  20. assert.Equal(t, int64(2), pid2, "expected to get pid 2 got %d", pid2)
  21. pm.Remove(pid2)
  22. _, exists := pm.Processes[pid2]
  23. assert.False(t, exists, "PID %d is in the list but shouldn't", pid2)
  24. }
  25. func TestExecTimeoutNever(t *testing.T) {
  26. // TODO Investigate how to improve the time elapsed per round.
  27. maxLoops := 10
  28. for i := 1; i < maxLoops; i++ {
  29. _, stderr, err := GetManager().ExecTimeout(5*time.Second, "ExecTimeout", "git", "--version")
  30. if err != nil {
  31. t.Fatalf("git --version: %v(%s)", err, stderr)
  32. }
  33. }
  34. }
  35. func TestExecTimeoutAlways(t *testing.T) {
  36. maxLoops := 100
  37. for i := 1; i < maxLoops; i++ {
  38. _, stderr, err := GetManager().ExecTimeout(100*time.Microsecond, "ExecTimeout", "sleep", "5")
  39. // TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded".
  40. if err == nil {
  41. t.Fatalf("sleep 5 secs: %v(%s)", err, stderr)
  42. }
  43. }
  44. }
上海开阖软件有限公司 沪ICP备12045867号-1