Browse Source

Merge pull request #1581 from ethereum/whisper

update whisper tests
cl-refactor
Gav Wood 10 years ago
parent
commit
80d93a7a9d
  1. 99
      test/libwhisper/whisperTopic.cpp

99
test/libwhisper/whisperTopic.cpp

@ -30,40 +30,40 @@ using namespace dev;
using namespace dev::p2p;
using namespace dev::shh;
BOOST_AUTO_TEST_SUITE(whisper)
struct P2PFixture
{
P2PFixture() { dev::p2p::NodeIPEndpoint::test_allowLocal = true; }
~P2PFixture() { dev::p2p::NodeIPEndpoint::test_allowLocal = false; }
};
BOOST_FIXTURE_TEST_SUITE(whisper, P2PFixture)
#if ALEX_HASH_FIXED_NETWORKING
BOOST_AUTO_TEST_CASE(topic)
{
cnote << "Testing Whisper...";
auto oldLogVerbosity = g_logVerbosity;
g_logVerbosity = 0;
Host host1("Test", NetworkPreferences(30303, "127.0.0.1", false, true));
Host host1("Test", NetworkPreferences("127.0.0.1", 30303, false));
host1.setIdealPeerCount(1);
auto whost1 = host1.registerCapability(new WhisperHost());
host1.start();
while (!host1.isStarted())
this_thread::sleep_for(chrono::milliseconds(2));
bool started = false;
bool host1Ready = false;
unsigned result = 0;
std::thread listener([&]()
{
setThreadName("other");
started = true;
/// Only interested in odd packets
auto w = whost1->installWatch(BuildTopicMask("odd"));
started = true;
host1Ready = true;
set<unsigned> received;
for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout)
{
for (auto i: whost1->checkWatch(w))
{
Message msg = whost1->envelope(i).open(whost1->fullTopic(w));
Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
last = RLP(msg.payload()).toInt<unsigned>();
if (received.count(last))
continue;
@ -76,21 +76,21 @@ BOOST_AUTO_TEST_CASE(topic)
});
Host host2("Test", NetworkPreferences(30300, "127.0.0.1", false, true));
Host host2("Test", NetworkPreferences("127.0.0.1", 30300, false));
host1.setIdealPeerCount(1);
auto whost2 = host2.registerCapability(new WhisperHost());
host2.start();
while (!host2.isStarted())
this_thread::sleep_for(chrono::milliseconds(2));
this_thread::sleep_for(chrono::milliseconds(100));
host2.addNode(host1.id(), "127.0.0.1", 30303, 30303);
this_thread::sleep_for(chrono::milliseconds(500));
while (!started)
this_thread::sleep_for(chrono::milliseconds(2));
while (!host1.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(5));
host2.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30303, 30303));
// wait for nodes to connect
this_thread::sleep_for(chrono::milliseconds(1000));
while (!host1Ready)
this_thread::sleep_for(chrono::milliseconds(10));
KeyPair us = KeyPair::create();
for (int i = 0; i < 10; ++i)
{
@ -111,11 +111,11 @@ BOOST_AUTO_TEST_CASE(forwarding)
g_logVerbosity = 0;
// Host must be configured not to share peers.
Host host1("Listner", NetworkPreferences(30303, "", false, true));
host1.setIdealPeerCount(0);
Host host1("Listner", NetworkPreferences("127.0.0.1", 30303, false));
host1.setIdealPeerCount(1);
auto whost1 = host1.registerCapability(new WhisperHost());
host1.start();
while (!host1.isStarted())
while (!host1.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
unsigned result = 0;
@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
{
for (auto i: whost1->checkWatch(w))
{
Message msg = whost1->envelope(i).open(whost1->fullTopic(w));
Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
unsigned last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
result = last;
@ -146,11 +146,11 @@ BOOST_AUTO_TEST_CASE(forwarding)
// Host must be configured not to share peers.
Host host2("Forwarder", NetworkPreferences(30305, "", false, true));
Host host2("Forwarder", NetworkPreferences("127.0.0.1", 30305, false));
host2.setIdealPeerCount(1);
auto whost2 = host2.registerCapability(new WhisperHost());
host2.start();
while (!host2.isStarted())
while (!host2.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
Public fwderid;
@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
this_thread::sleep_for(chrono::milliseconds(50));
this_thread::sleep_for(chrono::milliseconds(500));
host2.addNode(host1.id(), "127.0.0.1", 30303, 30303);
host2.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30303, 30303));
startedForwarder = true;
@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(forwarding)
{
for (auto i: whost2->checkWatch(w))
{
Message msg = whost2->envelope(i).open(whost2->fullTopic(w));
Message msg = whost2->envelope(i).open(whost2->fullTopics(w));
cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
}
this_thread::sleep_for(chrono::milliseconds(50));
@ -184,12 +184,15 @@ BOOST_AUTO_TEST_CASE(forwarding)
while (!startedForwarder)
this_thread::sleep_for(chrono::milliseconds(50));
Host ph("Sender", NetworkPreferences(30300, "", false, true));
Host ph("Sender", NetworkPreferences("127.0.0.1", 30300, false));
ph.setIdealPeerCount(1);
shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost());
ph.start();
ph.addNode(host2.id(), "127.0.0.1", 30305, 30305);
while (!ph.isStarted())
ph.addNode(host2.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30305, 30305));
while (!ph.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(10));
while (!ph.peerCount())
this_thread::sleep_for(chrono::milliseconds(10));
KeyPair us = KeyPair::create();
@ -214,11 +217,11 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
bool done = false;
// Host must be configured not to share peers.
Host host1("Forwarder", NetworkPreferences(30305, "", false, true));
Host host1("Forwarder", NetworkPreferences("127.0.0.1", 30305, false));
host1.setIdealPeerCount(1);
auto whost1 = host1.registerCapability(new WhisperHost());
host1.start();
while (!host1.isStarted())
while (!host1.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
bool startedForwarder = false;
@ -227,7 +230,6 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
setThreadName("forwarder");
this_thread::sleep_for(chrono::milliseconds(500));
// ph.addNode("127.0.0.1", 30303, 30303);
startedForwarder = true;
@ -238,7 +240,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
{
for (auto i: whost1->checkWatch(w))
{
Message msg = whost1->envelope(i).open(whost1->fullTopic(w));
Message msg = whost1->envelope(i).open(whost1->fullTopics(w));
cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
}
this_thread::sleep_for(chrono::milliseconds(50));
@ -249,13 +251,13 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
this_thread::sleep_for(chrono::milliseconds(2));
{
Host host2("Sender", NetworkPreferences(30300, "", false, true));
Host host2("Sender", NetworkPreferences("127.0.0.1", 30300, false));
host2.setIdealPeerCount(1);
shared_ptr<WhisperHost> whost2 = host2.registerCapability(new WhisperHost());
host2.start();
while (!host2.isStarted())
while (!host2.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
host2.addNode(host1.id(), "127.0.0.1", 30305, 30305);
host2.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30305, 30305));
while (!host2.peerCount())
this_thread::sleep_for(chrono::milliseconds(5));
@ -266,13 +268,13 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
}
{
Host ph("Listener", NetworkPreferences(30300, "", false, true));
Host ph("Listener", NetworkPreferences("127.0.0.1", 30300, false));
ph.setIdealPeerCount(1);
shared_ptr<WhisperHost> wh = ph.registerCapability(new WhisperHost());
ph.start();
while (!ph.isStarted())
while (!ph.haveNetwork())
this_thread::sleep_for(chrono::milliseconds(2));
ph.addNode(host1.id(), "127.0.0.1", 30305, 30305);
ph.addNode(host1.id(), NodeIPEndpoint(bi::address::from_string("127.0.0.1"), 30305, 30305));
/// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask("test"));
@ -281,7 +283,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
{
for (auto i: wh->checkWatch(w))
{
Message msg = wh->envelope(i).open(wh->fullTopic(w));
Message msg = wh->envelope(i).open(wh->fullTopics(w));
unsigned last = RLP(msg.payload()).toInt<unsigned>();
cnote << "New message from:" << msg.from() << RLP(msg.payload()).toInt<unsigned>();
result = last;
@ -296,6 +298,5 @@ BOOST_AUTO_TEST_CASE(asyncforwarding)
BOOST_REQUIRE_EQUAL(result, 1);
}
#endif
BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save