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.

manager_test.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package process
  2. import (
  3. "context"
  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", nil)
  11. assert.Equal(t, int64(1), pid, "expected to get pid 1 got %d", pid)
  12. pid = pm.Add("bar", nil)
  13. assert.Equal(t, int64(2), pid, "expected to get pid 2 got %d", pid)
  14. }
  15. func TestManager_Cancel(t *testing.T) {
  16. pm := Manager{processes: make(map[int64]*Process)}
  17. ctx, cancel := context.WithCancel(context.Background())
  18. pid := pm.Add("foo", cancel)
  19. pm.Cancel(pid)
  20. select {
  21. case <-ctx.Done():
  22. default:
  23. assert.Fail(t, "Cancel should cancel the provided context")
  24. }
  25. }
  26. func TestManager_Remove(t *testing.T) {
  27. pm := Manager{processes: make(map[int64]*Process)}
  28. pid1 := pm.Add("foo", nil)
  29. assert.Equal(t, int64(1), pid1, "expected to get pid 1 got %d", pid1)
  30. pid2 := pm.Add("bar", nil)
  31. assert.Equal(t, int64(2), pid2, "expected to get pid 2 got %d", pid2)
  32. pm.Remove(pid2)
  33. _, exists := pm.processes[pid2]
  34. assert.False(t, exists, "PID %d is in the list but shouldn't", pid2)
  35. }
  36. func TestExecTimeoutNever(t *testing.T) {
  37. // TODO Investigate how to improve the time elapsed per round.
  38. maxLoops := 10
  39. for i := 1; i < maxLoops; i++ {
  40. _, stderr, err := GetManager().ExecTimeout(5*time.Second, "ExecTimeout", "git", "--version")
  41. if err != nil {
  42. t.Fatalf("git --version: %v(%s)", err, stderr)
  43. }
  44. }
  45. }
  46. func TestExecTimeoutAlways(t *testing.T) {
  47. maxLoops := 100
  48. for i := 1; i < maxLoops; i++ {
  49. _, stderr, err := GetManager().ExecTimeout(100*time.Microsecond, "ExecTimeout", "sleep", "5")
  50. // TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded".
  51. if err == nil {
  52. t.Fatalf("sleep 5 secs: %v(%s)", err, stderr)
  53. }
  54. }
  55. }