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.

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