messenger_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package p2p
  2. import (
  3. // "fmt"
  4. "bytes"
  5. "github.com/ethereum/eth-go/ethutil"
  6. "testing"
  7. "time"
  8. )
  9. func setupMessenger(handlers Handlers) (*TestNetworkConnection, chan *PeerError, *Messenger) {
  10. errchan := NewPeerErrorChannel()
  11. addr := &TestAddr{"test:30303"}
  12. net := NewTestNetworkConnection(addr)
  13. conn := NewConnection(net, errchan)
  14. mess := NewMessenger(nil, conn, errchan, handlers)
  15. mess.Start()
  16. return net, errchan, mess
  17. }
  18. type TestProtocol struct {
  19. Msgs []*Msg
  20. }
  21. func (self *TestProtocol) Start() {
  22. }
  23. func (self *TestProtocol) Stop() {
  24. }
  25. func (self *TestProtocol) Offset() MsgCode {
  26. return MsgCode(5)
  27. }
  28. func (self *TestProtocol) HandleIn(msg *Msg, response chan *Msg) {
  29. self.Msgs = append(self.Msgs, msg)
  30. close(response)
  31. }
  32. func (self *TestProtocol) HandleOut(msg *Msg) bool {
  33. if msg.Code() > 3 {
  34. return false
  35. } else {
  36. return true
  37. }
  38. }
  39. func (self *TestProtocol) Name() string {
  40. return "a"
  41. }
  42. func Packet(offset MsgCode, code MsgCode, params ...interface{}) []byte {
  43. msg, _ := NewMsg(code, params...)
  44. encoded := msg.Encode(offset)
  45. packet := []byte{34, 64, 8, 145}
  46. packet = append(packet, ethutil.NumberToBytes(uint32(len(encoded)), 32)...)
  47. return append(packet, encoded...)
  48. }
  49. func TestRead(t *testing.T) {
  50. handlers := make(Handlers)
  51. testProtocol := &TestProtocol{Msgs: []*Msg{}}
  52. handlers["a"] = func(p *Peer) Protocol { return testProtocol }
  53. net, _, mess := setupMessenger(handlers)
  54. mess.AddProtocols([]string{"a"})
  55. defer mess.Stop()
  56. wait := 1 * time.Millisecond
  57. packet := Packet(16, 1, uint32(1), "000")
  58. go net.In(0, packet)
  59. time.Sleep(wait)
  60. if len(testProtocol.Msgs) != 1 {
  61. t.Errorf("msg not relayed to correct protocol")
  62. } else {
  63. if testProtocol.Msgs[0].Code() != 1 {
  64. t.Errorf("incorrect msg code relayed to protocol")
  65. }
  66. }
  67. }
  68. func TestWrite(t *testing.T) {
  69. handlers := make(Handlers)
  70. testProtocol := &TestProtocol{Msgs: []*Msg{}}
  71. handlers["a"] = func(p *Peer) Protocol { return testProtocol }
  72. net, _, mess := setupMessenger(handlers)
  73. mess.AddProtocols([]string{"a"})
  74. defer mess.Stop()
  75. wait := 1 * time.Millisecond
  76. msg, _ := NewMsg(3, uint32(1), "000")
  77. err := mess.Write("b", msg)
  78. if err == nil {
  79. t.Errorf("expect error for unknown protocol")
  80. }
  81. err = mess.Write("a", msg)
  82. if err != nil {
  83. t.Errorf("expect no error for known protocol: %v", err)
  84. } else {
  85. time.Sleep(wait)
  86. if len(net.Out) != 1 {
  87. t.Errorf("msg not written")
  88. } else {
  89. out := net.Out[0]
  90. packet := Packet(16, 3, uint32(1), "000")
  91. if bytes.Compare(out, packet) != 0 {
  92. t.Errorf("incorrect packet %v", out)
  93. }
  94. }
  95. }
  96. }
  97. func TestPulse(t *testing.T) {
  98. net, _, mess := setupMessenger(make(Handlers))
  99. defer mess.Stop()
  100. ping := false
  101. timeout := false
  102. pingTimeout := 10 * time.Millisecond
  103. gracePeriod := 200 * time.Millisecond
  104. go mess.PingPong(pingTimeout, gracePeriod, func() { ping = true }, func() { timeout = true })
  105. net.In(0, Packet(0, 1))
  106. if ping {
  107. t.Errorf("ping sent too early")
  108. }
  109. time.Sleep(pingTimeout + 100*time.Millisecond)
  110. if !ping {
  111. t.Errorf("no ping sent after timeout")
  112. }
  113. if timeout {
  114. t.Errorf("timeout too early")
  115. }
  116. ping = false
  117. net.In(0, Packet(0, 1))
  118. time.Sleep(pingTimeout + 100*time.Millisecond)
  119. if !ping {
  120. t.Errorf("no ping sent after timeout")
  121. }
  122. if timeout {
  123. t.Errorf("timeout too early")
  124. }
  125. ping = false
  126. time.Sleep(gracePeriod)
  127. if ping {
  128. t.Errorf("ping called twice")
  129. }
  130. if !timeout {
  131. t.Errorf("no timeout after grace period")
  132. }
  133. }