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.

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