github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/peer/queue/queue_test.go (about) 1 package queue 2 3 import ( 4 "fmt" 5 "sync" 6 "testing" 7 "time" 8 9 peer "github.com/jbenet/go-ipfs/peer" 10 u "github.com/jbenet/go-ipfs/util" 11 12 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" 13 ) 14 15 func newPeer(id string) peer.Peer { 16 return peer.WithIDString(id) 17 } 18 19 func TestQueue(t *testing.T) { 20 21 p1 := newPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31") 22 p2 := newPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32") 23 p3 := newPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33") 24 p4 := newPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34") 25 p5 := newPeer("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31") 26 27 // these are the peer.IDs' XORKeySpace Key values: 28 // [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] 29 // [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] 30 // [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] 31 // [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] 32 33 pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31")) 34 pq.Enqueue(p3) 35 pq.Enqueue(p1) 36 pq.Enqueue(p2) 37 pq.Enqueue(p4) 38 pq.Enqueue(p5) 39 pq.Enqueue(p1) 40 41 // should come out as: p1, p4, p3, p2 42 43 if d := pq.Dequeue(); d != p1 && d != p5 { 44 t.Error("ordering failed") 45 } 46 47 if d := pq.Dequeue(); d != p1 && d != p5 { 48 t.Error("ordering failed") 49 } 50 51 if d := pq.Dequeue(); d != p1 && d != p5 { 52 t.Error("ordering failed") 53 } 54 55 if pq.Dequeue() != p4 { 56 t.Error("ordering failed") 57 } 58 59 if pq.Dequeue() != p3 { 60 t.Error("ordering failed") 61 } 62 63 if pq.Dequeue() != p2 { 64 t.Error("ordering failed") 65 } 66 67 } 68 69 func newPeerTime(t time.Time) peer.Peer { 70 s := fmt.Sprintf("hmmm time: %v", t) 71 h := u.Hash([]byte(s)) 72 return peer.WithID(peer.ID(h)) 73 } 74 75 func TestSyncQueue(t *testing.T) { 76 ctx := context.Background() 77 78 pq := NewXORDistancePQ(u.Key("11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31")) 79 cq := NewChanQueue(ctx, pq) 80 wg := sync.WaitGroup{} 81 82 max := 10000 83 consumerN := 10 84 countsIn := make([]int, consumerN*2) 85 countsOut := make([]int, consumerN) 86 87 produce := func(p int) { 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: 94 countsIn[p]++ 95 cq.EnqChan <- newPeerTime(tim) 96 case <-ctx.Done(): 97 return 98 } 99 } 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 } 115 } 116 } 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 126 wg.Wait() 127 128 sum := func(ns []int) int { 129 total := 0 130 for _, n := range ns { 131 total += n 132 } 133 return total 134 } 135 136 if sum(countsIn) != sum(countsOut) { 137 t.Errorf("didnt get all of them out: %d/%d", sum(countsOut), sum(countsIn)) 138 } 139 }