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.

149 lines
3.8 KiB

11 years ago
11 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, numChannels int, sendQueueCapacity int, recvBufferSize int, recvQueueCapacity int) (*Switch, *Switch, []*ChannelDescriptor) {
  9. // Make numChannels channels starting at byte(0x00)
  10. chIds := []byte{}
  11. for i := 0; i < numChannels; i++ {
  12. chIds = append(chIds, byte(i))
  13. }
  14. // Make some channel descriptors.
  15. chDescs := []*ChannelDescriptor{}
  16. for _, chId := range chIds {
  17. chDescs = append(chDescs, &ChannelDescriptor{
  18. Id: chId,
  19. SendQueueCapacity: sendQueueCapacity,
  20. RecvBufferSize: recvBufferSize,
  21. RecvQueueCapacity: recvQueueCapacity,
  22. DefaultPriority: 1,
  23. })
  24. }
  25. // Create two switches that will be interconnected.
  26. s1 := NewSwitch(chDescs)
  27. s2 := NewSwitch(chDescs)
  28. // Create a listener for s1
  29. l := NewDefaultListener("tcp", ":8001")
  30. // Dial the listener & add the connection to s2.
  31. lAddr := l.ExternalAddress()
  32. connOut, err := lAddr.Dial()
  33. if err != nil {
  34. t.Fatalf("Could not connect to listener address %v", lAddr)
  35. } else {
  36. t.Logf("Created a connection to listener address %v", lAddr)
  37. }
  38. connIn, ok := <-l.Connections()
  39. if !ok {
  40. t.Fatalf("Could not get inbound connection from listener")
  41. }
  42. s1.AddPeerWithConnection(connIn, false)
  43. s2.AddPeerWithConnection(connOut, true)
  44. // Wait for things to happen, peers to get added...
  45. time.Sleep(100 * time.Millisecond)
  46. // Close the server, no longer needed.
  47. l.Stop()
  48. return s1, s2, chDescs
  49. }
  50. func TestSwitches(t *testing.T) {
  51. s1, s2, _ := makeSwitchPair(t, 10, 10, 1024, 10)
  52. defer s1.Stop()
  53. defer s2.Stop()
  54. // Lets send a message from s1 to s2.
  55. if s1.Peers().Size() != 1 {
  56. t.Errorf("Expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  57. }
  58. if s2.Peers().Size() != 1 {
  59. t.Errorf("Expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  60. }
  61. // Broadcast a message on ch0
  62. s1.Broadcast(byte(0x00), String("channel zero"))
  63. // Broadcast a message on ch1
  64. s1.Broadcast(byte(0x01), String("channel one"))
  65. // Broadcast a message on ch2
  66. s1.Broadcast(byte(0x02), String("channel two"))
  67. // Wait for things to settle...
  68. time.Sleep(100 * time.Millisecond)
  69. // Receive message from channel 1 and check
  70. inMsg, ok := s2.Receive(byte(0x01))
  71. if !ok {
  72. t.Errorf("Failed to receive from channel one")
  73. }
  74. if ReadString(inMsg.Bytes.Reader()) != "channel one" {
  75. t.Errorf("Unexpected received message bytes: %X = [%v]", inMsg.Bytes, ReadString(inMsg.Bytes.Reader()))
  76. }
  77. // Receive message from channel 0 and check
  78. inMsg, ok = s2.Receive(byte(0x00))
  79. if !ok {
  80. t.Errorf("Failed to receive from channel zero")
  81. }
  82. if ReadString(inMsg.Bytes.Reader()) != "channel zero" {
  83. t.Errorf("Unexpected received message bytes: %X = [%v]", inMsg.Bytes, ReadString(inMsg.Bytes.Reader()))
  84. }
  85. }
  86. func BenchmarkSwitches(b *testing.B) {
  87. b.StopTimer()
  88. s1, s2, chDescs := makeSwitchPair(b, 10, 10, 1024, 10)
  89. defer s1.Stop()
  90. defer s2.Stop()
  91. // Create a sink on either channel to just pop off messages.
  92. recvHandler := func(c *Switch, chId byte) {
  93. for {
  94. _, ok := c.Receive(chId)
  95. if !ok {
  96. break
  97. }
  98. }
  99. }
  100. // Create routines to consume from recvQueues.
  101. for _, chDesc := range chDescs {
  102. go recvHandler(s1, chDesc.Id)
  103. go recvHandler(s2, chDesc.Id)
  104. }
  105. // Allow time for goroutines to boot up
  106. time.Sleep(1000 * time.Millisecond)
  107. b.StartTimer()
  108. numSuccess, numFailure := 0, 0
  109. // Send random message from one channel to another
  110. for i := 0; i < b.N; i++ {
  111. chId := chDescs[i%len(chDescs)].Id
  112. nS, nF := s1.Broadcast(chId, String("test data"))
  113. numSuccess += nS
  114. numFailure += nF
  115. }
  116. log.Warning("success: %v, failure: %v", numSuccess, numFailure)
  117. // Allow everything to flush before stopping switches & closing connections.
  118. b.StopTimer()
  119. time.Sleep(1000 * time.Millisecond)
  120. }