v3_queue_test.go 5.7 KB

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