github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/peer/queue/queue_test.go (about) 1 package queue 2 3 import ( 4 "fmt" 5 "sync" 6 "testing" 7 "time" 8 9 key "github.com/ipfs/go-ipfs/blocks/key" 10 peer "github.com/ipfs/go-ipfs/p2p/peer" 11 u "github.com/ipfs/go-ipfs/util" 12 13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 14 ) 15 16 func TestQueue(t *testing.T) { 17 18 p1 := peer.ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31") // these aren't valid, because need to hex-decode. 19 p2 := peer.ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32") // these aren't valid, because need to hex-decode. 20 p3 := peer.ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33") // these aren't valid, because need to hex-decode. 21 p4 := peer.ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34") // these aren't valid, because need to hex-decode. 22 p5 := peer.ID("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31") // these aren't valid, because need to hex-decode. 23 // but they work. 24 25 // these are the peer.IDs' XORKeySpace Key values: 26 // [228 47 151 130 156 102 222 232 218 31 132 94 170 208 80 253 120 103 55 35 91 237 48 157 81 245 57 247 66 150 9 40] 27 // [26 249 85 75 54 49 25 30 21 86 117 62 85 145 48 175 155 194 210 216 58 14 241 143 28 209 129 144 122 28 163 6] 28 // [78 135 26 216 178 181 224 181 234 117 2 248 152 115 255 103 244 34 4 152 193 88 9 225 8 127 216 158 226 8 236 246] 29 // [125 135 124 6 226 160 101 94 192 57 39 12 18 79 121 140 190 154 147 55 44 83 101 151 63 255 94 179 51 203 241 51] 30 31 pq := NewXORDistancePQ(key.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31")) 32 pq.Enqueue(p3) 33 pq.Enqueue(p1) 34 pq.Enqueue(p2) 35 pq.Enqueue(p4) 36 pq.Enqueue(p5) 37 pq.Enqueue(p1) 38 39 // should come out as: p1, p4, p3, p2 40 41 if d := pq.Dequeue(); d != p1 && d != p5 { 42 t.Error("ordering failed") 43 } 44 45 if d := pq.Dequeue(); d != p1 && d != p5 { 46 t.Error("ordering failed") 47 } 48 49 if d := pq.Dequeue(); d != p1 && d != p5 { 50 t.Error("ordering failed") 51 } 52 53 if pq.Dequeue() != p4 { 54 t.Error("ordering failed") 55 } 56 57 if pq.Dequeue() != p3 { 58 t.Error("ordering failed") 59 } 60 61 if pq.Dequeue() != p2 { 62 t.Error("ordering failed") 63 } 64 65 } 66 67 func newPeerTime(t time.Time) peer.ID { 68 s := fmt.Sprintf("hmmm time: %v", t) 69 h := u.Hash([]byte(s)) 70 return peer.ID(h) 71 } 72 73 func TestSyncQueue(t *testing.T) { 74 tickT := time.Microsecond * 50 75 max := 5000 76 consumerN := 10 77 countsIn := make([]int, consumerN*2) 78 countsOut := make([]int, consumerN) 79 80 if testing.Short() { 81 max = 1000 82 } 83 84 ctx := context.Background() 85 pq := NewXORDistancePQ(key.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31")) 86 cq := NewChanQueue(ctx, pq) 87 wg := sync.WaitGroup{} 88 89 produce := func(p int) { 90 defer wg.Done() 91 92 tick := time.Tick(tickT) 93 for i := 0; i < max; i++ { 94 select { 95 case tim := <-tick: 96 countsIn[p]++ 97 cq.EnqChan <- newPeerTime(tim) 98 case <-ctx.Done(): 99 return 100 } 101 } 102 } 103 104 consume := func(c int) { 105 defer wg.Done() 106 107 for { 108 select { 109 case <-cq.DeqChan: 110 countsOut[c]++ 111 if countsOut[c] >= max*2 { 112 return 113 } 114 case <-ctx.Done(): 115 return 116 } 117 } 118 } 119 120 // make n * 2 producers and n consumers 121 for i := 0; i < consumerN; i++ { 122 wg.Add(3) 123 go produce(i) 124 go produce(consumerN + i) 125 go consume(i) 126 } 127 128 wg.Wait() 129 130 sum := func(ns []int) int { 131 total := 0 132 for _, n := range ns { 133 total += n 134 } 135 return total 136 } 137 138 if sum(countsIn) != sum(countsOut) { 139 t.Errorf("didnt get all of them out: %d/%d", sum(countsOut), sum(countsIn)) 140 } 141 }