unbounded_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2019 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package buffer
  18. import (
  19. "reflect"
  20. "sort"
  21. "sync"
  22. "testing"
  23. "google.golang.org/grpc/internal/grpctest"
  24. )
  25. const (
  26. numWriters = 10
  27. numWrites = 10
  28. )
  29. type s struct {
  30. grpctest.Tester
  31. }
  32. func Test(t *testing.T) {
  33. grpctest.RunSubTests(t, s{})
  34. }
  35. // wantReads contains the set of values expected to be read by the reader
  36. // goroutine in the tests.
  37. var wantReads []int
  38. func init() {
  39. for i := 0; i < numWriters; i++ {
  40. for j := 0; j < numWrites; j++ {
  41. wantReads = append(wantReads, i)
  42. }
  43. }
  44. }
  45. // TestSingleWriter starts one reader and one writer goroutine and makes sure
  46. // that the reader gets all the value added to the buffer by the writer.
  47. func (s) TestSingleWriter(t *testing.T) {
  48. ub := NewUnbounded()
  49. reads := []int{}
  50. var wg sync.WaitGroup
  51. wg.Add(1)
  52. go func() {
  53. defer wg.Done()
  54. ch := ub.Get()
  55. for i := 0; i < numWriters*numWrites; i++ {
  56. r := <-ch
  57. reads = append(reads, r.(int))
  58. ub.Load()
  59. }
  60. }()
  61. wg.Add(1)
  62. go func() {
  63. defer wg.Done()
  64. for i := 0; i < numWriters; i++ {
  65. for j := 0; j < numWrites; j++ {
  66. ub.Put(i)
  67. }
  68. }
  69. }()
  70. wg.Wait()
  71. if !reflect.DeepEqual(reads, wantReads) {
  72. t.Errorf("reads: %#v, wantReads: %#v", reads, wantReads)
  73. }
  74. }
  75. // TestMultipleWriters starts multiple writers and one reader goroutine and
  76. // makes sure that the reader gets all the data written by all writers.
  77. func (s) TestMultipleWriters(t *testing.T) {
  78. ub := NewUnbounded()
  79. reads := []int{}
  80. var wg sync.WaitGroup
  81. wg.Add(1)
  82. go func() {
  83. defer wg.Done()
  84. ch := ub.Get()
  85. for i := 0; i < numWriters*numWrites; i++ {
  86. r := <-ch
  87. reads = append(reads, r.(int))
  88. ub.Load()
  89. }
  90. }()
  91. wg.Add(numWriters)
  92. for i := 0; i < numWriters; i++ {
  93. go func(index int) {
  94. defer wg.Done()
  95. for j := 0; j < numWrites; j++ {
  96. ub.Put(index)
  97. }
  98. }(i)
  99. }
  100. wg.Wait()
  101. sort.Ints(reads)
  102. if !reflect.DeepEqual(reads, wantReads) {
  103. t.Errorf("reads: %#v, wantReads: %#v", reads, wantReads)
  104. }
  105. }
  106. // TestClose closes the buffer and makes sure that nothing is sent after the
  107. // buffer is closed.
  108. func (s) TestClose(t *testing.T) {
  109. ub := NewUnbounded()
  110. ub.Close()
  111. if v, ok := <-ub.Get(); ok {
  112. t.Errorf("Unbounded.Get() = %v, want closed channel", v)
  113. }
  114. ub.Put(1)
  115. ub.Load()
  116. if v, ok := <-ub.Get(); ok {
  117. t.Errorf("Unbounded.Get() = %v, want closed channel", v)
  118. }
  119. ub.Close()
  120. }