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

Add back more peer tests

parent 80dd8500
No related branches found
No related tags found
No related merge requests found
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <glog/logging.h>
#include <ftl/net/socket.hpp>
#include <ftl/net/protocol.hpp>
#include <functional>
#include <iostream>
using ftl::net::Socket;
using ftl::net::Protocol;
std::map<std::string,Protocol*> Protocol::protocols__;
Protocol *Protocol::find(const std::string &id) {
if (protocols__.count(id) > 0) return protocols__[id];
else return NULL;
}
Protocol::Protocol(const std::string &id) : id_(id) {
protocols__[id] = this;
}
Protocol::~Protocol() {
protocols__.erase(id_);
// TODO Make sure all dependent sockets are closed!
}
void Protocol::bind(int service, std::function<void(uint32_t,Socket&)> func) {
if (handlers_.count(service) == 0) {
handlers_[service] = func;
} else {
LOG(ERROR) << "Message service " << service << " already bound";
}
}
void Protocol::dispatchRPC(Socket &s, const std::string &d) {
disp_.dispatch(s,d);
}
void Protocol::dispatchRaw(uint32_t service, Socket &s) {
// Lookup raw message handler
if (handlers_.count(service) > 0) {
handlers_[service](service, s);
} else {
LOG(ERROR) << "Unrecognised service request (" << service << ") from " << s.getURI();
}
}
...@@ -260,26 +260,6 @@ TEST_CASE("Peer::bind()", "[rpc]") { ...@@ -260,26 +260,6 @@ TEST_CASE("Peer::bind()", "[rpc]") {
} }
} }
/*TEST_CASE("Socket::operator>>()", "[io]") {
MockPeer s;
SECTION("stream ints") {
int i[2];
i[0] = 99;
i[1] = 101;
fake_send(0, 100, std::string((char*)&i,2*sizeof(int)));
i[0] = 0;
i[1] = 0;
s.mock_data(); // Force a message read, but no protocol...
REQUIRE( (s.size() == 2*sizeof(int)) );
s >> i;
REQUIRE( (i[0] == 99) );
REQUIRE( (i[1] == 101) );
}
}*/
TEST_CASE("Socket::send()", "[io]") { TEST_CASE("Socket::send()", "[io]") {
MockPeer s; MockPeer s;
...@@ -293,49 +273,54 @@ TEST_CASE("Socket::send()", "[io]") { ...@@ -293,49 +273,54 @@ TEST_CASE("Socket::send()", "[io]") {
REQUIRE( (get<0>(value) == 607) ); REQUIRE( (get<0>(value) == 607) );
} }
/*SECTION("send a string") { SECTION("send a string") {
std::string str("hello world"); std::string str("hello world");
s.send(100,str); s.send("dummy",str);
REQUIRE( (get_service(0) == 100) ); auto [name, value] = readResponse<tuple<std::string>>(0);
REQUIRE( (get_size(0) == str.size()) );
REQUIRE( (get_value<std::string>(0) == "hello world") ); REQUIRE( (name == "dummy") );
REQUIRE( (get<0>(value) == "hello world") );
} }
SECTION("send const char* string") { SECTION("send const char* string") {
s.send(100,"hello world"); s.send("dummy","hello world");
REQUIRE( (get_service(0) == 100) ); auto [name, value] = readResponse<tuple<std::string>>(0);
REQUIRE( (get_size(0) == 11) );
REQUIRE( (get_value<std::string>(0) == "hello world") ); REQUIRE( (name == "dummy") );
REQUIRE( (get<0>(value) == "hello world") );
} }
SECTION("send const char* array") { /*SECTION("send const char* array") {
s.send(100,ftl::net::array{"hello world",10}); s.send(100,ftl::net::array{"hello world",10});
REQUIRE( (get_service(0) == 100) ); REQUIRE( (get_service(0) == 100) );
REQUIRE( (get_size(0) == 10) ); REQUIRE( (get_size(0) == 10) );
REQUIRE( (get_value<std::string>(0) == "hello worl") ); REQUIRE( (get_value<std::string>(0) == "hello worl") );
} }*/
SECTION("send a tuple") { SECTION("send a tuple") {
auto tup = std::make_tuple(55,66,true,6.7); auto tup = std::make_tuple(55,66,true,6.7);
s.send(100,tup); s.send("dummy",tup);
REQUIRE( (get_service(0) == 100) ); auto [name, value] = readResponse<tuple<decltype(tup)>>(0);
REQUIRE( (get_size(0) == sizeof(tup)) );
REQUIRE( (get_value<decltype(tup)>(0) == tup) ); REQUIRE( (name == "dummy") );
REQUIRE( (get<1>(get<0>(value)) == 66) );
} }
SECTION("send multiple strings") { SECTION("send multiple strings") {
std::string str("hello "); std::string str("hello ");
std::string str2("world"); std::string str2("world");
s.send(100,str,str2); s.send("dummy2",str,str2);
REQUIRE( (get_service(0) == 100) ); auto [name, value] = readResponse<tuple<std::string,std::string>>(0);
REQUIRE( (get_size(0) == str.size()+str2.size()) );
REQUIRE( (get_value<std::string>(0) == "hello world") ); REQUIRE( (name == "dummy2") );
}*/ REQUIRE( (get<0>(value) == "hello") );
REQUIRE( (get<1>(value) == "world") );
}
} }
/*TEST_CASE("Socket::read()", "[io]") { /*TEST_CASE("Socket::read()", "[io]") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment