Commit f3c490a4 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

peer+mocknet: sorting for determinism.

parent e4f1221b
......@@ -2,6 +2,7 @@ package mocknet
import (
"fmt"
"sort"
"sync"
"time"
......@@ -90,6 +91,7 @@ func (mn *mocknet) Peers() []peer.ID {
for _, n := range mn.nets {
cp = append(cp, n.peer)
}
sort.Sort(peer.IDSlice(cp))
return cp
}
......@@ -115,6 +117,8 @@ func (mn *mocknet) Hosts() []host.Host {
for _, h := range mn.hosts {
cp = append(cp, h)
}
sort.Sort(hostSlice(cp))
return cp
}
......@@ -126,6 +130,7 @@ func (mn *mocknet) Nets() []inet.Network {
for _, n := range mn.nets {
cp = append(cp, n)
}
sort.Sort(netSlice(cp))
return cp
}
......@@ -339,3 +344,17 @@ func (mn *mocknet) LinkDefaults() LinkOptions {
defer mn.RUnlock()
return mn.linkDefaults
}
// netSlice for sorting by peer
type netSlice []inet.Network
func (es netSlice) Len() int { return len(es) }
func (es netSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es netSlice) Less(i, j int) bool { return string(es[i].LocalPeer()) < string(es[j].LocalPeer()) }
// hostSlice for sorting by peer
type hostSlice []host.Host
func (es hostSlice) Len() int { return len(es) }
func (es hostSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es hostSlice) Less(i, j int) bool { return string(es[i].ID()) < string(es[j].ID()) }
......@@ -130,3 +130,10 @@ type PeerInfo struct {
ID ID
Addrs []ma.Multiaddr
}
// IDSlice for sorting peers
type IDSlice []ID
func (es IDSlice) Len() int { return len(es) }
func (es IDSlice) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es IDSlice) Less(i, j int) bool { return string(es[i]) < string(es[j]) }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment