2
0

worker_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package worker
  2. import (
  3. "math/rand"
  4. "runtime"
  5. "sync"
  6. "testing"
  7. "time"
  8. )
  9. type void struct{}
  10. var none = void{}
  11. func waitChannelFor(wg *sync.WaitGroup) <-chan void {
  12. ch := make(chan void)
  13. go func() {
  14. defer close(ch)
  15. wg.Wait()
  16. ch <- none
  17. }()
  18. return ch
  19. }
  20. func TestWorkerPoolShutdown(t *testing.T) {
  21. const workers = 3
  22. // running goroutines
  23. routines := runtime.NumGoroutine()
  24. t.Logf("Go Routines Before: %d\n", routines)
  25. wp := NewWorkerPool(workers, func(any) any { return nil })
  26. t.Logf("Go Routines After: %d\n", runtime.NumGoroutine())
  27. wp.Shutdown()
  28. time.Sleep(time.Second)
  29. if runtime.NumGoroutine() != routines {
  30. t.Errorf("Go routines after shutdown: %d != Go routines at start of test: %d\n", runtime.NumGoroutine(), routines)
  31. }
  32. }
  33. func TestWorkerPoolExactWorkers(t *testing.T) {
  34. const workers = 3
  35. // worker func logs start/finish for simulated work
  36. work := func(i int) void {
  37. t.Logf("Starting Work: %d\n", i)
  38. time.Sleep(2 * time.Second)
  39. t.Logf("Finished Work: %d\n", i)
  40. return none
  41. }
  42. var wg sync.WaitGroup
  43. wg.Add(workers)
  44. pool := NewWorkerPool(workers, work)
  45. for i := 0; i < workers; i++ {
  46. onComplete := make(chan void)
  47. go func() {
  48. defer close(onComplete)
  49. <-onComplete
  50. wg.Done()
  51. }()
  52. // run work on worker pool
  53. pool.Run(i+1, onComplete)
  54. }
  55. select {
  56. case <-waitChannelFor(&wg):
  57. case <-time.After(5 * time.Second):
  58. t.Errorf("Failed to Complete Run for %d jobs in 5s\n", workers)
  59. }
  60. }
  61. func TestOrderedWorkGroup(t *testing.T) {
  62. const workers = 5
  63. const tasks = 10
  64. // worker func logs start/finish for simulated work, returns input value
  65. // for testing resulting group output
  66. work := func(i int) int {
  67. t.Logf("Starting Work: %d\n", i)
  68. time.Sleep(2 * time.Second)
  69. t.Logf("Finished Work: %d\n", i)
  70. return i
  71. }
  72. pool := NewWorkerPool(workers, work)
  73. ordered := NewOrderedGroup(pool, tasks)
  74. input := make([]int, tasks)
  75. // we create more tasks than workers to test queueing
  76. for i := 0; i < tasks; i++ {
  77. input[i] = i + 1
  78. err := ordered.Push(input[i])
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. }
  83. // get results and verify they match the recorded inputs
  84. results := ordered.Wait()
  85. for i := 0; i < tasks; i++ {
  86. if results[i] != input[i] {
  87. t.Errorf("Expected Results[%d](%d) to equal Input[%d](%d)\n", i, results[i], i, input[i])
  88. }
  89. }
  90. // The typical test run will show different tasks starting and stopping out of order (expected),
  91. // the result collection handles the ordering in the group, which is what we want to ensure in the
  92. // above assertion
  93. }
  94. func TestConcurrentDoOrdered(t *testing.T) {
  95. // Perform a similar test to the above ordered test, but use the helper func with pre-built inputs
  96. const tasks = 50
  97. // worker func logs start/finish for simulated work, returns input value
  98. // for testing resulting group output
  99. work := func(i int) int {
  100. t.Logf("Starting Work: %d\n", i)
  101. time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
  102. t.Logf("Finished Work: %d\n", i)
  103. return i
  104. }
  105. // pre-build inputs
  106. input := make([]int, tasks)
  107. for i := 0; i < tasks; i++ {
  108. input[i] = i + 1
  109. }
  110. // get results and verify they match the recorded inputs
  111. results := ConcurrentDo(work, input)
  112. for i := 0; i < tasks; i++ {
  113. if results[i] != input[i] {
  114. t.Errorf("Expected Results[%d](%d) to equal Input[%d](%d)\n", i, results[i], i, input[i])
  115. }
  116. }
  117. // The typical test run will show different tasks starting and stopping out of order (expected),
  118. // the result collection handles the ordering in the group, which is what we want to ensure in the
  119. // above assertion
  120. }