fixed test
Juan Batiz-Benet committed
Sep 19, 2014 at 22:40 UTC
8ed04cfe2391fbb42f5d6babfe48ffe5fb0ad39b
1 file changed
+24
-13
peer/queue/queue_test.go
+24
-13
@@ -76,15 +76,18 @@ func TestSyncQueue(t *testing.T) {
76
77
pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
78
cq := NewChanQueue(ctx, pq)
79
- countIn := 0
80
- countOut := 0
79
82
- produce := func() {
80
+ max := 100000
81
+ consumerN := 10
82
+ countsIn := make([]int, consumerN*2)
83
+ countsOut := make([]int, consumerN)
84
+
85
+ produce := func(p int) {
86
tick := time.Tick(time.Millisecond)
84
- for {
87
+ for i := 0; i < max; i++ {
88
select {
89
case tim := <-tick:
87
- countIn++
90
+ countsIn[p]++
91
cq.EnqChan <- newPeerTime(tim)
92
case <-ctx.Done():
93
return
@@ -92,29 +95,37 @@ func TestSyncQueue(t *testing.T) {
95
}
96
}
97
95
- consume := func() {
98
+ consume := func(c int) {
99
for {
100
select {
101
case <-cq.DeqChan:
99
- countOut++
102
+ countsOut[c]++
103
case <-ctx.Done():
104
return
105
}
106
}
107
}
108
106
- for i := 0; i < 10; i++ {
107
- go produce()
108
- go produce()
109
- go consume()
109
+ // make n * 2 producers and n consumers
110
+ for i := 0; i < consumerN; i++ {
111
+ go produce(i)
112
+ go produce(consumerN + i)
113
+ go consume(i)
114
}
115
116
select {
117
case <-ctx.Done():
118
}
119
116
- if countIn != countOut {
117
- t.Errorf("didnt get them all out: %d/%d", countOut, countIn)
120
+ sum := func(ns []int) int {
121
+ total := 0
122
+ for _, n := range ns {
123
+ total += n
124
+ }
125
+ return total
126
}
127
128
+ if sum(countsIn) != sum(countsOut) {
129
+ t.Errorf("didnt get all of them out: %d/%d", countsOut, countsIn)
130
+ }
131
}