ok fixed queue test
Juan Batiz-Benet committed
Sep 26, 2014 at 03:26 UTC
2507680d779eca88efe36ecb02bb38795872f93b
1 file changed
+15
-7
peer/queue/queue_test.go
+15
-7
@@ -2,6 +2,7 @@ package queue
2
3
import (
4
"fmt"
5
+ "sync"
6
"testing"
7
"time"
8
@@ -72,18 +73,21 @@ func newPeerTime(t time.Time) *peer.Peer {
73
}
74
75
func TestSyncQueue(t *testing.T) {
75
- ctx, _ := context.WithTimeout(context.Background(), time.Second*4)
76
+ ctx := context.Background()
77
78
pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
79
cq := NewChanQueue(ctx, pq)
80
+ wg := sync.WaitGroup{}
81
80
- max := 100000
82
+ max := 10000
83
consumerN := 10
84
countsIn := make([]int, consumerN*2)
85
countsOut := make([]int, consumerN)
86
87
produce := func(p int) {
86
- tick := time.Tick(time.Millisecond)
88
+ defer wg.Done()
89
+
90
+ tick := time.Tick(time.Microsecond * 100)
91
for i := 0; i < max; i++ {
92
select {
93
case tim := <-tick:
@@ -96,10 +100,15 @@ func TestSyncQueue(t *testing.T) {
100
}
101
102
consume := func(c int) {
103
+ defer wg.Done()
104
+
105
for {
106
select {
107
case <-cq.DeqChan:
108
countsOut[c]++
109
+ if countsOut[c] >= max*2 {
110
+ return
111
+ }
112
case <-ctx.Done():
113
return
114
}
@@ -108,14 +117,13 @@ func TestSyncQueue(t *testing.T) {
117
118
// make n * 2 producers and n consumers
119
for i := 0; i < consumerN; i++ {
120
+ wg.Add(3)
121
go produce(i)
122
go produce(consumerN + i)
123
go consume(i)
124
}
125
116
- select {
117
- case <-ctx.Done():
118
- }
126
+ wg.Wait()
127
128
sum := func(ns []int) int {
129
total := 0
@@ -126,6 +134,6 @@ func TestSyncQueue(t *testing.T) {
134
}
135
136
if sum(countsIn) != sum(countsOut) {
129
- t.Errorf("didnt get all of them out: %d/%d", countsOut, countsIn)
137
+ t.Errorf("didnt get all of them out: %d/%d", sum(countsOut), sum(countsIn))
138
}
139
}