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.

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