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.

154 lines
4.4 KiB

  1. package p2ptest
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/gogo/protobuf/proto"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/internal/p2p"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. // RequireEmpty requires that the given channel is empty.
  12. func RequireEmpty(t *testing.T, channels ...*p2p.Channel) {
  13. for _, channel := range channels {
  14. select {
  15. case e := <-channel.In:
  16. require.Fail(t, "unexpected message", "channel %v should be empty, got %v", channel.ID, e)
  17. case <-time.After(10 * time.Millisecond):
  18. }
  19. }
  20. }
  21. // RequireReceive requires that the given envelope is received on the channel.
  22. func RequireReceive(t *testing.T, channel *p2p.Channel, expect p2p.Envelope) {
  23. t.Helper()
  24. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  25. defer timer.Stop()
  26. select {
  27. case e := <-channel.In:
  28. require.Equal(t, expect, e)
  29. case <-timer.C:
  30. require.Fail(t, "timed out waiting for message", "%v on channel %v", expect, channel.ID)
  31. }
  32. }
  33. // RequireReceiveUnordered requires that the given envelopes are all received on
  34. // the channel, ignoring order.
  35. func RequireReceiveUnordered(t *testing.T, channel *p2p.Channel, expect []p2p.Envelope) {
  36. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  37. defer timer.Stop()
  38. actual := []p2p.Envelope{}
  39. for {
  40. select {
  41. case e := <-channel.In:
  42. actual = append(actual, e)
  43. if len(actual) == len(expect) {
  44. require.ElementsMatch(t, expect, actual)
  45. return
  46. }
  47. case <-timer.C:
  48. require.ElementsMatch(t, expect, actual)
  49. return
  50. }
  51. }
  52. }
  53. // RequireSend requires that the given envelope is sent on the channel.
  54. func RequireSend(t *testing.T, channel *p2p.Channel, envelope p2p.Envelope) {
  55. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  56. defer timer.Stop()
  57. select {
  58. case channel.Out <- envelope:
  59. case <-timer.C:
  60. require.Fail(t, "timed out sending message", "%v on channel %v", envelope, channel.ID)
  61. }
  62. }
  63. // RequireSendReceive requires that a given Protobuf message is sent to the
  64. // given peer, and then that the given response is received back.
  65. func RequireSendReceive(
  66. t *testing.T,
  67. channel *p2p.Channel,
  68. peerID types.NodeID,
  69. send proto.Message,
  70. receive proto.Message,
  71. ) {
  72. RequireSend(t, channel, p2p.Envelope{To: peerID, Message: send})
  73. RequireReceive(t, channel, p2p.Envelope{From: peerID, Message: send})
  74. }
  75. // RequireNoUpdates requires that a PeerUpdates subscription is empty.
  76. func RequireNoUpdates(ctx context.Context, t *testing.T, peerUpdates *p2p.PeerUpdates) {
  77. t.Helper()
  78. select {
  79. case update := <-peerUpdates.Updates():
  80. if ctx.Err() == nil {
  81. require.Fail(t, "unexpected peer updates", "got %v", update)
  82. }
  83. case <-ctx.Done():
  84. default:
  85. }
  86. }
  87. // RequireError requires that the given peer error is submitted for a peer.
  88. func RequireError(t *testing.T, channel *p2p.Channel, peerError p2p.PeerError) {
  89. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  90. defer timer.Stop()
  91. select {
  92. case channel.Error <- peerError:
  93. case <-timer.C:
  94. require.Fail(t, "timed out reporting error", "%v on %v", peerError, channel.ID)
  95. }
  96. }
  97. // RequireUpdate requires that a PeerUpdates subscription yields the given update.
  98. func RequireUpdate(t *testing.T, peerUpdates *p2p.PeerUpdates, expect p2p.PeerUpdate) {
  99. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  100. defer timer.Stop()
  101. select {
  102. case update := <-peerUpdates.Updates():
  103. require.Equal(t, expect, update, "peer update did not match")
  104. case <-peerUpdates.Done():
  105. require.Fail(t, "peer updates subscription is closed")
  106. case <-timer.C:
  107. require.Fail(t, "timed out waiting for peer update", "expected %v", expect)
  108. }
  109. }
  110. // RequireUpdates requires that a PeerUpdates subscription yields the given updates
  111. // in the given order.
  112. func RequireUpdates(t *testing.T, peerUpdates *p2p.PeerUpdates, expect []p2p.PeerUpdate) {
  113. timer := time.NewTimer(time.Second) // not time.After due to goroutine leaks
  114. defer timer.Stop()
  115. actual := []p2p.PeerUpdate{}
  116. for {
  117. select {
  118. case update := <-peerUpdates.Updates():
  119. actual = append(actual, update)
  120. if len(actual) == len(expect) {
  121. require.Equal(t, expect, actual)
  122. return
  123. }
  124. case <-peerUpdates.Done():
  125. require.Fail(t, "peer updates subscription is closed")
  126. case <-timer.C:
  127. require.Equal(t, expect, actual, "did not receive expected peer updates")
  128. return
  129. }
  130. }
  131. }