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.

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