TestMultipleWriters starts multiple writers and one reader goroutine and makes sure that the reader gets all the data written by all writers.
(t *testing.T)
| 88 | // TestMultipleWriters starts multiple writers and one reader goroutine and |
| 89 | // makes sure that the reader gets all the data written by all writers. |
| 90 | func (s) TestMultipleWriters(t *testing.T) { |
| 91 | ub := NewUnbounded() |
| 92 | reads := []int{} |
| 93 | |
| 94 | var wg sync.WaitGroup |
| 95 | wg.Add(1) |
| 96 | go func() { |
| 97 | defer wg.Done() |
| 98 | ch := ub.Get() |
| 99 | for i := 0; i < numWriters*numWrites; i++ { |
| 100 | r := <-ch |
| 101 | reads = append(reads, r.(int)) |
| 102 | ub.Load() |
| 103 | } |
| 104 | }() |
| 105 | |
| 106 | wg.Add(numWriters) |
| 107 | for i := 0; i < numWriters; i++ { |
| 108 | go func(index int) { |
| 109 | defer wg.Done() |
| 110 | for j := 0; j < numWrites; j++ { |
| 111 | ub.Put(index) |
| 112 | } |
| 113 | }(i) |
| 114 | } |
| 115 | |
| 116 | wg.Wait() |
| 117 | sort.Ints(reads) |
| 118 | if !reflect.DeepEqual(reads, wantReads) { |
| 119 | t.Errorf("reads: %#v, wantReads: %#v", reads, wantReads) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // TestClose closes the buffer and makes sure that nothing is sent after the |
| 124 | // buffer is closed. |