whisper: mock tests to use simulated peers

This commit is contained in:
Péter Szilágyi 2015-04-14 19:00:57 +03:00
parent 86372b20c0
commit 4fb7ab5d09
2 changed files with 55 additions and 71 deletions

View File

@ -3,7 +3,9 @@
package whisper package whisper
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
@ -36,3 +38,33 @@ func whisperCaps() []p2p.Cap {
}, },
} }
} }
// bufMsgPipe creates a buffered message pipe between two endpoints.
func bufMsgPipe() (*p2p.MsgPipeRW, *p2p.MsgPipeRW) {
A, midA := p2p.MsgPipe()
midB, B := p2p.MsgPipe()
go copyMsgPipe(midA, midB)
go copyMsgPipe(midB, midA)
return A, B
}
// copyMsgPipe copies messages from the src pipe to the dest.
func copyMsgPipe(dst, src *p2p.MsgPipeRW) {
defer dst.Close()
for {
msg, err := src.ReadMsg()
if err != nil {
return
}
data, err := ioutil.ReadAll(msg.Payload)
if err != nil {
return
}
msg.Payload = bytes.NewReader(data)
if err := dst.WriteMsg(msg); err != nil {
return
}
}
}

View File

@ -1,75 +1,36 @@
package whisper package whisper
import ( import (
"fmt"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
) )
type testNode struct { func startTestCluster(n int) []*Whisper {
server *p2p.Server // Create the batch of simulated peers
client *Whisper nodes := make([]*p2p.Peer, n)
}
func startNodes(n int) ([]*testNode, error) {
// Start up the cluster of nodes
cluster := make([]*testNode, 0, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
shh := New() nodes[i] = p2p.NewPeer(randomNodeID(), randomNodeName(), whisperCaps())
}
whispers := make([]*Whisper, n)
for i := 0; i < n; i++ {
whispers[i] = New()
whispers[i].Start()
}
// Wire all the peers to the root one
for i := 1; i < n; i++ {
src, dst := bufMsgPipe()
// Generate the node identity go whispers[0].handlePeer(nodes[i], src)
key, err := crypto.GenerateKey() go whispers[i].handlePeer(nodes[0], dst)
if err != nil {
return nil, err
}
name := common.MakeName(fmt.Sprintf("whisper-go-test-%d", i), "1.0")
// Create an Ethereum server to communicate through
server := &p2p.Server{
PrivateKey: key,
MaxPeers: 10,
Name: name,
Protocols: []p2p.Protocol{shh.Protocol()},
ListenAddr: fmt.Sprintf(":%d", 30300+i),
NAT: nat.Any(),
}
if err := server.Start(); err != nil {
return nil, err
}
// Peer online, store and iterate
cluster = append(cluster, &testNode{
server: server,
client: shh,
})
}
// Manually wire together the cluster nodes
root := cluster[0].server.Self()
for _, node := range cluster[1:] {
node.server.SuggestPeer(root)
}
return cluster, nil
}
func stopNodes(cluster []*testNode) {
for _, node := range cluster {
node.server.Stop()
} }
return whispers
} }
func TestSelfMessage(t *testing.T) { func TestSelfMessage(t *testing.T) {
// Start the single node cluster // Start the single node cluster
cluster, err := startNodes(1) client := startTestCluster(1)[0]
if err != nil {
t.Fatalf("failed to boot test cluster: %v", err)
}
defer stopNodes(cluster)
client := cluster[0].client
// Start watching for self messages, signal any arrivals // Start watching for self messages, signal any arrivals
self := client.NewIdentity() self := client.NewIdentity()
@ -104,16 +65,12 @@ func TestSelfMessage(t *testing.T) {
func TestDirectMessage(t *testing.T) { func TestDirectMessage(t *testing.T) {
// Start the sender-recipient cluster // Start the sender-recipient cluster
cluster, err := startNodes(2) cluster := startTestCluster(2)
if err != nil {
t.Fatalf("failed to boot test cluster: %v", err)
}
defer stopNodes(cluster)
sender := cluster[0].client sender := cluster[0]
senderId := sender.NewIdentity() senderId := sender.NewIdentity()
recipient := cluster[1].client recipient := cluster[1]
recipientId := recipient.NewIdentity() recipientId := recipient.NewIdentity()
// Watch for arriving messages on the recipient // Watch for arriving messages on the recipient
@ -155,18 +112,13 @@ func TestIdentifiedBroadcast(t *testing.T) {
func testBroadcast(anonymous bool, t *testing.T) { func testBroadcast(anonymous bool, t *testing.T) {
// Start the single sender multi recipient cluster // Start the single sender multi recipient cluster
cluster, err := startNodes(3) cluster := startTestCluster(3)
if err != nil {
t.Fatalf("failed to boot test cluster: %v", err)
}
defer stopNodes(cluster)
sender := cluster[0].client sender := cluster[1]
targets := make([]*Whisper, len(cluster)-1) targets := cluster[1:]
for i, node := range cluster[1:] { for _, target := range targets {
targets[i] = node.client
if !anonymous { if !anonymous {
targets[i].NewIdentity() target.NewIdentity()
} }
} }
// Watch for arriving messages on the recipients // Watch for arriving messages on the recipients