Browse Source

Merge pull request #2091 from gluk256/v_test_refactor

fixed peer test
cl-refactor
Gav Wood 10 years ago
parent
commit
796fb5de2c
  1. 9
      libdevcore/Log.h
  2. 6
      test/TestHelper.cpp
  3. 10
      test/libp2p/capability.cpp
  4. 42
      test/libp2p/peer.cpp
  5. 8
      test/libwhisper/whisperMessage.cpp

9
libdevcore/Log.h

@ -59,6 +59,15 @@ extern std::function<void(std::string const&, char const*)> g_logPost;
/// or equal to the currently output verbosity (g_logVerbosity).
extern std::map<std::type_info const*, bool> 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<dev::ThreadContext, bool> __eth_thread_context(name, true); p.second; p.second = false)
class ThreadContext

6
test/TestHelper.cpp

@ -581,8 +581,7 @@ void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> 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;
@ -607,14 +606,11 @@ void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> 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<void(json_spirit::mValue&, bool)> doTests)

10
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";

42
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<Host*> 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<unsigned>() == 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()

8
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;

Loading…
Cancel
Save