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 ...@@ -2,6 +2,7 @@ package mocknet
import ( import (
"fmt" "fmt"
"sort"
"sync" "sync"
"time" "time"
...@@ -90,6 +91,7 @@ func (mn *mocknet) Peers() []peer.ID { ...@@ -90,6 +91,7 @@ func (mn *mocknet) Peers() []peer.ID {
for _, n := range mn.nets { for _, n := range mn.nets {
cp = append(cp, n.peer) cp = append(cp, n.peer)
} }
sort.Sort(peer.IDSlice(cp))
return cp return cp
} }
...@@ -115,6 +117,8 @@ func (mn *mocknet) Hosts() []host.Host { ...@@ -115,6 +117,8 @@ func (mn *mocknet) Hosts() []host.Host {
for _, h := range mn.hosts { for _, h := range mn.hosts {
cp = append(cp, h) cp = append(cp, h)
} }
sort.Sort(hostSlice(cp))
return cp return cp
} }
...@@ -126,6 +130,7 @@ func (mn *mocknet) Nets() []inet.Network { ...@@ -126,6 +130,7 @@ func (mn *mocknet) Nets() []inet.Network {
for _, n := range mn.nets { for _, n := range mn.nets {
cp = append(cp, n) cp = append(cp, n)
} }
sort.Sort(netSlice(cp))
return cp return cp
} }
...@@ -339,3 +344,17 @@ func (mn *mocknet) LinkDefaults() LinkOptions { ...@@ -339,3 +344,17 @@ func (mn *mocknet) LinkDefaults() LinkOptions {
defer mn.RUnlock() defer mn.RUnlock()
return mn.linkDefaults 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 { ...@@ -130,3 +130,10 @@ type PeerInfo struct {
ID ID ID ID
Addrs []ma.Multiaddr 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