workers_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package appsrv
  15. import (
  16. "fmt"
  17. "sync"
  18. "testing"
  19. "time"
  20. )
  21. type workerTask struct {
  22. counter int
  23. }
  24. func (t *workerTask) Run() {
  25. t.counter += 1
  26. time.Sleep(1 * time.Second)
  27. }
  28. func (t *workerTask) Dump() string {
  29. return fmt.Sprintf("counter: %d", t.counter)
  30. }
  31. func TestWorkerManager(t *testing.T) {
  32. enableDebug()
  33. startTime := time.Now()
  34. // end := make(chan int)
  35. wm := NewWorkerManager("testwm", 2, 10, false)
  36. task := &workerTask{
  37. counter: 0,
  38. }
  39. for i := 0; i < 10; i += 1 {
  40. wm.Run(task, nil, nil)
  41. }
  42. for wm.ActiveWorkerCount() != 0 {
  43. time.Sleep(time.Second)
  44. }
  45. if time.Since(startTime) < 5*time.Second {
  46. t.Error("Incorrect timing")
  47. }
  48. }
  49. type errWorkerTask struct {
  50. wg *sync.WaitGroup
  51. }
  52. func (t *errWorkerTask) Run() {
  53. t.wg.Done()
  54. }
  55. func (t *errWorkerTask) Dump() string {
  56. return ""
  57. }
  58. type panicWorkerTask struct {
  59. wg *sync.WaitGroup
  60. }
  61. func (t *panicWorkerTask) Run() {
  62. defer t.wg.Done()
  63. panic("panic inside worker")
  64. }
  65. func (t *panicWorkerTask) Dump() string {
  66. return ""
  67. }
  68. func TestWorkerManagerError(t *testing.T) {
  69. wm := NewWorkerManager("testwm", 2, 10, false)
  70. errCbFactory := func(wg *sync.WaitGroup, errMark *bool) func(error) {
  71. return func(error) {
  72. defer wg.Done()
  73. if errMark != nil && !*errMark {
  74. *errMark = true
  75. }
  76. }
  77. }
  78. t.Run("normal", func(t *testing.T) {
  79. task := &errWorkerTask{
  80. wg: &sync.WaitGroup{},
  81. }
  82. errMark := false
  83. errCb := errCbFactory(task.wg, &errMark)
  84. task.wg.Add(1)
  85. wm.Run(task, nil, errCb)
  86. task.wg.Wait()
  87. if errMark {
  88. t.Errorf("should be normal")
  89. }
  90. })
  91. t.Run("panic", func(t *testing.T) {
  92. task := &panicWorkerTask{
  93. wg: &sync.WaitGroup{},
  94. }
  95. errMark := false
  96. errCb := errCbFactory(task.wg, &errMark)
  97. task.wg.Add(2) // 1 for errCb
  98. wm.Run(task, nil, errCb)
  99. task.wg.Wait()
  100. if !errMark {
  101. t.Errorf("expecting error")
  102. }
  103. })
  104. }