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.

121 lines
3.0 KiB

  1. package p2p_test
  2. import (
  3. "context"
  4. "io"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/libs/log"
  8. "github.com/tendermint/tendermint/p2p"
  9. )
  10. func TestMemoryTransport(t *testing.T) {
  11. ctx := context.Background()
  12. network := p2p.NewMemoryNetwork(log.TestingLogger())
  13. a := network.GenerateTransport()
  14. b := network.GenerateTransport()
  15. c := network.GenerateTransport()
  16. // Dialing a missing endpoint should fail.
  17. _, err := a.Dial(ctx, p2p.Endpoint{
  18. Protocol: p2p.MemoryProtocol,
  19. PeerID: p2p.NodeID("foo"),
  20. Path: "foo",
  21. })
  22. require.Error(t, err)
  23. // Dialing and accepting a→b and a→c should work.
  24. aToB, bToA, err := a.DialAccept(ctx, b)
  25. require.NoError(t, err)
  26. defer aToB.Close()
  27. defer bToA.Close()
  28. aToC, cToA, err := a.DialAccept(ctx, c)
  29. require.NoError(t, err)
  30. defer aToC.Close()
  31. defer cToA.Close()
  32. // Send and receive a message both ways a→b and b→a
  33. sent, err := aToB.SendMessage(1, []byte("hi!"))
  34. require.NoError(t, err)
  35. require.True(t, sent)
  36. ch, msg, err := bToA.ReceiveMessage()
  37. require.NoError(t, err)
  38. require.EqualValues(t, 1, ch)
  39. require.EqualValues(t, "hi!", msg)
  40. sent, err = bToA.SendMessage(1, []byte("hello"))
  41. require.NoError(t, err)
  42. require.True(t, sent)
  43. ch, msg, err = aToB.ReceiveMessage()
  44. require.NoError(t, err)
  45. require.EqualValues(t, 1, ch)
  46. require.EqualValues(t, "hello", msg)
  47. // Send and receive a message both ways a→c and c→a
  48. sent, err = aToC.SendMessage(1, []byte("foo"))
  49. require.NoError(t, err)
  50. require.True(t, sent)
  51. ch, msg, err = cToA.ReceiveMessage()
  52. require.NoError(t, err)
  53. require.EqualValues(t, 1, ch)
  54. require.EqualValues(t, "foo", msg)
  55. sent, err = cToA.SendMessage(1, []byte("bar"))
  56. require.NoError(t, err)
  57. require.True(t, sent)
  58. ch, msg, err = aToC.ReceiveMessage()
  59. require.NoError(t, err)
  60. require.EqualValues(t, 1, ch)
  61. require.EqualValues(t, "bar", msg)
  62. // If we close aToB, sending and receiving on either end will error.
  63. err = aToB.Close()
  64. require.NoError(t, err)
  65. _, err = aToB.SendMessage(1, []byte("foo"))
  66. require.Equal(t, io.EOF, err)
  67. _, _, err = aToB.ReceiveMessage()
  68. require.Equal(t, io.EOF, err)
  69. _, err = bToA.SendMessage(1, []byte("foo"))
  70. require.Equal(t, io.EOF, err)
  71. _, _, err = bToA.ReceiveMessage()
  72. require.Equal(t, io.EOF, err)
  73. // We can still send aToC.
  74. sent, err = aToC.SendMessage(1, []byte("foo"))
  75. require.NoError(t, err)
  76. require.True(t, sent)
  77. ch, msg, err = cToA.ReceiveMessage()
  78. require.NoError(t, err)
  79. require.EqualValues(t, 1, ch)
  80. require.EqualValues(t, "foo", msg)
  81. // If we close the c transport, it will no longer accept connections,
  82. // but we can still use the open connection.
  83. endpoint := c.Endpoints()[0]
  84. err = c.Close()
  85. require.NoError(t, err)
  86. require.Empty(t, c.Endpoints())
  87. _, err = a.Dial(ctx, endpoint)
  88. require.Error(t, err)
  89. sent, err = aToC.SendMessage(1, []byte("bar"))
  90. require.NoError(t, err)
  91. require.True(t, sent)
  92. ch, msg, err = cToA.ReceiveMessage()
  93. require.NoError(t, err)
  94. require.EqualValues(t, 1, ch)
  95. require.EqualValues(t, "bar", msg)
  96. }