Skip to content
Snippets Groups Projects
Commit ea86f98c authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Make net tests more reliable

parent abec46f6
No related branches found
No related tags found
No related merge requests found
Pipeline #10496 passed
...@@ -56,7 +56,7 @@ class Universe { ...@@ -56,7 +56,7 @@ class Universe {
* *
* @param addr URI giving protocol, interface and port * @param addr URI giving protocol, interface and port
*/ */
bool connect(const std::string &addr); Peer *connect(const std::string &addr);
int numberOfPeers() const { return peers_.size(); } int numberOfPeers() const { return peers_.size(); }
......
...@@ -178,7 +178,8 @@ Peer::Peer(const char *pUri, Dispatcher *d) : uri_(pUri) { ...@@ -178,7 +178,8 @@ Peer::Peer(const char *pUri, Dispatcher *d) : uri_(pUri) {
scheme_ = uri.getProtocol(); scheme_ = uri.getProtocol();
if (uri.getProtocol() == URI::SCHEME_TCP) { if (uri.getProtocol() == URI::SCHEME_TCP) {
sock_ = tcpConnect(uri); sock_ = tcpConnect(uri);
status_ = kConnecting; if (sock_ != INVALID_SOCKET) status_ = kConnecting;
else status_ = kReconnecting;
} else if (uri.getProtocol() == URI::SCHEME_WS) { } else if (uri.getProtocol() == URI::SCHEME_WS) {
LOG(INFO) << "Websocket connect " << uri.getPath(); LOG(INFO) << "Websocket connect " << uri.getPath();
sock_ = tcpConnect(uri); sock_ = tcpConnect(uri);
......
...@@ -66,9 +66,9 @@ bool Universe::listen(const string &addr) { ...@@ -66,9 +66,9 @@ bool Universe::listen(const string &addr) {
return l->isListening(); return l->isListening();
} }
bool Universe::connect(const string &addr) { Peer *Universe::connect(const string &addr) {
auto p = new Peer(addr.c_str(), &disp_); auto p = new Peer(addr.c_str(), &disp_);
if (!p) return false; if (!p) return nullptr;
if (p->status() != Peer::kInvalid) { if (p->status() != Peer::kInvalid) {
unique_lock<mutex> lk(net_mutex_); unique_lock<mutex> lk(net_mutex_);
...@@ -81,7 +81,7 @@ bool Universe::connect(const string &addr) { ...@@ -81,7 +81,7 @@ bool Universe::connect(const string &addr) {
peer_ids_[p.id()] = &p; peer_ids_[p.id()] = &p;
}); });
return p->status() == Peer::kConnecting; return p;
} }
int Universe::_setDescriptors() { int Universe::_setDescriptors() {
......
...@@ -17,28 +17,31 @@ TEST_CASE("Universe::connect()", "[net]") { ...@@ -17,28 +17,31 @@ TEST_CASE("Universe::connect()", "[net]") {
Universe b; Universe b;
a.listen("tcp://localhost:7077"); a.listen("tcp://localhost:7077");
sleep_for(milliseconds(100)); //sleep_for(milliseconds(100));
SECTION("valid tcp connection using ipv4") { SECTION("valid tcp connection using ipv4") {
REQUIRE( b.connect("tcp://127.0.0.1:7077") ); auto p = b.connect("tcp://127.0.0.1:7077");
REQUIRE( p );
sleep_for(milliseconds(200)); while (!p->isConnected()) sleep_for(milliseconds(20));
REQUIRE( a.numberOfPeers() == 1 ); REQUIRE( a.numberOfPeers() == 1 );
REQUIRE( b.numberOfPeers() == 1 ); REQUIRE( b.numberOfPeers() == 1 );
} }
SECTION("valid tcp connection using hostname") { SECTION("valid tcp connection using hostname") {
REQUIRE( b.connect("tcp://localhost:7077") ); auto p = b.connect("tcp://localhost:7077");
REQUIRE( p );
sleep_for(milliseconds(200)); while (!p->isConnected()) sleep_for(milliseconds(20));
REQUIRE( a.numberOfPeers() == 1 ); REQUIRE( a.numberOfPeers() == 1 );
REQUIRE( b.numberOfPeers() == 1 ); REQUIRE( b.numberOfPeers() == 1 );
} }
SECTION("invalid protocol") { SECTION("invalid protocol") {
REQUIRE( !b.connect("http://127.0.0.1:7077") ); auto p = b.connect("http://127.0.0.1:7077");
REQUIRE( !p->isValid() );
sleep_for(milliseconds(100)); sleep_for(milliseconds(100));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment