Changeset View
Changeset View
Standalone View
Standalone View
src/test/txrequest_tests.cpp
Show All 25 Lines | |||||
/** | /** | ||||
* Object that stores actions from multiple interleaved scenarios, and data | * Object that stores actions from multiple interleaved scenarios, and data | ||||
* shared across them. | * shared across them. | ||||
* | * | ||||
* The Scenario below is used to fill this. | * The Scenario below is used to fill this. | ||||
*/ | */ | ||||
struct Runner { | struct Runner { | ||||
/** The TxRequestTracker being tested. */ | /** The InvRequestTracker being tested. */ | ||||
TxRequestTracker txrequest; | InvRequestTracker txrequest; | ||||
/** List of actions to be executed (in order of increasing timestamp). */ | /** List of actions to be executed (in order of increasing timestamp). */ | ||||
std::vector<Action> actions; | std::vector<Action> actions; | ||||
/** Which node ids have been assigned already (to prevent reuse). */ | /** Which node ids have been assigned already (to prevent reuse). */ | ||||
std::set<NodeId> peerset; | std::set<NodeId> peerset; | ||||
/** Which txids have been assigned already (to prevent reuse). */ | /** Which txids have been assigned already (to prevent reuse). */ | ||||
Show All 12 Lines | std::chrono::microseconds RandomTime8s() { | ||||
return std::chrono::microseconds{1 + InsecureRandBits(23)}; | return std::chrono::microseconds{1 + InsecureRandBits(23)}; | ||||
} | } | ||||
std::chrono::microseconds RandomTime1y() { | std::chrono::microseconds RandomTime1y() { | ||||
return std::chrono::microseconds{1 + InsecureRandBits(45)}; | return std::chrono::microseconds{1 + InsecureRandBits(45)}; | ||||
} | } | ||||
/** | /** | ||||
* A proxy for a Runner that helps build a sequence of consecutive test actions | * A proxy for a Runner that helps build a sequence of consecutive test actions | ||||
* on a TxRequestTracker. | * on a InvRequestTracker. | ||||
* | * | ||||
* Each Scenario is a proxy through which actions for the (sequential) execution | * Each Scenario is a proxy through which actions for the (sequential) execution | ||||
* of various tests are added to a Runner. The actions from multiple scenarios | * of various tests are added to a Runner. The actions from multiple scenarios | ||||
* are then run concurrently, resulting in these tests being performed against a | * are then run concurrently, resulting in these tests being performed against a | ||||
* TxRequestTracker in parallel. Every test has its own unique txids and | * InvRequestTracker in parallel. Every test has its own unique txids and | ||||
* NodeIds which are not reused in other tests, and thus they should be | * NodeIds which are not reused in other tests, and thus they should be | ||||
* independent from each other. Running them in parallel however means that we | * independent from each other. Running them in parallel however means that we | ||||
* verify the behavior (w.r.t. one test's txids and NodeIds) even when the | * verify the behavior (w.r.t. one test's txids and NodeIds) even when the | ||||
* state of the data structure is more complicated due to the presence of other | * state of the data structure is more complicated due to the presence of other | ||||
* tests. | * tests. | ||||
*/ | */ | ||||
class Scenario { | class Scenario { | ||||
Runner &m_runner; | Runner &m_runner; | ||||
Show All 15 Lines | void AdvanceTime(std::chrono::microseconds amount) { | ||||
assert(amount.count() >= 0); | assert(amount.count() >= 0); | ||||
m_now += amount; | m_now += amount; | ||||
} | } | ||||
/** Schedule a ForgetTxId call at the Scheduler's current time. */ | /** Schedule a ForgetTxId call at the Scheduler's current time. */ | ||||
void ForgetTxId(const TxId &txid) { | void ForgetTxId(const TxId &txid) { | ||||
auto &runner = m_runner; | auto &runner = m_runner; | ||||
runner.actions.emplace_back(m_now, [=, &runner]() { | runner.actions.emplace_back(m_now, [=, &runner]() { | ||||
runner.txrequest.ForgetTxId(txid); | runner.txrequest.ForgetInvId(txid); | ||||
runner.txrequest.SanityCheck(); | runner.txrequest.SanityCheck(); | ||||
}); | }); | ||||
} | } | ||||
/** Schedule a ReceivedInv call at the Scheduler's current time. */ | /** Schedule a ReceivedInv call at the Scheduler's current time. */ | ||||
void ReceivedInv(NodeId peer, const TxId &txid, bool pref, | void ReceivedInv(NodeId peer, const TxId &txid, bool pref, | ||||
std::chrono::microseconds reqtime) { | std::chrono::microseconds reqtime) { | ||||
auto &runner = m_runner; | auto &runner = m_runner; | ||||
Show All 12 Lines | void DisconnectedPeer(NodeId peer) { | ||||
}); | }); | ||||
} | } | ||||
/** Schedule a RequestedTx call at the Scheduler's current time. */ | /** Schedule a RequestedTx call at the Scheduler's current time. */ | ||||
void RequestedTx(NodeId peer, const TxId &txid, | void RequestedTx(NodeId peer, const TxId &txid, | ||||
std::chrono::microseconds exptime) { | std::chrono::microseconds exptime) { | ||||
auto &runner = m_runner; | auto &runner = m_runner; | ||||
runner.actions.emplace_back(m_now, [=, &runner]() { | runner.actions.emplace_back(m_now, [=, &runner]() { | ||||
runner.txrequest.RequestedTx(peer, txid, exptime); | runner.txrequest.RequestedData(peer, txid, exptime); | ||||
runner.txrequest.SanityCheck(); | runner.txrequest.SanityCheck(); | ||||
}); | }); | ||||
} | } | ||||
/** Schedule a ReceivedResponse call at the Scheduler's current time. */ | /** Schedule a ReceivedResponse call at the Scheduler's current time. */ | ||||
void ReceivedResponse(NodeId peer, const TxId &txid) { | void ReceivedResponse(NodeId peer, const TxId &txid) { | ||||
auto &runner = m_runner; | auto &runner = m_runner; | ||||
runner.actions.emplace_back(m_now, [=, &runner]() { | runner.actions.emplace_back(m_now, [=, &runner]() { | ||||
runner.txrequest.ReceivedResponse(peer, txid); | runner.txrequest.ReceivedResponse(peer, txid); | ||||
runner.txrequest.SanityCheck(); | runner.txrequest.SanityCheck(); | ||||
}); | }); | ||||
} | } | ||||
/** | /** | ||||
* Schedule calls to verify the TxRequestTracker's state at the Scheduler's | * Schedule calls to verify the InvRequestTracker's state at the Scheduler's | ||||
* current time. | * current time. | ||||
* | * | ||||
* @param peer The peer whose state will be inspected. | * @param peer The peer whose state will be inspected. | ||||
* @param expected The expected return value for GetRequestable(peer) | * @param expected The expected return value for GetRequestable(peer) | ||||
* @param candidates The expected return value CountCandidates(peer) | * @param candidates The expected return value CountCandidates(peer) | ||||
* @param inflight The expected return value CountInFlight(peer) | * @param inflight The expected return value CountInFlight(peer) | ||||
* @param completed The expected return value of Count(peer), minus | * @param completed The expected return value of Count(peer), minus | ||||
* candidates and inflight. | * candidates and inflight. | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | public: | ||||
/** | /** | ||||
* Generate a random txid, whose priorities for certain peers are | * Generate a random txid, whose priorities for certain peers are | ||||
* constrained. | * constrained. | ||||
* | * | ||||
* For example, NewTxId({{p1,p2,p3},{p2,p4,p5}}) will generate a txid T | * For example, NewTxId({{p1,p2,p3},{p2,p4,p5}}) will generate a txid T | ||||
* such that both: | * such that both: | ||||
* - priority(p1,T) > priority(p2,T) > priority(p3,T) | * - priority(p1,T) > priority(p2,T) > priority(p3,T) | ||||
* - priority(p2,T) > priority(p4,T) > priority(p5,T) | * - priority(p2,T) > priority(p4,T) > priority(p5,T) | ||||
* where priority is the predicted internal TxRequestTracker's priority, | * where priority is the predicted internal InvRequestTracker's priority, | ||||
* assuming all announcements are within the same preferredness class. | * assuming all announcements are within the same preferredness class. | ||||
*/ | */ | ||||
TxId NewTxId(const std::vector<std::vector<NodeId>> &orders = {}) { | TxId NewTxId(const std::vector<std::vector<NodeId>> &orders = {}) { | ||||
TxId ret; | TxId ret; | ||||
bool ok; | bool ok; | ||||
do { | do { | ||||
ret = TxId(InsecureRand256()); | ret = TxId(InsecureRand256()); | ||||
ok = true; | ok = true; | ||||
▲ Show 20 Lines • Show All 532 Lines • Show Last 20 Lines |