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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package process
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGetManager(t *testing.T) {
  11. go func() {
  12. // test race protection
  13. _ = GetManager()
  14. }()
  15. pm := GetManager()
  16. assert.NotNil(t, pm)
  17. }
  18. func TestManager_AddContext(t *testing.T) {
  19. pm := Manager{processMap: make(map[IDType]*process), next: 1}
  20. ctx, cancel := context.WithCancel(context.Background())
  21. defer cancel()
  22. p1Ctx, _, finished := pm.AddContext(ctx, "foo")
  23. defer finished()
  24. assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to get non-empty pid")
  25. p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar")
  26. defer finished()
  27. assert.NotEmpty(t, GetContext(p2Ctx).GetPID(), "expected to get non-empty pid")
  28. assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID())
  29. assert.Equal(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID(), "expected to get pid %s got %s", GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetParent().GetPID())
  30. }
  31. func TestManager_Cancel(t *testing.T) {
  32. pm := Manager{processMap: make(map[IDType]*process), next: 1}
  33. ctx, _, finished := pm.AddContext(context.Background(), "foo")
  34. defer finished()
  35. pm.Cancel(GetPID(ctx))
  36. select {
  37. case <-ctx.Done():
  38. default:
  39. assert.Fail(t, "Cancel should cancel the provided context")
  40. }
  41. finished()
  42. ctx, cancel, finished := pm.AddContext(context.Background(), "foo")
  43. defer finished()
  44. cancel()
  45. select {
  46. case <-ctx.Done():
  47. default:
  48. assert.Fail(t, "Cancel should cancel the provided context")
  49. }
  50. finished()
  51. }
  52. func TestManager_Remove(t *testing.T) {
  53. pm := Manager{processMap: make(map[IDType]*process), next: 1}
  54. ctx, cancel := context.WithCancel(context.Background())
  55. defer cancel()
  56. p1Ctx, _, finished := pm.AddContext(ctx, "foo")
  57. defer finished()
  58. assert.NotEmpty(t, GetContext(p1Ctx).GetPID(), "expected to have non-empty PID")
  59. p2Ctx, _, finished := pm.AddContext(p1Ctx, "bar")
  60. defer finished()
  61. assert.NotEqual(t, GetContext(p1Ctx).GetPID(), GetContext(p2Ctx).GetPID(), "expected to get different pids got %s == %s", GetContext(p2Ctx).GetPID(), GetContext(p1Ctx).GetPID())
  62. finished()
  63. _, exists := pm.processMap[GetPID(p2Ctx)]
  64. assert.False(t, exists, "PID %d is in the list but shouldn't", GetPID(p2Ctx))
  65. }
  66. func TestExecTimeoutNever(t *testing.T) {
  67. // TODO Investigate how to improve the time elapsed per round.
  68. maxLoops := 10
  69. for i := 1; i < maxLoops; i++ {
  70. _, stderr, err := GetManager().ExecTimeout(5*time.Second, "ExecTimeout", "git", "--version")
  71. if err != nil {
  72. t.Fatalf("git --version: %v(%s)", err, stderr)
  73. }
  74. }
  75. }
  76. func TestExecTimeoutAlways(t *testing.T) {
  77. maxLoops := 100
  78. for i := 1; i < maxLoops; i++ {
  79. _, stderr, err := GetManager().ExecTimeout(100*time.Microsecond, "ExecTimeout", "sleep", "5")
  80. // TODO Simplify logging and errors to get precise error type. E.g. checking "if err != context.DeadlineExceeded".
  81. if err == nil {
  82. t.Fatalf("sleep 5 secs: %v(%s)", err, stderr)
  83. }
  84. }
  85. }