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.

135 lines
3.3 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}}
  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. assert.False(mconn.CanSend(0x01))
  33. server.Read(make([]byte, len(msg)))
  34. assert.True(mconn.CanSend(0x01))
  35. msg = "Spider-Man"
  36. assert.True(mconn.TrySend(0x01, msg))
  37. server.Read(make([]byte, len(msg)))
  38. }
  39. func TestMConnectionReceive(t *testing.T) {
  40. assert, require := assert.New(t), require.New(t)
  41. server, client := net.Pipe()
  42. defer server.Close()
  43. defer client.Close()
  44. receivedCh := make(chan []byte)
  45. errorsCh := make(chan interface{})
  46. onReceive := func(chID byte, msgBytes []byte) {
  47. receivedCh <- msgBytes
  48. }
  49. onError := func(r interface{}) {
  50. errorsCh <- r
  51. }
  52. mconn1 := createMConnectionWithCallbacks(client, onReceive, onError)
  53. _, err := mconn1.Start()
  54. require.Nil(err)
  55. defer mconn1.Stop()
  56. mconn2 := createMConnection(server)
  57. _, err = mconn2.Start()
  58. require.Nil(err)
  59. defer mconn2.Stop()
  60. msg := "Cyclops"
  61. assert.True(mconn2.Send(0x01, msg))
  62. select {
  63. case receivedBytes := <-receivedCh:
  64. assert.Equal([]byte(msg), receivedBytes[2:]) // first 3 bytes are internal
  65. case err := <-errorsCh:
  66. t.Fatalf("Expected %s, got %+v", msg, err)
  67. case <-time.After(500 * time.Millisecond):
  68. t.Fatalf("Did not receive %s message in 500ms", msg)
  69. }
  70. }
  71. func TestMConnectionStatus(t *testing.T) {
  72. assert, require := assert.New(t), require.New(t)
  73. server, client := net.Pipe()
  74. defer server.Close()
  75. defer client.Close()
  76. mconn := createMConnection(client)
  77. _, err := mconn.Start()
  78. require.Nil(err)
  79. defer mconn.Stop()
  80. status := mconn.Status()
  81. assert.NotNil(status)
  82. assert.Zero(status.Channels[0].SendQueueSize)
  83. }
  84. func TestMConnectionStopsAndReturnsError(t *testing.T) {
  85. assert, require := assert.New(t), require.New(t)
  86. server, client := net.Pipe()
  87. defer server.Close()
  88. defer client.Close()
  89. receivedCh := make(chan []byte)
  90. errorsCh := make(chan interface{})
  91. onReceive := func(chID byte, msgBytes []byte) {
  92. receivedCh <- msgBytes
  93. }
  94. onError := func(r interface{}) {
  95. errorsCh <- r
  96. }
  97. mconn := createMConnectionWithCallbacks(client, onReceive, onError)
  98. _, err := mconn.Start()
  99. require.Nil(err)
  100. defer mconn.Stop()
  101. client.Close()
  102. select {
  103. case receivedBytes := <-receivedCh:
  104. t.Fatalf("Expected error, got %v", receivedBytes)
  105. case err := <-errorsCh:
  106. assert.NotNil(err)
  107. assert.False(mconn.IsRunning())
  108. case <-time.After(500 * time.Millisecond):
  109. t.Fatal("Did not receive error in 500ms")
  110. }
  111. }