You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package peer
  2. import (
  3. "testing"
  4. "time"
  5. . "github.com/tendermint/tendermint/binary"
  6. )
  7. // convenience method for creating two clients connected to each other.
  8. func makeClientPair(t testing.TB, bufferSize int, chNames []String) (*Client, *Client) {
  9. peerMaker := func(conn *Connection) *Peer {
  10. channels := map[String]*Channel{}
  11. for _, chName := range chNames {
  12. channels[chName] = NewChannel(chName, bufferSize)
  13. }
  14. return NewPeer(conn, channels)
  15. }
  16. // Create two clients that will be interconnected.
  17. c1 := NewClient(peerMaker)
  18. c2 := NewClient(peerMaker)
  19. // Create a server for the listening client.
  20. s1 := NewServer("tcp", ":8001", c1)
  21. // Dial the server & add the connection to c2.
  22. s1laddr := s1.ExternalAddress()
  23. conn, err := s1laddr.Dial()
  24. if err != nil {
  25. t.Fatalf("Could not connect to server address %v", s1laddr)
  26. } else {
  27. t.Logf("Created a connection to local server address %v", s1laddr)
  28. }
  29. c2.AddPeerWithConnection(conn, true)
  30. // Wait for things to happen, peers to get added...
  31. time.Sleep(100 * time.Millisecond)
  32. // Close the server, no longer needed.
  33. s1.Stop()
  34. return c1, c2
  35. }
  36. func TestClients(t *testing.T) {
  37. channels := []String{"ch1", "ch2", "ch3"}
  38. c1, c2 := makeClientPair(t, 10, channels)
  39. defer c1.Stop()
  40. defer c2.Stop()
  41. // Lets send a message from c1 to c2.
  42. if c1.Peers().Size() != 1 {
  43. t.Errorf("Expected exactly 1 peer in c1, got %v", c1.Peers().Size())
  44. }
  45. if c2.Peers().Size() != 1 {
  46. t.Errorf("Expected exactly 1 peer in c2, got %v", c2.Peers().Size())
  47. }
  48. // Broadcast a message on ch1
  49. c1.Broadcast(NewPacket("ch1", ByteSlice("channel one")))
  50. // Broadcast a message on ch2
  51. c1.Broadcast(NewPacket("ch2", ByteSlice("channel two")))
  52. // Broadcast a message on ch3
  53. c1.Broadcast(NewPacket("ch3", ByteSlice("channel three")))
  54. // Wait for things to settle...
  55. time.Sleep(100 * time.Millisecond)
  56. // Receive message from channel 2 and check
  57. inMsg := c2.Receive("ch2")
  58. if string(inMsg.Bytes) != "channel two" {
  59. t.Errorf("Unexpected received message bytes: %v", string(inMsg.Bytes))
  60. }
  61. // Receive message from channel 1 and check
  62. inMsg = c2.Receive("ch1")
  63. if string(inMsg.Bytes) != "channel one" {
  64. t.Errorf("Unexpected received message bytes: %v", string(inMsg.Bytes))
  65. }
  66. }
  67. func BenchmarkClients(b *testing.B) {
  68. b.StopTimer()
  69. channels := []String{"ch1", "ch2", "ch3"}
  70. c1, c2 := makeClientPair(b, 10, channels)
  71. defer c1.Stop()
  72. defer c2.Stop()
  73. // Create a sink on either channel to just pop off messages.
  74. recvHandler := func(c *Client, chName String) {
  75. for {
  76. it := c.Receive(chName)
  77. if it == nil {
  78. break
  79. }
  80. }
  81. }
  82. for _, chName := range channels {
  83. go recvHandler(c1, chName)
  84. go recvHandler(c2, chName)
  85. }
  86. // Allow time for goroutines to boot up
  87. time.Sleep(1000 * time.Millisecond)
  88. b.StartTimer()
  89. numSuccess, numFailure := 0, 0
  90. // Send random message from one channel to another
  91. for i := 0; i < b.N; i++ {
  92. chName := channels[i%len(channels)]
  93. pkt := NewPacket(chName, ByteSlice("test data"))
  94. nS, nF := c1.Broadcast(pkt)
  95. numSuccess += nS
  96. numFailure += nF
  97. }
  98. log.Warnf("success: %v, failure: %v", numSuccess, numFailure)
  99. // Allow everything to flush before stopping clients & closing connections.
  100. b.StopTimer()
  101. time.Sleep(1000 * time.Millisecond)
  102. }