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.

137 lines
3.5 KiB

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