@cryptotaxi247 / kubo / commits / 65f95a194

mux test race #270

Juan Batiz-Benet committed Nov 5, 2014 at 07:51 UTC 65f95a1940b3cb7e7e3b94333d5dc771f7ebf3b1
1 file changed +12
net/mux/mux_test.go
+12
@@ -3,6 +3,7 @@ package mux
3 import (
4 "bytes"
5 "fmt"
6 + "sync"
7 "testing"
8 "time"
9
@@ -121,6 +122,7 @@ func TestSimultMuxer(t *testing.T) {
122 total := 10000
123 speed := time.Microsecond * 1
124 counts := [2][2][2]int{}
125 + var countsLock sync.Mutex
126
127 // run producers at every end sending incrementing messages
128 produceOut := func(pid pb.ProtocolID, size int) {
@@ -130,7 +132,9 @@ func TestSimultMuxer(t *testing.T) {
132 s := fmt.Sprintf("proto %v out %v", pid, i)
133 m := msg.New(peer1, []byte(s))
134 mux1.Protocols[pid].GetPipe().Outgoing <- m
135 + countsLock.Lock()
136 counts[pid][0][0]++
137 + countsLock.Unlock()
138 // log.Debug("sent %v", s)
139 }
140 }
@@ -147,7 +151,9 @@ func TestSimultMuxer(t *testing.T) {
151
152 m := msg.New(peer1, d)
153 mux1.Incoming <- m
154 + countsLock.Lock()
155 counts[pid][1][0]++
156 + countsLock.Unlock()
157 // log.Debug("sent %v", s)
158 }
159 }
@@ -163,7 +169,9 @@ func TestSimultMuxer(t *testing.T) {
169
170 // log.Debug("got %v", string(data))
171 _ = data
172 + countsLock.Lock()
173 counts[pid][1][1]++
174 + countsLock.Unlock()
175
176 case <-ctx.Done():
177 return
@@ -175,7 +183,9 @@ func TestSimultMuxer(t *testing.T) {
183 for {
184 select {
185 case m := <-mux1.Protocols[pid].GetPipe().Incoming:
186 + countsLock.Lock()
187 counts[pid][0][1]++
188 + countsLock.Unlock()
189 // log.Debug("got %v", string(m.Data()))
190 _ = m
191 case <-ctx.Done():
@@ -195,10 +205,12 @@ func TestSimultMuxer(t *testing.T) {
205 limiter := time.Tick(speed)
206 for {
207 <-limiter
208 + countsLock.Lock()
209 got := counts[0][0][0] + counts[0][0][1] +
210 counts[0][1][0] + counts[0][1][1] +
211 counts[1][0][0] + counts[1][0][1] +
212 counts[1][1][0] + counts[1][1][1]
213 + countsLock.Unlock()
214
215 if got == total*8 {
216 cancel()