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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package process
  5. import (
  6. "context"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestManager_Add(t *testing.T) {
  12. pm := Manager{processes: make(map[int64]*Process)}
  13. pid := pm.Add("foo", nil)
  14. assert.Equal(t, int64(1), pid, "expected to get pid 1 got %d", pid)
  15. pid = pm.Add("bar", nil)
  16. assert.Equal(t, int64(2), pid, "expected to get pid 2 got %d", pid)
  17. }
  18. func TestManager_Cancel(t *testing.T) {
  19. pm := Manager{processes: make(map[int64]*Process)}
  20. ctx, cancel := context.WithCancel(context.Background())
  21. pid := pm.Add("foo", cancel)
  22. pm.Cancel(pid)
  23. select {
  24. case <-ctx.Done():
  25. default:
  26. assert.Fail(t, "Cancel should cancel the provided context")
  27. }
  28. }
  29. func TestManager_Remove(t *testing.T) {
  30. pm := Manager{processes: make(map[int64]*Process)}
  31. pid1 := pm.Add("foo", nil)
  32. assert.Equal(t, int64(1), pid1, "expected to get pid 1 got %d", pid1)
  33. pid2 := pm.Add("bar", nil)
  34. assert.Equal(t, int64(2), pid2, "expected to get pid 2 got %d", pid2)
  35. pm.Remove(pid2)
  36. _, exists := pm.processes[pid2]
  37. assert.False(t, exists, "PID %d is in the list but shouldn't", pid2)
  38. }
  39. func TestExecTimeoutNever(t *testing.T) {
  40. // TODO Investigate how to improve the time elapsed per round.
  41. maxLoops := 10
  42. for i := 1; i < maxLoops; i++ {
  43. _, stderr, err := GetManager().ExecTimeout(5*time.Second, "ExecTimeout", "git", "--version")
  44. if err != nil {
  45. t.Fatalf("git --version: %v(%s)", err, stderr)
  46. }
  47. }
  48. }
  49. func TestExecTimeoutAlways(t *testing.T) {
  50. maxLoops := 100
  51. for i := 1; i < maxLoops; i++ {
  52. _, stderr, err := GetManager().ExecTimeout(100*time.Microsecond, "ExecTimeout", "sleep", "5")
  53. // TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded".
  54. if err == nil {
  55. t.Fatalf("sleep 5 secs: %v(%s)", err, stderr)
  56. }
  57. }
  58. }