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.

139 lines
3.6 KiB

  1. package p2p_test
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. p2p "github.com/tendermint/go-p2p"
  9. )
  10. func createMConnection(conn net.Conn) *p2p.MConnection {
  11. onReceive := func(chID byte, msgBytes []byte) {
  12. }
  13. onError := func(r interface{}) {
  14. }
  15. return createMConnectionWithCallbacks(conn, onReceive, onError)
  16. }
  17. func createMConnectionWithCallbacks(conn net.Conn, onReceive func(chID byte, msgBytes []byte), onError func(r interface{})) *p2p.MConnection {
  18. chDescs := []*p2p.ChannelDescriptor{&p2p.ChannelDescriptor{ID: 0x01, Priority: 1, SendQueueCapacity: 1}}
  19. return p2p.NewMConnection(conn, chDescs, onReceive, onError)
  20. }
  21. func TestMConnectionSend(t *testing.T) {
  22. assert, require := assert.New(t), require.New(t)
  23. server, client := net.Pipe()
  24. defer server.Close()
  25. defer client.Close()
  26. mconn := createMConnection(client)
  27. _, err := mconn.Start()
  28. require.Nil(err)
  29. defer mconn.Stop()
  30. msg := "Ant-Man"
  31. assert.True(mconn.Send(0x01, msg))
  32. // Note: subsequent Send/TrySend calls could pass because we are reading from
  33. // the send queue in a separate goroutine.
  34. server.Read(make([]byte, len(msg)))
  35. assert.True(mconn.CanSend(0x01))
  36. msg = "Spider-Man"
  37. assert.True(mconn.TrySend(0x01, msg))
  38. server.Read(make([]byte, len(msg)))
  39. assert.False(mconn.CanSend(0x05), "CanSend should return false because channel is unknown")
  40. assert.False(mconn.Send(0x05, "Absorbing Man"), "Send should return false because channel is unknown")
  41. }
  42. func TestMConnectionReceive(t *testing.T) {
  43. assert, require := assert.New(t), require.New(t)
  44. server, client := net.Pipe()
  45. defer server.Close()
  46. defer client.Close()
  47. receivedCh := make(chan []byte)
  48. errorsCh := make(chan interface{})
  49. onReceive := func(chID byte, msgBytes []byte) {
  50. receivedCh <- msgBytes
  51. }
  52. onError := func(r interface{}) {
  53. errorsCh <- r
  54. }
  55. mconn1 := createMConnectionWithCallbacks(client, onReceive, onError)
  56. _, err := mconn1.Start()
  57. require.Nil(err)
  58. defer mconn1.Stop()
  59. mconn2 := createMConnection(server)
  60. _, err = mconn2.Start()
  61. require.Nil(err)
  62. defer mconn2.Stop()
  63. msg := "Cyclops"
  64. assert.True(mconn2.Send(0x01, msg))
  65. select {
  66. case receivedBytes := <-receivedCh:
  67. assert.Equal([]byte(msg), receivedBytes[2:]) // first 3 bytes are internal
  68. case err := <-errorsCh:
  69. t.Fatalf("Expected %s, got %+v", msg, err)
  70. case <-time.After(500 * time.Millisecond):
  71. t.Fatalf("Did not receive %s message in 500ms", msg)
  72. }
  73. }
  74. func TestMConnectionStatus(t *testing.T) {
  75. assert, require := assert.New(t), require.New(t)
  76. server, client := net.Pipe()
  77. defer server.Close()
  78. defer client.Close()
  79. mconn := createMConnection(client)
  80. _, err := mconn.Start()
  81. require.Nil(err)
  82. defer mconn.Stop()
  83. status := mconn.Status()
  84. assert.NotNil(status)
  85. assert.Zero(status.Channels[0].SendQueueSize)
  86. }
  87. func TestMConnectionStopsAndReturnsError(t *testing.T) {
  88. assert, require := assert.New(t), require.New(t)
  89. server, client := net.Pipe()
  90. defer server.Close()
  91. defer client.Close()
  92. receivedCh := make(chan []byte)
  93. errorsCh := make(chan interface{})
  94. onReceive := func(chID byte, msgBytes []byte) {
  95. receivedCh <- msgBytes
  96. }
  97. onError := func(r interface{}) {
  98. errorsCh <- r
  99. }
  100. mconn := createMConnectionWithCallbacks(client, onReceive, onError)
  101. _, err := mconn.Start()
  102. require.Nil(err)
  103. defer mconn.Stop()
  104. client.Close()
  105. select {
  106. case receivedBytes := <-receivedCh:
  107. t.Fatalf("Expected error, got %v", receivedBytes)
  108. case err := <-errorsCh:
  109. assert.NotNil(err)
  110. assert.False(mconn.IsRunning())
  111. case <-time.After(500 * time.Millisecond):
  112. t.Fatal("Did not receive error in 500ms")
  113. }
  114. }