Skip to content

Commit fc826a5

Browse files
committed
Refactored app synchonization
1 parent 2d8e472 commit fc826a5

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

tests/app/main.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ namespace bpo = boost::program_options;
5353
namespace fc {
5454
extern std::unordered_map<std::string, logger> &get_logger_map();
5555
extern std::unordered_map<std::string, appender::ptr> &get_appender_map();
56+
57+
/** Waits for F() to return true before max_duration has passed.
58+
*/
59+
template<typename Functor>
60+
static void wait_for( const fc::microseconds max_duration, const Functor&& f )
61+
{
62+
const auto start = fc::time_point::now();
63+
while( !f() && fc::time_point::now() < start + max_duration )
64+
fc::usleep(fc::milliseconds(100));
65+
BOOST_REQUIRE( f() );
66+
}
5667
}
5768

5869
BOOST_AUTO_TEST_CASE(load_configuration_options_test_config_logging_files_created)
@@ -234,7 +245,10 @@ BOOST_AUTO_TEST_CASE( two_node_network )
234245
app1.initialize(app_dir.path(), cfg);
235246
BOOST_TEST_MESSAGE( "Starting app1 and waiting 500 ms" );
236247
app1.startup();
237-
fc::usleep(fc::milliseconds(500));
248+
fc::wait_for( fc::seconds(10), [&app1] () {
249+
const auto status = app1.p2p_node()->network_get_info();
250+
return status["listening_on"].as<fc::ip::endpoint>( 5 ).port() == 3939;
251+
});
238252

239253
BOOST_TEST_MESSAGE( "Creating and initializing app2" );
240254

@@ -254,11 +268,7 @@ BOOST_AUTO_TEST_CASE( two_node_network )
254268
BOOST_TEST_MESSAGE( "Starting app2 and waiting for connection" );
255269
app2.startup();
256270

257-
const auto start = fc::time_point::now();
258-
do
259-
{
260-
fc::usleep(fc::milliseconds(200));
261-
} while( app1.p2p_node()->get_connection_count() < 1 && fc::time_point::now() < start + fc::seconds(5) );
271+
fc::wait_for( fc::seconds(10), [&app1] () { return app1.p2p_node()->get_connection_count() > 0; } );
262272

263273
BOOST_REQUIRE_EQUAL(app1.p2p_node()->get_connection_count(), 1u);
264274
BOOST_CHECK_EQUAL(std::string(app1.p2p_node()->get_connected_peers().front().host.get_address()), "127.0.0.1");

0 commit comments

Comments
 (0)