MakeConnectedSwitches function

This commit is contained in:
Ethan Buchman
2016-06-25 21:59:52 -04:00
parent 7376a72dd7
commit 711d2541f5
4 changed files with 94 additions and 57 deletions

View File

@ -415,3 +415,45 @@ type SwitchEventDonePeer struct {
Peer *Peer
Error interface{}
}
//------------------------------------------------------------------
// Switches connected via arbitrary net.Conn; useful for testing
// Returns n fully connected switches.
// initSwitch defines how the ith switch should be initialized (ie. with what reactors).
func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connPipe func() (net.Conn, net.Conn)) []*Switch {
switches := make([]*Switch, n)
for i := 0; i < n; i++ {
switches[i] = makeSwitch(i, "testing", "123.123.123", initSwitch)
}
for i := 0; i < n; i++ {
switchI := switches[i]
for j := i; j < n; j++ {
switchJ := switches[j]
c1, c2 := connPipe()
go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake.
go switchJ.AddPeerWithConnection(c2, true)
}
}
// Wait for things to happen, peers to get added...
time.Sleep(100 * time.Millisecond * time.Duration(n*n))
return switches
}
func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch {
privKey := crypto.GenPrivKeyEd25519()
// new switch, add reactors
// TODO: let the config be passed in?
s := initSwitch(i, NewSwitch(cfg.NewMapConfig(nil)))
s.SetNodeInfo(&NodeInfo{
PubKey: privKey.PubKey().(crypto.PubKeyEd25519),
Moniker: Fmt("switch%d", i),
Network: network,
Version: version,
})
s.SetNodePrivKey(privKey)
s.Start() // start switch and reactors
return s
}