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.

216 lines
5.7 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
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
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 two switches connected to each other.
  59. func makeSwitchPair(t testing.TB, initSwitch func(*Switch) *Switch) (*Switch, *Switch) {
  60. // Create two switches that will be interconnected.
  61. s1 := initSwitch(NewSwitch())
  62. s2 := initSwitch(NewSwitch())
  63. s1.Start()
  64. s2.Start()
  65. // Create a listener for s1
  66. l := NewDefaultListener("tcp", ":8001", true)
  67. // Dial the listener & add the connection to s2.
  68. lAddr := l.ExternalAddress()
  69. connOut, err := lAddr.Dial()
  70. if err != nil {
  71. t.Fatalf("Could not connect to listener address %v", lAddr)
  72. } else {
  73. t.Logf("Created a connection to listener address %v", lAddr)
  74. }
  75. connIn, ok := <-l.Connections()
  76. if !ok {
  77. t.Fatalf("Could not get inbound connection from listener")
  78. }
  79. s1.AddPeerWithConnection(connIn, false)
  80. s2.AddPeerWithConnection(connOut, true)
  81. // Wait for things to happen, peers to get added...
  82. time.Sleep(100 * time.Millisecond)
  83. // Close the server, no longer needed.
  84. l.Stop()
  85. return s1, s2
  86. }
  87. func TestSwitches(t *testing.T) {
  88. s1, s2 := makeSwitchPair(t, func(sw *Switch) *Switch {
  89. // Make two reactors of two channels each
  90. sw.AddReactor("foo", NewTestReactor([]*ChannelDescriptor{
  91. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  92. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  93. }, true)).Start(sw) // Start the reactor
  94. sw.AddReactor("bar", NewTestReactor([]*ChannelDescriptor{
  95. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  96. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  97. }, true)).Start(sw) // Start the reactor
  98. return sw
  99. })
  100. defer s1.Stop()
  101. defer s2.Stop()
  102. // Lets send a message from s1 to s2.
  103. if s1.Peers().Size() != 1 {
  104. t.Errorf("Expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  105. }
  106. if s2.Peers().Size() != 1 {
  107. t.Errorf("Expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  108. }
  109. ch0Msg := "channel zero"
  110. ch1Msg := "channel foo"
  111. ch2Msg := "channel bar"
  112. s1.Broadcast(byte(0x00), ch0Msg)
  113. s1.Broadcast(byte(0x01), ch1Msg)
  114. s1.Broadcast(byte(0x02), ch2Msg)
  115. // Wait for things to settle...
  116. time.Sleep(5000 * time.Millisecond)
  117. // Check message on ch0
  118. ch0Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x00)]
  119. if len(ch0Msgs) != 2 {
  120. t.Errorf("Expected to have received 1 message in ch0")
  121. }
  122. if !bytes.Equal(ch0Msgs[1].Bytes, binary.BinaryBytes(ch0Msg)) {
  123. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch0Msg), ch0Msgs[0].Bytes)
  124. }
  125. // Check message on ch1
  126. ch1Msgs := s2.Reactor("foo").(*TestReactor).msgsReceived[byte(0x01)]
  127. if len(ch1Msgs) != 1 {
  128. t.Errorf("Expected to have received 1 message in ch1")
  129. }
  130. if !bytes.Equal(ch1Msgs[0].Bytes, binary.BinaryBytes(ch1Msg)) {
  131. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch1Msg), ch1Msgs[0].Bytes)
  132. }
  133. // Check message on ch2
  134. ch2Msgs := s2.Reactor("bar").(*TestReactor).msgsReceived[byte(0x02)]
  135. if len(ch2Msgs) != 1 {
  136. t.Errorf("Expected to have received 1 message in ch2")
  137. }
  138. if !bytes.Equal(ch2Msgs[0].Bytes, binary.BinaryBytes(ch2Msg)) {
  139. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch2Msg), ch2Msgs[0].Bytes)
  140. }
  141. }
  142. func BenchmarkSwitches(b *testing.B) {
  143. b.StopTimer()
  144. s1, s2 := makeSwitchPair(b, func(sw *Switch) *Switch {
  145. // Make bar reactors of bar channels each
  146. sw.AddReactor("foo", NewTestReactor([]*ChannelDescriptor{
  147. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  148. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  149. }, false))
  150. sw.AddReactor("bar", NewTestReactor([]*ChannelDescriptor{
  151. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  152. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  153. }, false))
  154. return sw
  155. })
  156. defer s1.Stop()
  157. defer s2.Stop()
  158. // Allow time for goroutines to boot up
  159. time.Sleep(1000 * time.Millisecond)
  160. b.StartTimer()
  161. numSuccess, numFailure := 0, 0
  162. // Send random message from foo channel to another
  163. for i := 0; i < b.N; i++ {
  164. chId := byte(i % 4)
  165. successChan := s1.Broadcast(chId, "test data")
  166. for s := range successChan {
  167. if s {
  168. numSuccess += 1
  169. } else {
  170. numFailure += 1
  171. }
  172. }
  173. }
  174. log.Warn(Fmt("success: %v, failure: %v", numSuccess, numFailure))
  175. // Allow everything to flush before stopping switches & closing connections.
  176. b.StopTimer()
  177. time.Sleep(1000 * time.Millisecond)
  178. }