PIVX Core  5.6.99
P2P Digital Currency
netfulfilledman_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php.
4 
5 #include "netbase.h"
6 #include "test/test_pivx.h"
8 #include "utiltime.h"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 BOOST_FIXTURE_TEST_SUITE(netfulfilledman_tests, RegTestingSetup)
13 
14 BOOST_AUTO_TEST_CASE(netfulfilledman_simple_add_and_expire)
15 {
16  int64_t now = GetTime();
17  SetMockTime(now);
18 
19  CNetFulfilledRequestManager fulfilledMan(DEFAULT_ITEMS_FILTER_SIZE);
20  CService service = LookupNumeric("1.1.1.1", 9999);
21  std::string request = "request";
22  BOOST_ASSERT(!fulfilledMan.HasFulfilledRequest(service, request));
23 
24  // Add request
25  fulfilledMan.AddFulfilledRequest(service, request);
26  // Verify that the request is there
27  BOOST_ASSERT(fulfilledMan.HasFulfilledRequest(service, request));
28 
29  // Advance mock time to surpass FulfilledRequestExpireTime
30  SetMockTime(GetMockTime() + 60 * 60 + 1);
31 
32  // Verify that the request exists and expired now
33  BOOST_CHECK(fulfilledMan.Size() == 1);
34  BOOST_CHECK(!fulfilledMan.HasFulfilledRequest(service, request));
35 
36  // Verify request removal
37  fulfilledMan.CheckAndRemove();
38  BOOST_CHECK(fulfilledMan.Size() == 0);
39 
40  // Items filter, insertion and lookup.
42  fulfilledMan.AddItemRequest(service, item);
43  BOOST_CHECK(fulfilledMan.HasItemRequest(service, item));
44 
45  CService service2 = LookupNumeric("1.1.1.2", 9999);
46  BOOST_CHECK(!fulfilledMan.HasItemRequest(service2, item));
47 
48  // Advance mock time to surpass the expiration time
49  SetMockTime(GetMockTime() + DEFAULT_ITEMS_FILTER_CLEANUP + 1);
50  fulfilledMan.CheckAndRemove();
51  BOOST_CHECK(!fulfilledMan.HasItemRequest(service, item));
52 
53  // Now test filling up the filter
54  fulfilledMan.AddItemRequest(service, item);
55  for (int i = 0; i < 300; i++) {
57  fulfilledMan.AddItemRequest(service, element);
58  BOOST_CHECK(fulfilledMan.HasItemRequest(service, element));
59  }
60 
61  fulfilledMan.CheckAndRemove();
62  BOOST_CHECK(!fulfilledMan.HasItemRequest(service, item));
63 }
64 
void AddFulfilledRequest(const CService &addr, const std::string &strRequest)
void AddItemRequest(const CService &addr, const uint256 &itemHash)
bool HasFulfilledRequest(const CService &addr, const std::string &strRequest) const
bool HasItemRequest(const CService &addr, const uint256 &itemHash) const
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:621
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
CService LookupNumeric(const std::string &name, int portDefault)
Definition: netbase.cpp:209
BOOST_AUTO_TEST_CASE(netfulfilledman_simple_add_and_expire)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
FastRandomContext g_insecure_rand_ctx
Definition: test_pivx.cpp:35
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: utiltime.cpp:51
int64_t GetMockTime()
For testing.
Definition: utiltime.cpp:56
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: utiltime.cpp:27