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.

214 lines
5.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package p2p
  2. import (
  3. "bytes"
  4. "sync"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/tendermint/binary"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. type PeerMessage struct {
  11. PeerKey string
  12. Bytes []byte
  13. Counter int
  14. }
  15. type TestReactor struct {
  16. mtx sync.Mutex
  17. channels []*ChannelDescriptor
  18. peersAdded []*Peer
  19. peersRemoved []*Peer
  20. logMessages bool
  21. msgsCounter int
  22. msgsReceived map[byte][]PeerMessage
  23. }
  24. func NewTestReactor(channels []*ChannelDescriptor, logMessages bool) *TestReactor {
  25. return &TestReactor{
  26. channels: channels,
  27. logMessages: logMessages,
  28. msgsReceived: make(map[byte][]PeerMessage),
  29. }
  30. }
  31. func (tr *TestReactor) Start(sw *Switch) {
  32. }
  33. func (tr *TestReactor) Stop() {
  34. }
  35. func (tr *TestReactor) GetChannels() []*ChannelDescriptor {
  36. return tr.channels
  37. }
  38. func (tr *TestReactor) AddPeer(peer *Peer) {
  39. tr.mtx.Lock()
  40. defer tr.mtx.Unlock()
  41. tr.peersAdded = append(tr.peersAdded, peer)
  42. }
  43. func (tr *TestReactor) RemovePeer(peer *Peer, reason interface{}) {
  44. tr.mtx.Lock()
  45. defer tr.mtx.Unlock()
  46. tr.peersRemoved = append(tr.peersRemoved, peer)
  47. }
  48. func (tr *TestReactor) Receive(chId byte, peer *Peer, msgBytes []byte) {
  49. if tr.logMessages {
  50. tr.mtx.Lock()
  51. defer tr.mtx.Unlock()
  52. //fmt.Printf("Received: %X, %X\n", chId, msgBytes)
  53. tr.msgsReceived[chId] = append(tr.msgsReceived[chId], PeerMessage{peer.Key, msgBytes, tr.msgsCounter})
  54. tr.msgsCounter++
  55. }
  56. }
  57. //-----------------------------------------------------------------------------
  58. // convenience method for creating bar switches connected to each other.
  59. func makeSwitchPair(t testing.TB, initSwitch func(*Switch) *Switch) (*Switch, *Switch) {
  60. // Create bar switches that will be interconnected.
  61. s1 := initSwitch(NewSwitch())
  62. s2 := initSwitch(NewSwitch())
  63. // Create a listener for s1
  64. l := NewDefaultListener("tcp", ":8001", true)
  65. // Dial the listener & add the connection to s2.
  66. lAddr := l.ExternalAddress()
  67. connOut, err := lAddr.Dial()
  68. if err != nil {
  69. t.Fatalf("Could not connect to listener address %v", lAddr)
  70. } else {
  71. t.Logf("Created a connection to listener address %v", lAddr)
  72. }
  73. connIn, ok := <-l.Connections()
  74. if !ok {
  75. t.Fatalf("Could not get inbound connection from listener")
  76. }
  77. s1.AddPeerWithConnection(connIn, false)
  78. s2.AddPeerWithConnection(connOut, true)
  79. // Wait for things to happen, peers to get added...
  80. time.Sleep(100 * time.Millisecond)
  81. // Close the server, no longer needed.
  82. l.Stop()
  83. return s1, s2
  84. }
  85. func TestSwitches(t *testing.T) {
  86. s1, s2 := makeSwitchPair(t, func(sw *Switch) *Switch {
  87. // Make bar reactors of bar channels each
  88. sw.AddReactor("foo", NewTestReactor([]*ChannelDescriptor{
  89. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  90. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  91. }, true)).Start(sw) // Start the reactor
  92. sw.AddReactor("bar", NewTestReactor([]*ChannelDescriptor{
  93. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  94. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  95. }, true)).Start(sw) // Start the reactor
  96. return sw
  97. })
  98. defer s1.Stop()
  99. defer s2.Stop()
  100. // Lets send a message from s1 to s2.
  101. if s1.Peers().Size() != 1 {
  102. t.Errorf("Expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  103. }
  104. if s2.Peers().Size() != 1 {
  105. t.Errorf("Expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  106. }
  107. ch0Msg := "channel zero"
  108. ch1Msg := "channel foo"
  109. ch2Msg := "channel bar"
  110. s1.Broadcast(byte(0x00), ch0Msg)
  111. s1.Broadcast(byte(0x01), ch1Msg)
  112. s1.Broadcast(byte(0x02), ch2Msg)
  113. // Wait for things to settle...
  114. time.Sleep(5000 * time.Millisecond)
  115. // Check message on ch0
  116. ch0Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x00)]
  117. if len(ch0Msgs) != 2 {
  118. t.Errorf("Expected to have received 1 message in ch0")
  119. }
  120. if !bytes.Equal(ch0Msgs[1].Bytes, binary.BinaryBytes(ch0Msg)) {
  121. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch0Msg), ch0Msgs[0].Bytes)
  122. }
  123. // Check message on ch1
  124. ch1Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x01)]
  125. if len(ch1Msgs) != 1 {
  126. t.Errorf("Expected to have received 1 message in ch1")
  127. }
  128. if !bytes.Equal(ch1Msgs[0].Bytes, binary.BinaryBytes(ch1Msg)) {
  129. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch1Msg), ch1Msgs[0].Bytes)
  130. }
  131. // Check message on ch2
  132. ch2Msgs := s2.Reactor("bar").(*TestReactor).msgsReceived[byte(0x02)]
  133. if len(ch2Msgs) != 1 {
  134. t.Errorf("Expected to have received 1 message in ch2")
  135. }
  136. if !bytes.Equal(ch2Msgs[0].Bytes, binary.BinaryBytes(ch2Msg)) {
  137. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch2Msg), ch2Msgs[0].Bytes)
  138. }
  139. }
  140. func BenchmarkSwitches(b *testing.B) {
  141. b.StopTimer()
  142. s1, s2 := makeSwitchPair(b, func(sw *Switch) *Switch {
  143. // Make bar reactors of bar channels each
  144. sw.AddReactor("foo", NewTestReactor([]*ChannelDescriptor{
  145. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  146. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  147. }, false))
  148. sw.AddReactor("bar", NewTestReactor([]*ChannelDescriptor{
  149. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  150. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  151. }, false))
  152. return sw
  153. })
  154. defer s1.Stop()
  155. defer s2.Stop()
  156. // Allow time for goroutines to boot up
  157. time.Sleep(1000 * time.Millisecond)
  158. b.StartTimer()
  159. numSuccess, numFailure := 0, 0
  160. // Send random message from foo channel to another
  161. for i := 0; i < b.N; i++ {
  162. chId := byte(i % 4)
  163. successChan := s1.Broadcast(chId, "test data")
  164. for s := range successChan {
  165. if s {
  166. numSuccess += 1
  167. } else {
  168. numFailure += 1
  169. }
  170. }
  171. }
  172. log.Warn(Fmt("success: %v, failure: %v", numSuccess, numFailure))
  173. // Allow everything to flush before stopping switches & closing connections.
  174. b.StopTimer()
  175. time.Sleep(1000 * time.Millisecond)
  176. }