timed_workers_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package scheduler
  14. import (
  15. "sync"
  16. "sync/atomic"
  17. "testing"
  18. "time"
  19. )
  20. func TestExecute(t *testing.T) {
  21. testVal := int32(0)
  22. wg := sync.WaitGroup{}
  23. wg.Add(5)
  24. queue := CreateWorkerQueue(func(args *WorkArgs) error {
  25. atomic.AddInt32(&testVal, 1)
  26. wg.Done()
  27. return nil
  28. })
  29. now := time.Now()
  30. queue.AddWork(NewWorkArgs("1", "1"), now, now)
  31. queue.AddWork(NewWorkArgs("2", "2"), now, now)
  32. queue.AddWork(NewWorkArgs("3", "3"), now, now)
  33. queue.AddWork(NewWorkArgs("4", "4"), now, now)
  34. queue.AddWork(NewWorkArgs("5", "5"), now, now)
  35. // Adding the same thing second time should be no-op
  36. queue.AddWork(NewWorkArgs("1", "1"), now, now)
  37. queue.AddWork(NewWorkArgs("2", "2"), now, now)
  38. queue.AddWork(NewWorkArgs("3", "3"), now, now)
  39. queue.AddWork(NewWorkArgs("4", "4"), now, now)
  40. queue.AddWork(NewWorkArgs("5", "5"), now, now)
  41. wg.Wait()
  42. lastVal := atomic.LoadInt32(&testVal)
  43. if lastVal != 5 {
  44. t.Errorf("Espected testVal = 5, got %v", lastVal)
  45. }
  46. }
  47. func TestExecuteDelayed(t *testing.T) {
  48. testVal := int32(0)
  49. wg := sync.WaitGroup{}
  50. wg.Add(5)
  51. queue := CreateWorkerQueue(func(args *WorkArgs) error {
  52. atomic.AddInt32(&testVal, 1)
  53. wg.Done()
  54. return nil
  55. })
  56. now := time.Now()
  57. then := now.Add(10 * time.Second)
  58. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  59. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  60. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  61. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  62. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  63. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  64. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  65. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  66. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  67. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  68. wg.Wait()
  69. lastVal := atomic.LoadInt32(&testVal)
  70. if lastVal != 5 {
  71. t.Errorf("Espected testVal = 5, got %v", lastVal)
  72. }
  73. }
  74. func TestCancel(t *testing.T) {
  75. testVal := int32(0)
  76. wg := sync.WaitGroup{}
  77. wg.Add(3)
  78. queue := CreateWorkerQueue(func(args *WorkArgs) error {
  79. atomic.AddInt32(&testVal, 1)
  80. wg.Done()
  81. return nil
  82. })
  83. now := time.Now()
  84. then := now.Add(10 * time.Second)
  85. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  86. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  87. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  88. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  89. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  90. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  91. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  92. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  93. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  94. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  95. queue.CancelWork(NewWorkArgs("2", "2").KeyFromWorkArgs())
  96. queue.CancelWork(NewWorkArgs("4", "4").KeyFromWorkArgs())
  97. wg.Wait()
  98. lastVal := atomic.LoadInt32(&testVal)
  99. if lastVal != 3 {
  100. t.Errorf("Espected testVal = 3, got %v", lastVal)
  101. }
  102. }
  103. func TestCancelAndReadd(t *testing.T) {
  104. testVal := int32(0)
  105. wg := sync.WaitGroup{}
  106. wg.Add(4)
  107. queue := CreateWorkerQueue(func(args *WorkArgs) error {
  108. atomic.AddInt32(&testVal, 1)
  109. wg.Done()
  110. return nil
  111. })
  112. now := time.Now()
  113. then := now.Add(10 * time.Second)
  114. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  115. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  116. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  117. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  118. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  119. queue.AddWork(NewWorkArgs("1", "1"), now, then)
  120. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  121. queue.AddWork(NewWorkArgs("3", "3"), now, then)
  122. queue.AddWork(NewWorkArgs("4", "4"), now, then)
  123. queue.AddWork(NewWorkArgs("5", "5"), now, then)
  124. queue.CancelWork(NewWorkArgs("2", "2").KeyFromWorkArgs())
  125. queue.CancelWork(NewWorkArgs("4", "4").KeyFromWorkArgs())
  126. queue.AddWork(NewWorkArgs("2", "2"), now, then)
  127. wg.Wait()
  128. lastVal := atomic.LoadInt32(&testVal)
  129. if lastVal != 4 {
  130. t.Errorf("Espected testVal = 4, got %v", lastVal)
  131. }
  132. }