mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-26 23:53:19 +08:00
ok fixed queue test
This commit is contained in:
@ -2,6 +2,7 @@ package queue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -72,18 +73,21 @@ func newPeerTime(t time.Time) *peer.Peer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSyncQueue(t *testing.T) {
|
func TestSyncQueue(t *testing.T) {
|
||||||
ctx, _ := context.WithTimeout(context.Background(), time.Second*4)
|
ctx := context.Background()
|
||||||
|
|
||||||
pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
|
pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"))
|
||||||
cq := NewChanQueue(ctx, pq)
|
cq := NewChanQueue(ctx, pq)
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
|
||||||
max := 100000
|
max := 10000
|
||||||
consumerN := 10
|
consumerN := 10
|
||||||
countsIn := make([]int, consumerN*2)
|
countsIn := make([]int, consumerN*2)
|
||||||
countsOut := make([]int, consumerN)
|
countsOut := make([]int, consumerN)
|
||||||
|
|
||||||
produce := func(p int) {
|
produce := func(p int) {
|
||||||
tick := time.Tick(time.Millisecond)
|
defer wg.Done()
|
||||||
|
|
||||||
|
tick := time.Tick(time.Microsecond * 100)
|
||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
select {
|
select {
|
||||||
case tim := <-tick:
|
case tim := <-tick:
|
||||||
@ -96,10 +100,15 @@ func TestSyncQueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
consume := func(c int) {
|
consume := func(c int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-cq.DeqChan:
|
case <-cq.DeqChan:
|
||||||
countsOut[c]++
|
countsOut[c]++
|
||||||
|
if countsOut[c] >= max*2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -108,14 +117,13 @@ func TestSyncQueue(t *testing.T) {
|
|||||||
|
|
||||||
// make n * 2 producers and n consumers
|
// make n * 2 producers and n consumers
|
||||||
for i := 0; i < consumerN; i++ {
|
for i := 0; i < consumerN; i++ {
|
||||||
|
wg.Add(3)
|
||||||
go produce(i)
|
go produce(i)
|
||||||
go produce(consumerN + i)
|
go produce(consumerN + i)
|
||||||
go consume(i)
|
go consume(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
wg.Wait()
|
||||||
case <-ctx.Done():
|
|
||||||
}
|
|
||||||
|
|
||||||
sum := func(ns []int) int {
|
sum := func(ns []int) int {
|
||||||
total := 0
|
total := 0
|
||||||
@ -126,6 +134,6 @@ func TestSyncQueue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if sum(countsIn) != sum(countsOut) {
|
if sum(countsIn) != sum(countsOut) {
|
||||||
t.Errorf("didnt get all of them out: %d/%d", countsOut, countsIn)
|
t.Errorf("didnt get all of them out: %d/%d", sum(countsOut), sum(countsIn))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user