peer+mocknet: sorting for determinism.
Juan Batiz-Benet committed
Jan 5, 2015 at 04:30 UTC
9d0736bc3b546f2fcf63ada12bb0ced14c288f8e
2 files changed
+26
p2p/net/mock/mock_net.go
+19
@@ -2,6 +2,7 @@ package mocknet
2
3
import (
4
"fmt"
5
+ "sort"
6
"sync"
7
"time"
8
@@ -90,6 +91,7 @@ func (mn *mocknet) Peers() []peer.ID {
91
for _, n := range mn.nets {
92
cp = append(cp, n.peer)
93
}
94
+ sort.Sort(peer.IDSlice(cp))
95
return cp
96
}
97
@@ -115,6 +117,8 @@ func (mn *mocknet) Hosts() []host.Host {
117
for _, h := range mn.hosts {
118
cp = append(cp, h)
119
}
120
+
121
+ sort.Sort(hostSlice(cp))
122
return cp
123
}
124
@@ -126,6 +130,7 @@ func (mn *mocknet) Nets() []inet.Network {
130
for _, n := range mn.nets {
131
cp = append(cp, n)
132
}
133
+ sort.Sort(netSlice(cp))
134
return cp
135
}
136
@@ -339,3 +344,17 @@ func (mn *mocknet) LinkDefaults() LinkOptions {
344
defer mn.RUnlock()
345
return mn.linkDefaults
346
}
347
+
348
+// netSlice for sorting by peer
349
+type netSlice []inet.Network
350
+
351
+func (es netSlice) Len() int { return len(es) }
352
+func (es netSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
353
+func (es netSlice) Less(i, j int) bool { return string(es[i].LocalPeer()) < string(es[j].LocalPeer()) }
354
+
355
+// hostSlice for sorting by peer
356
+type hostSlice []host.Host
357
+
358
+func (es hostSlice) Len() int { return len(es) }
359
+func (es hostSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
360
+func (es hostSlice) Less(i, j int) bool { return string(es[i].ID()) < string(es[j].ID()) }
p2p/peer/peer.go
+7
@@ -130,3 +130,10 @@ type PeerInfo struct {
130
ID ID
131
Addrs []ma.Multiaddr
132
}
133
+
134
+// IDSlice for sorting peers
135
+type IDSlice []ID
136
+
137
+func (es IDSlice) Len() int { return len(es) }
138
+func (es IDSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
139
+func (es IDSlice) Less(i, j int) bool { return string(es[i]) < string(es[j]) }