Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
// Copyright (c) 2010 The Bitcoin developers | // Copyright (c) 2010 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "avalanche.h" | #include "avalanche.h" | ||||
#include "scheduler.h" | |||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
BOOST_FIXTURE_TEST_SUITE(avalanche_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(avalanche_tests, BasicTestingSetup) | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | #define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | ||||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(block_register) { | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
// Adding the block twice does nothing. | // Adding the block twice does nothing. | ||||
BOOST_CHECK(!p.addBlockToReconcile(&index)); | BOOST_CHECK(!p.addBlockToReconcile(&index)); | ||||
BOOST_CHECK(!p.isAccepted(&index)); | BOOST_CHECK(!p.isAccepted(&index)); | ||||
BOOST_CHECK(p.hasFinalized(&index)); | BOOST_CHECK(p.hasFinalized(&index)); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(event_loop) { | |||||
AvalancheProcessor p; | |||||
CScheduler scheduler; | |||||
// Starting the event loop. | |||||
BOOST_CHECK(p.startEventLoop(scheduler)); | |||||
// There is one task planned in the next hour (our event loop). | |||||
boost::chrono::system_clock::time_point start, stop; | |||||
BOOST_CHECK_EQUAL(scheduler.getQueueInfo(start, stop), 1); | |||||
// Starting twice doesn't start it twice. | |||||
BOOST_CHECK(!p.startEventLoop(scheduler)); | |||||
// Start the scheduler thread. | |||||
std::thread schedulerThread( | |||||
std::bind(&CScheduler::serviceQueue, &scheduler)); | |||||
// Stop event loop. | |||||
BOOST_CHECK(p.stopEventLoop()); | |||||
// We don't have any task scheduled anymore. | |||||
BOOST_CHECK_EQUAL(scheduler.getQueueInfo(start, stop), 0); | |||||
// Can't stop the event loop twice. | |||||
BOOST_CHECK(!p.stopEventLoop()); | |||||
// Wait for the scheduler to stop. | |||||
scheduler.stop(true); | |||||
schedulerThread.join(); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(destructor) { | |||||
CScheduler scheduler; | |||||
boost::chrono::system_clock::time_point start, stop; | |||||
// Start the scheduler thread. | |||||
std::thread schedulerThread( | |||||
std::bind(&CScheduler::serviceQueue, &scheduler)); | |||||
{ | |||||
AvalancheProcessor p; | |||||
BOOST_CHECK(p.startEventLoop(scheduler)); | |||||
BOOST_CHECK_EQUAL(scheduler.getQueueInfo(start, stop), 1); | |||||
} | |||||
// Now that avalanche is destroyed, there is scheduled task anymore. | |||||
BOOST_CHECK_EQUAL(scheduler.getQueueInfo(start, stop), 0); | |||||
// Wait for the scheduler to stop. | |||||
scheduler.stop(true); | |||||
schedulerThread.join(); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |