queue_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2015 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 workqueue_test
  14. import (
  15. "sync"
  16. "testing"
  17. "time"
  18. "k8s.io/kubernetes/pkg/util/workqueue"
  19. )
  20. func TestBasic(t *testing.T) {
  21. // If something is seriously wrong this test will never complete.
  22. q := workqueue.New()
  23. // Start producers
  24. const producers = 50
  25. producerWG := sync.WaitGroup{}
  26. producerWG.Add(producers)
  27. for i := 0; i < producers; i++ {
  28. go func(i int) {
  29. defer producerWG.Done()
  30. for j := 0; j < 50; j++ {
  31. q.Add(i)
  32. time.Sleep(time.Millisecond)
  33. }
  34. }(i)
  35. }
  36. // Start consumers
  37. const consumers = 10
  38. consumerWG := sync.WaitGroup{}
  39. consumerWG.Add(consumers)
  40. for i := 0; i < consumers; i++ {
  41. go func(i int) {
  42. defer consumerWG.Done()
  43. for {
  44. item, quit := q.Get()
  45. if item == "added after shutdown!" {
  46. t.Errorf("Got an item added after shutdown.")
  47. }
  48. if quit {
  49. return
  50. }
  51. t.Logf("Worker %v: begin processing %v", i, item)
  52. time.Sleep(3 * time.Millisecond)
  53. t.Logf("Worker %v: done processing %v", i, item)
  54. q.Done(item)
  55. }
  56. }(i)
  57. }
  58. producerWG.Wait()
  59. q.ShutDown()
  60. q.Add("added after shutdown!")
  61. consumerWG.Wait()
  62. }
  63. func TestAddWhileProcessing(t *testing.T) {
  64. q := workqueue.New()
  65. // Start producers
  66. const producers = 50
  67. producerWG := sync.WaitGroup{}
  68. producerWG.Add(producers)
  69. for i := 0; i < producers; i++ {
  70. go func(i int) {
  71. defer producerWG.Done()
  72. q.Add(i)
  73. }(i)
  74. }
  75. // Start consumers
  76. const consumers = 10
  77. consumerWG := sync.WaitGroup{}
  78. consumerWG.Add(consumers)
  79. for i := 0; i < consumers; i++ {
  80. go func(i int) {
  81. defer consumerWG.Done()
  82. // Every worker will re-add every item up to two times.
  83. // This tests the dirty-while-processing case.
  84. counters := map[interface{}]int{}
  85. for {
  86. item, quit := q.Get()
  87. if quit {
  88. return
  89. }
  90. counters[item]++
  91. if counters[item] < 2 {
  92. q.Add(item)
  93. }
  94. q.Done(item)
  95. }
  96. }(i)
  97. }
  98. producerWG.Wait()
  99. q.ShutDown()
  100. consumerWG.Wait()
  101. }
  102. func TestLen(t *testing.T) {
  103. q := workqueue.New()
  104. q.Add("foo")
  105. if e, a := 1, q.Len(); e != a {
  106. t.Errorf("Expected %v, got %v", e, a)
  107. }
  108. q.Add("bar")
  109. if e, a := 2, q.Len(); e != a {
  110. t.Errorf("Expected %v, got %v", e, a)
  111. }
  112. q.Add("foo") // should not increase the queue length.
  113. if e, a := 2, q.Len(); e != a {
  114. t.Errorf("Expected %v, got %v", e, a)
  115. }
  116. }