|
@ -1,19 +1,20 @@ |
|
|
package peer |
|
|
package peer |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
|
|
|
"testing" |
|
|
"testing" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// convenience method for creating two clients connected to each other.
|
|
|
// convenience method for creating two clients connected to each other.
|
|
|
func makeClientPair(t *testing.T, bufferSize int, channels []string) (*Client, *Client) { |
|
|
|
|
|
|
|
|
func makeClientPair(t testing.TB, bufferSize int, channels []String) (*Client, *Client) { |
|
|
|
|
|
|
|
|
peerMaker := func(conn *Connection) *Peer { |
|
|
peerMaker := func(conn *Connection) *Peer { |
|
|
p := NewPeer(conn) |
|
|
p := NewPeer(conn) |
|
|
p.channels = map[String]*Channel{} |
|
|
p.channels = map[String]*Channel{} |
|
|
for chName := range channels { |
|
|
|
|
|
p.channels[String(chName)] = NewChannel(String(chName), bufferSize) |
|
|
|
|
|
|
|
|
for _, chName := range channels { |
|
|
|
|
|
p.channels[chName] = NewChannel(chName, bufferSize) |
|
|
} |
|
|
} |
|
|
return p |
|
|
return p |
|
|
} |
|
|
} |
|
@ -39,12 +40,18 @@ func makeClientPair(t *testing.T, bufferSize int, channels []string) (*Client, * |
|
|
// Wait for things to happen, peers to get added...
|
|
|
// Wait for things to happen, peers to get added...
|
|
|
time.Sleep(100 * time.Millisecond) |
|
|
time.Sleep(100 * time.Millisecond) |
|
|
|
|
|
|
|
|
|
|
|
// Close the server, no longer needed.
|
|
|
|
|
|
s1.Stop() |
|
|
|
|
|
|
|
|
return c1, c2 |
|
|
return c1, c2 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestClients(t *testing.T) { |
|
|
func TestClients(t *testing.T) { |
|
|
|
|
|
|
|
|
c1, c2 := makeClientPair(t, 10, []string{"ch1", "ch2", "ch3"}) |
|
|
|
|
|
|
|
|
channels := []String{"ch1", "ch2", "ch3"} |
|
|
|
|
|
c1, c2 := makeClientPair(t, 10, channels) |
|
|
|
|
|
defer c1.Stop() |
|
|
|
|
|
defer c2.Stop() |
|
|
|
|
|
|
|
|
// Lets send a message from c1 to c2.
|
|
|
// Lets send a message from c1 to c2.
|
|
|
if c1.Peers().Size() != 1 { |
|
|
if c1.Peers().Size() != 1 { |
|
@ -75,31 +82,51 @@ func TestClients(t *testing.T) { |
|
|
if string(inMsg.Bytes) != "channel one" { |
|
|
if string(inMsg.Bytes) != "channel one" { |
|
|
t.Errorf("Unexpected received message bytes: %v", string(inMsg.Bytes)) |
|
|
t.Errorf("Unexpected received message bytes: %v", string(inMsg.Bytes)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
s1.Stop() |
|
|
|
|
|
c2.Stop() |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func BenchmarkClients(b *testing.B) { |
|
|
func BenchmarkClients(b *testing.B) { |
|
|
|
|
|
|
|
|
b.StopTimer() |
|
|
b.StopTimer() |
|
|
|
|
|
|
|
|
// TODO: benchmark the random functions, which is faster?
|
|
|
|
|
|
|
|
|
|
|
|
c1, c2 := makeClientPair(t, 10, []string{"ch1", "ch2", "ch3"}) |
|
|
|
|
|
|
|
|
channels := []String{"ch1", "ch2", "ch3"} |
|
|
|
|
|
c1, c2 := makeClientPair(b, 10, channels) |
|
|
|
|
|
defer c1.Stop() |
|
|
|
|
|
defer c2.Stop() |
|
|
|
|
|
|
|
|
// Create a sink on either channel to just pop off messages.
|
|
|
// Create a sink on either channel to just pop off messages.
|
|
|
// TODO: ensure that when clients stop, this goroutine stops.
|
|
|
|
|
|
recvHandler := func(c *Client) { |
|
|
|
|
|
|
|
|
recvHandler := func(c *Client, chName String) { |
|
|
|
|
|
for { |
|
|
|
|
|
it := c.Receive(chName) |
|
|
|
|
|
if it == nil { |
|
|
|
|
|
break |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
go recvHandler(c1) |
|
|
|
|
|
go recvHandler(c2) |
|
|
|
|
|
|
|
|
for _, chName := range channels { |
|
|
|
|
|
go recvHandler(c1, chName) |
|
|
|
|
|
go recvHandler(c2, chName) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Allow time for goroutines to boot up
|
|
|
|
|
|
time.Sleep(1000 * time.Millisecond) |
|
|
b.StartTimer() |
|
|
b.StartTimer() |
|
|
|
|
|
|
|
|
|
|
|
numSuccess, numFailure := 0, 0 |
|
|
|
|
|
|
|
|
// Send random message from one channel to another
|
|
|
// Send random message from one channel to another
|
|
|
for i := 0; i < b.N; i++ { |
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
|
chName := channels[i%len(channels)] |
|
|
|
|
|
pkt := NewPacket(chName, ByteSlice("test data")) |
|
|
|
|
|
nS, nF := c1.Broadcast(pkt) |
|
|
|
|
|
numSuccess += nS |
|
|
|
|
|
numFailure += nF |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Warnf("success: %v, failure: %v", numSuccess, numFailure) |
|
|
|
|
|
|
|
|
|
|
|
// Allow everything to flush before stopping clients & closing connections.
|
|
|
|
|
|
b.StopTimer() |
|
|
|
|
|
time.Sleep(1000 * time.Millisecond) |
|
|
|
|
|
|
|
|
} |
|
|
} |