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.

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