v3_queue_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2016 The etcd Authors
  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 integration
  15. import (
  16. "fmt"
  17. "math/rand"
  18. "sync/atomic"
  19. "testing"
  20. "go.etcd.io/etcd/contrib/recipes"
  21. )
  22. const (
  23. manyQueueClients = 3
  24. queueItemsPerClient = 2
  25. )
  26. // TestQueueOneReaderOneWriter confirms the queue is FIFO
  27. func TestQueueOneReaderOneWriter(t *testing.T) {
  28. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  29. defer clus.Terminate(t)
  30. done := make(chan struct{})
  31. go func() {
  32. defer func() {
  33. done <- struct{}{}
  34. }()
  35. etcdc := clus.RandClient()
  36. q := recipe.NewQueue(etcdc, "testq")
  37. for i := 0; i < 5; i++ {
  38. if err := q.Enqueue(fmt.Sprintf("%d", i)); err != nil {
  39. t.Errorf("error enqueuing (%v)", err)
  40. }
  41. }
  42. }()
  43. etcdc := clus.RandClient()
  44. q := recipe.NewQueue(etcdc, "testq")
  45. for i := 0; i < 5; i++ {
  46. s, err := q.Dequeue()
  47. if err != nil {
  48. t.Fatalf("error dequeueing (%v)", err)
  49. }
  50. if s != fmt.Sprintf("%d", i) {
  51. t.Fatalf("expected dequeue value %v, got %v", s, i)
  52. }
  53. }
  54. <-done
  55. }
  56. func TestQueueManyReaderOneWriter(t *testing.T) {
  57. testQueueNReaderMWriter(t, manyQueueClients, 1)
  58. }
  59. func TestQueueOneReaderManyWriter(t *testing.T) {
  60. testQueueNReaderMWriter(t, 1, manyQueueClients)
  61. }
  62. func TestQueueManyReaderManyWriter(t *testing.T) {
  63. testQueueNReaderMWriter(t, manyQueueClients, manyQueueClients)
  64. }
  65. // BenchmarkQueue benchmarks Queues using many/many readers/writers
  66. func BenchmarkQueue(b *testing.B) {
  67. // XXX switch tests to use TB interface
  68. clus := NewClusterV3(nil, &ClusterConfig{Size: 3})
  69. defer clus.Terminate(nil)
  70. for i := 0; i < b.N; i++ {
  71. testQueueNReaderMWriter(nil, manyQueueClients, manyQueueClients)
  72. }
  73. }
  74. // TestPrQueueOneReaderOneWriter tests whether priority queues respect priorities.
  75. func TestPrQueueOneReaderOneWriter(t *testing.T) {
  76. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  77. defer clus.Terminate(t)
  78. // write out five items with random priority
  79. etcdc := clus.RandClient()
  80. q := recipe.NewPriorityQueue(etcdc, "testprq")
  81. for i := 0; i < 5; i++ {
  82. // [0, 2] priority for priority collision to test seq keys
  83. pr := uint16(rand.Intn(3))
  84. if err := q.Enqueue(fmt.Sprintf("%d", pr), pr); err != nil {
  85. t.Fatalf("error enqueuing (%v)", err)
  86. }
  87. }
  88. // read back items; confirm priority order is respected
  89. lastPr := -1
  90. for i := 0; i < 5; i++ {
  91. s, err := q.Dequeue()
  92. if err != nil {
  93. t.Fatalf("error dequeueing (%v)", err)
  94. }
  95. curPr := 0
  96. if _, err := fmt.Sscanf(s, "%d", &curPr); err != nil {
  97. t.Fatalf(`error parsing item "%s" (%v)`, s, err)
  98. }
  99. if lastPr > curPr {
  100. t.Fatalf("expected priority %v > %v", curPr, lastPr)
  101. }
  102. }
  103. }
  104. func TestPrQueueManyReaderManyWriter(t *testing.T) {
  105. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  106. defer clus.Terminate(t)
  107. rqs := newPriorityQueues(clus, manyQueueClients)
  108. wqs := newPriorityQueues(clus, manyQueueClients)
  109. testReadersWriters(t, rqs, wqs)
  110. }
  111. // BenchmarkQueue benchmarks Queues using n/n readers/writers
  112. func BenchmarkPrQueueOneReaderOneWriter(b *testing.B) {
  113. // XXX switch tests to use TB interface
  114. clus := NewClusterV3(nil, &ClusterConfig{Size: 3})
  115. defer clus.Terminate(nil)
  116. rqs := newPriorityQueues(clus, 1)
  117. wqs := newPriorityQueues(clus, 1)
  118. for i := 0; i < b.N; i++ {
  119. testReadersWriters(nil, rqs, wqs)
  120. }
  121. }
  122. func testQueueNReaderMWriter(t *testing.T, n int, m int) {
  123. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  124. defer clus.Terminate(t)
  125. testReadersWriters(t, newQueues(clus, n), newQueues(clus, m))
  126. }
  127. func newQueues(clus *ClusterV3, n int) (qs []testQueue) {
  128. for i := 0; i < n; i++ {
  129. etcdc := clus.RandClient()
  130. qs = append(qs, recipe.NewQueue(etcdc, "q"))
  131. }
  132. return qs
  133. }
  134. func newPriorityQueues(clus *ClusterV3, n int) (qs []testQueue) {
  135. for i := 0; i < n; i++ {
  136. etcdc := clus.RandClient()
  137. q := &flatPriorityQueue{recipe.NewPriorityQueue(etcdc, "prq")}
  138. qs = append(qs, q)
  139. }
  140. return qs
  141. }
  142. func testReadersWriters(t *testing.T, rqs []testQueue, wqs []testQueue) {
  143. rerrc := make(chan error)
  144. werrc := make(chan error)
  145. manyWriters(wqs, queueItemsPerClient, werrc)
  146. manyReaders(rqs, len(wqs)*queueItemsPerClient, rerrc)
  147. for range wqs {
  148. if err := <-werrc; err != nil {
  149. t.Errorf("error writing (%v)", err)
  150. }
  151. }
  152. for range rqs {
  153. if err := <-rerrc; err != nil {
  154. t.Errorf("error reading (%v)", err)
  155. }
  156. }
  157. }
  158. func manyReaders(qs []testQueue, totalReads int, errc chan<- error) {
  159. var rxReads int32
  160. for _, q := range qs {
  161. go func(q testQueue) {
  162. for {
  163. total := atomic.AddInt32(&rxReads, 1)
  164. if int(total) > totalReads {
  165. break
  166. }
  167. if _, err := q.Dequeue(); err != nil {
  168. errc <- err
  169. return
  170. }
  171. }
  172. errc <- nil
  173. }(q)
  174. }
  175. }
  176. func manyWriters(qs []testQueue, writesEach int, errc chan<- error) {
  177. for _, q := range qs {
  178. go func(q testQueue) {
  179. for j := 0; j < writesEach; j++ {
  180. if err := q.Enqueue("foo"); err != nil {
  181. errc <- err
  182. return
  183. }
  184. }
  185. errc <- nil
  186. }(q)
  187. }
  188. }
  189. type testQueue interface {
  190. Enqueue(val string) error
  191. Dequeue() (string, error)
  192. }
  193. type flatPriorityQueue struct{ *recipe.PriorityQueue }
  194. func (q *flatPriorityQueue) Enqueue(val string) error {
  195. // randomized to stress dequeuing logic; order isn't important
  196. return q.PriorityQueue.Enqueue(val, uint16(rand.Intn(2)))
  197. }
  198. func (q *flatPriorityQueue) Dequeue() (string, error) {
  199. return q.PriorityQueue.Dequeue()
  200. }