Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6853 from EOSIO/stable-priorty-queue-dev
Browse files Browse the repository at this point in the history
Stable application execution priority queue
  • Loading branch information
heifner committed Mar 2, 2019
2 parents abd7b1a + 0a706f2 commit a70edad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libraries/appbase
2 changes: 1 addition & 1 deletion unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/contracts.hpp.in ${CMAKE_CURRENT_BINA
### BUILD UNIT TEST EXECUTABLE ###
file(GLOB UNIT_TESTS "*.cpp") # find all unit test suites
add_executable( unit_test ${UNIT_TESTS}) # build unit tests as one executable
target_link_libraries( unit_test eosio_chain chainbase eosio_testing fc ${PLATFORM_SPECIFIC_LIBS} )
target_link_libraries( unit_test eosio_chain chainbase eosio_testing fc appbase ${PLATFORM_SPECIFIC_LIBS} )
target_compile_options(unit_test PUBLIC -DDISABLE_EOSLIB_SERIALIZE)
target_include_directories( unit_test PUBLIC
${CMAKE_SOURCE_DIR}/libraries/testing/include
Expand Down
54 changes: 54 additions & 0 deletions unittests/misc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <eosio/testing/tester.hpp>

#include <fc/io/json.hpp>
#include <appbase/execution_priority_queue.hpp>

#include <boost/asio/thread_pool.hpp>
#include <boost/test/unit_test.hpp>
Expand Down Expand Up @@ -1056,6 +1057,59 @@ BOOST_AUTO_TEST_CASE(reflector_init_test) {
} FC_LOG_AND_RETHROW()
}

// Verify appbase::execution_priority_queue uses a stable priority queue so that jobs are executed
// in order, FIFO, as submitted.
BOOST_AUTO_TEST_CASE(stable_priority_queue_test) {
try {
using namespace std::chrono_literals;

appbase::execution_priority_queue pri_queue;
auto io_serv = std::make_shared<boost::asio::io_service>();
auto work_ptr = std::make_unique<boost::asio::io_service::work>(*io_serv);
std::atomic<int> posted{0};

std::thread t( [io_serv, &pri_queue, &posted]() {
while( posted < 100 && io_serv->run_one() ) {
++posted;
}
bool more = true;
while( more || io_serv->run_one() ) {
while( io_serv->poll_one() ) {}
// execute the highest priority item
more = pri_queue.execute_highest();
}
} );
std::atomic<int> ran{0};
std::mutex mx;
std::vector<int> results;
for( int i = 0; i < 50; ++i ) {
boost::asio::post(*io_serv, pri_queue.wrap(appbase::priority::low, [io_serv, &mx, &ran, &results, i](){
std::lock_guard<std::mutex> g(mx);
results.push_back( 50 + i );
++ran;
}));
boost::asio::post(*io_serv, pri_queue.wrap(appbase::priority::high, [io_serv, &mx, &ran, &results, i](){
std::lock_guard<std::mutex> g(mx);
results.push_back( i );
++ran;
}));
}

while( ran < 100 ) std::this_thread::sleep_for( 5us );

work_ptr.reset();
io_serv->stop();
t.join();

std::lock_guard<std::mutex> g(mx);
BOOST_CHECK_EQUAL( 100, results.size() );
for( int i = 0; i < 100; ++i ) {
BOOST_CHECK_EQUAL( i, results.at( i ) );
}

} FC_LOG_AND_RETHROW()
}


BOOST_AUTO_TEST_SUITE_END()

Expand Down

0 comments on commit a70edad

Please sign in to comment.