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.

89 lines
2.4 KiB

  1. package mempool
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. func TestMempoolIDsBasic(t *testing.T) {
  8. ids := NewMempoolIDs()
  9. peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
  10. require.NoError(t, err)
  11. require.EqualValues(t, 0, ids.GetForPeer(peerID))
  12. ids.ReserveForPeer(peerID)
  13. require.EqualValues(t, 1, ids.GetForPeer(peerID))
  14. ids.Reclaim(peerID)
  15. require.EqualValues(t, 0, ids.GetForPeer(peerID))
  16. ids.ReserveForPeer(peerID)
  17. require.EqualValues(t, 1, ids.GetForPeer(peerID))
  18. }
  19. func TestMempoolIDsPeerDupReserve(t *testing.T) {
  20. ids := NewMempoolIDs()
  21. peerID, err := types.NewNodeID("0011223344556677889900112233445566778899")
  22. require.NoError(t, err)
  23. require.EqualValues(t, 0, ids.GetForPeer(peerID))
  24. ids.ReserveForPeer(peerID)
  25. require.EqualValues(t, 1, ids.GetForPeer(peerID))
  26. ids.ReserveForPeer(peerID)
  27. require.EqualValues(t, 1, ids.GetForPeer(peerID))
  28. }
  29. func TestMempoolIDs2Peers(t *testing.T) {
  30. ids := NewMempoolIDs()
  31. peer1ID, _ := types.NewNodeID("0011223344556677889900112233445566778899")
  32. require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
  33. ids.ReserveForPeer(peer1ID)
  34. require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
  35. ids.Reclaim(peer1ID)
  36. require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
  37. peer2ID, _ := types.NewNodeID("1011223344556677889900112233445566778899")
  38. ids.ReserveForPeer(peer2ID)
  39. require.EqualValues(t, 1, ids.GetForPeer(peer2ID))
  40. ids.ReserveForPeer(peer1ID)
  41. require.EqualValues(t, 2, ids.GetForPeer(peer1ID))
  42. }
  43. func TestMempoolIDsNextExistID(t *testing.T) {
  44. ids := NewMempoolIDs()
  45. peer1ID, _ := types.NewNodeID("0011223344556677889900112233445566778899")
  46. ids.ReserveForPeer(peer1ID)
  47. require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
  48. peer2ID, _ := types.NewNodeID("1011223344556677889900112233445566778899")
  49. ids.ReserveForPeer(peer2ID)
  50. require.EqualValues(t, 2, ids.GetForPeer(peer2ID))
  51. peer3ID, _ := types.NewNodeID("2011223344556677889900112233445566778899")
  52. ids.ReserveForPeer(peer3ID)
  53. require.EqualValues(t, 3, ids.GetForPeer(peer3ID))
  54. ids.Reclaim(peer1ID)
  55. require.EqualValues(t, 0, ids.GetForPeer(peer1ID))
  56. ids.Reclaim(peer3ID)
  57. require.EqualValues(t, 0, ids.GetForPeer(peer3ID))
  58. ids.ReserveForPeer(peer1ID)
  59. require.EqualValues(t, 1, ids.GetForPeer(peer1ID))
  60. ids.ReserveForPeer(peer3ID)
  61. require.EqualValues(t, 3, ids.GetForPeer(peer3ID))
  62. }