diff --git a/libdevcore/Log.h b/libdevcore/Log.h index 20ad9fd20..a4011fc0e 100644 --- a/libdevcore/Log.h +++ b/libdevcore/Log.h @@ -59,6 +59,15 @@ extern std::function g_logPost; /// or equal to the currently output verbosity (g_logVerbosity). extern std::map g_logOverride; +/// Temporary changes system's verbosity for specific function. Restores the old verbosity when function returns. +/// Not thread-safe, use with caution! +struct VerbosityHolder +{ + VerbosityHolder(int _temporaryValue) : oldLogVerbosity(g_logVerbosity) { g_logVerbosity = _temporaryValue; } + ~VerbosityHolder() { g_logVerbosity = oldLogVerbosity; } + int oldLogVerbosity; +}; + #define ETH_THREAD_CONTEXT(name) for (std::pair __eth_thread_context(name, true); p.second; p.second = false) class ThreadContext diff --git a/test/TestHelper.cpp b/test/TestHelper.cpp index 793e05652..2c6384942 100644 --- a/test/TestHelper.cpp +++ b/test/TestHelper.cpp @@ -567,8 +567,7 @@ void userDefinedTest(std::function doTests) auto& filename = Options::get().singleTestFile; auto& testname = Options::get().singleTestName; - int currentVerbosity = g_logVerbosity; - g_logVerbosity = 12; + VerbosityHolder sentinel(12); try { cnote << "Testing user defined test: " << filename; @@ -593,14 +592,11 @@ void userDefinedTest(std::function doTests) catch (Exception const& _e) { BOOST_ERROR("Failed Test with Exception: " << diagnostic_information(_e)); - g_logVerbosity = currentVerbosity; } catch (std::exception const& _e) { BOOST_ERROR("Failed Test with Exception: " << _e.what()); - g_logVerbosity = currentVerbosity; } - g_logVerbosity = currentVerbosity; } void executeTests(const string& _name, const string& _testPathAppendix, const boost::filesystem::path _pathToFiller, std::function doTests) diff --git a/test/libp2p/capability.cpp b/test/libp2p/capability.cpp index 0a8542ee0..fa8592bdd 100644 --- a/test/libp2p/capability.cpp +++ b/test/libp2p/capability.cpp @@ -38,14 +38,6 @@ struct P2PFixture ~P2PFixture() { dev::p2p::NodeIPEndpoint::test_allowLocal = false; } }; -struct VerbosityHolder -{ - VerbosityHolder(): oldLogVerbosity(g_logVerbosity) { g_logVerbosity = 10; } - ~VerbosityHolder() { g_logVerbosity = oldLogVerbosity; } - - int oldLogVerbosity; -}; - class TestCapability: public Capability { public: @@ -106,7 +98,7 @@ BOOST_FIXTURE_TEST_SUITE(p2pCapability, P2PFixture) BOOST_AUTO_TEST_CASE(capability) { - VerbosityHolder verbosityHolder; + VerbosityHolder verbosityHolder(10); cnote << "Testing Capability..."; const char* const localhost = "127.0.0.1"; diff --git a/test/libp2p/peer.cpp b/test/libp2p/peer.cpp index a5a4a64dc..bcb6d4085 100644 --- a/test/libp2p/peer.cpp +++ b/test/libp2p/peer.cpp @@ -38,8 +38,7 @@ BOOST_FIXTURE_TEST_SUITE(p2p, P2PFixture) BOOST_AUTO_TEST_CASE(host) { - auto oldLogVerbosity = g_logVerbosity; - g_logVerbosity = 10; + VerbosityHolder sentinel(10); NetworkPreferences host1prefs("127.0.0.1", 30301, false); NetworkPreferences host2prefs("127.0.0.1", 30302, false); @@ -61,8 +60,6 @@ BOOST_AUTO_TEST_CASE(host) auto host2peerCount = host2.peerCount(); BOOST_REQUIRE_EQUAL(host1peerCount, 1); BOOST_REQUIRE_EQUAL(host2peerCount, 1); - - g_logVerbosity = oldLogVerbosity; } BOOST_AUTO_TEST_CASE(networkConfig) @@ -76,40 +73,50 @@ BOOST_AUTO_TEST_CASE(networkConfig) BOOST_AUTO_TEST_CASE(saveNodes) { + VerbosityHolder reduceVerbosity(2); + std::list hosts; - for (auto i:{0,1,2,3,4,5}) + unsigned const c_step = 10; + unsigned const c_nodes = 6; + unsigned const c_peers = c_nodes - 1; + + for (unsigned i = 0; i < c_nodes; ++i) { Host* h = new Host("Test", NetworkPreferences("127.0.0.1", 30300 + i, false)); h->setIdealPeerCount(10); // starting host is required so listenport is available h->start(); while (!h->haveNetwork()) - this_thread::sleep_for(chrono::milliseconds(2)); + this_thread::sleep_for(chrono::milliseconds(c_step)); hosts.push_back(h); } Host& host = *hosts.front(); for (auto const& h: hosts) host.addNode(h->id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), h->listenPort(), h->listenPort())); - + + for (unsigned i = 0; i < c_peers * 1000 && host.peerCount() < c_peers; i += c_step) + this_thread::sleep_for(chrono::milliseconds(c_step)); + Host& host2 = *hosts.back(); for (auto const& h: hosts) host2.addNode(h->id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), h->listenPort(), h->listenPort())); - this_thread::sleep_for(chrono::milliseconds(2000)); + for (unsigned i = 0; i < c_peers * 1000 && host2.peerCount() < c_peers; i += c_step) + this_thread::sleep_for(chrono::milliseconds(c_step)); + + BOOST_CHECK_EQUAL(host.peerCount(), c_peers); + BOOST_CHECK_EQUAL(host2.peerCount(), c_peers); + bytes firstHostNetwork(host.saveNetwork()); - bytes secondHostNetwork(host.saveNetwork()); - - BOOST_REQUIRE_EQUAL(sha3(firstHostNetwork), sha3(secondHostNetwork)); - - BOOST_CHECK_EQUAL(host.peerCount(), 5); - BOOST_CHECK_EQUAL(host2.peerCount(), 5); + bytes secondHostNetwork(host.saveNetwork()); + BOOST_REQUIRE_EQUAL(sha3(firstHostNetwork), sha3(secondHostNetwork)); RLP r(firstHostNetwork); BOOST_REQUIRE(r.itemCount() == 3); BOOST_REQUIRE(r[0].toInt() == dev::p2p::c_protocolVersion); BOOST_REQUIRE_EQUAL(r[1].toBytes().size(), 32); // secret - BOOST_REQUIRE(r[2].itemCount() >= 5); + BOOST_REQUIRE(r[2].itemCount() >= c_nodes); for (auto i: r[2]) { @@ -127,8 +134,7 @@ BOOST_FIXTURE_TEST_SUITE(p2pPeer, P2PFixture) BOOST_AUTO_TEST_CASE(requirePeer) { - auto oldLogVerbosity = g_logVerbosity; - g_logVerbosity = 10; + VerbosityHolder reduceVerbosity(10); const char* const localhost = "127.0.0.1"; NetworkPreferences prefs1(localhost, 30301, false); @@ -172,8 +178,6 @@ BOOST_AUTO_TEST_CASE(requirePeer) host2peerCount = host2.peerCount(); BOOST_REQUIRE_EQUAL(host1peerCount, 1); BOOST_REQUIRE_EQUAL(host2peerCount, 1); - - g_logVerbosity = oldLogVerbosity; } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libwhisper/whisperMessage.cpp b/test/libwhisper/whisperMessage.cpp index b3cda581b..56a763622 100644 --- a/test/libwhisper/whisperMessage.cpp +++ b/test/libwhisper/whisperMessage.cpp @@ -26,14 +26,6 @@ using namespace std; using namespace dev; using namespace dev::shh; -struct VerbosityHolder -{ - VerbosityHolder(int _temporaryValue) : oldLogVerbosity(g_logVerbosity) { g_logVerbosity = _temporaryValue; } - ~VerbosityHolder() { g_logVerbosity = oldLogVerbosity; } - - int oldLogVerbosity; -}; - Topics createRandomTopics(unsigned int i) { Topics ret;