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.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
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, reactorsGenerator func() []Reactor) (*Switch, *Switch) {
  60. // Create two switches that will be interconnected.
  61. s1 := NewSwitch(reactorsGenerator())
  62. s2 := NewSwitch(reactorsGenerator())
  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() []Reactor {
  87. // Make two reactors of two channels each
  88. reactors := make([]Reactor, 2)
  89. reactors[0] = NewTestReactor([]*ChannelDescriptor{
  90. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  91. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  92. }, true)
  93. reactors[1] = NewTestReactor([]*ChannelDescriptor{
  94. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  95. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  96. }, true)
  97. return reactors
  98. })
  99. defer s1.Stop()
  100. defer s2.Stop()
  101. // Lets send a message from s1 to s2.
  102. if s1.Peers().Size() != 1 {
  103. t.Errorf("Expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  104. }
  105. if s2.Peers().Size() != 1 {
  106. t.Errorf("Expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  107. }
  108. ch0Msg := "channel zero"
  109. ch1Msg := "channel one"
  110. ch2Msg := "channel two"
  111. s1.Broadcast(byte(0x00), ch0Msg)
  112. s1.Broadcast(byte(0x01), ch1Msg)
  113. s1.Broadcast(byte(0x02), ch2Msg)
  114. // Wait for things to settle...
  115. time.Sleep(5000 * time.Millisecond)
  116. // Check message on ch0
  117. ch0Msgs := s2.Reactors()[0].(*TestReactor).msgsReceived[byte(0x00)]
  118. if len(ch0Msgs) != 2 {
  119. t.Errorf("Expected to have received 1 message in ch0")
  120. }
  121. if !bytes.Equal(ch0Msgs[1].Bytes, binary.BinaryBytes(ch0Msg)) {
  122. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch0Msg), ch0Msgs[0].Bytes)
  123. }
  124. // Check message on ch1
  125. ch1Msgs := s2.Reactors()[0].(*TestReactor).msgsReceived[byte(0x01)]
  126. if len(ch1Msgs) != 1 {
  127. t.Errorf("Expected to have received 1 message in ch1")
  128. }
  129. if !bytes.Equal(ch1Msgs[0].Bytes, binary.BinaryBytes(ch1Msg)) {
  130. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch1Msg), ch1Msgs[0].Bytes)
  131. }
  132. // Check message on ch2
  133. ch2Msgs := s2.Reactors()[1].(*TestReactor).msgsReceived[byte(0x02)]
  134. if len(ch2Msgs) != 1 {
  135. t.Errorf("Expected to have received 1 message in ch2")
  136. }
  137. if !bytes.Equal(ch2Msgs[0].Bytes, binary.BinaryBytes(ch2Msg)) {
  138. t.Errorf("Unexpected message bytes. Wanted: %X, Got: %X", binary.BinaryBytes(ch2Msg), ch2Msgs[0].Bytes)
  139. }
  140. }
  141. func BenchmarkSwitches(b *testing.B) {
  142. b.StopTimer()
  143. s1, s2 := makeSwitchPair(b, func() []Reactor {
  144. // Make two reactors of two channels each
  145. reactors := make([]Reactor, 2)
  146. reactors[0] = NewTestReactor([]*ChannelDescriptor{
  147. &ChannelDescriptor{Id: byte(0x00), Priority: 10},
  148. &ChannelDescriptor{Id: byte(0x01), Priority: 10},
  149. }, false)
  150. reactors[1] = NewTestReactor([]*ChannelDescriptor{
  151. &ChannelDescriptor{Id: byte(0x02), Priority: 10},
  152. &ChannelDescriptor{Id: byte(0x03), Priority: 10},
  153. }, false)
  154. return reactors
  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 one 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. }