tests add to namespace sls (#466)

* tests add to namespace sls

* fixed for tests

* finish up namespace sls for tests
This commit is contained in:
Dhanya Thattil
2022-05-23 16:17:32 +02:00
committed by GitHub
parent 8656eeec25
commit d61741c28b
44 changed files with 360 additions and 236 deletions

View File

@ -15,6 +15,8 @@
#include <unistd.h>
#include <vector>
namespace sls {
constexpr int default_port = 50001;
int open_socket(int port) {
@ -31,16 +33,16 @@ int open_socket(int port) {
const std::string portname = std::to_string(port);
if (getaddrinfo(host, portname.c_str(), &hints, &res)) {
throw sls::RuntimeError("Failed at getaddrinfo with " +
throw RuntimeError("Failed at getaddrinfo with " +
std::string(host));
}
int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd == -1) {
throw sls::RuntimeError("Failed to create UDP RX socket");
throw RuntimeError("Failed to create UDP RX socket");
}
if (connect(fd, res->ai_addr, res->ai_addrlen)) {
throw sls::RuntimeError("Failed to connect socket");
throw RuntimeError("Failed to connect socket");
}
freeaddrinfo(res);
return fd;
@ -49,7 +51,7 @@ int open_socket(int port) {
TEST_CASE("Get packet size returns the packet size we set in the constructor") {
constexpr int port = 50001;
constexpr ssize_t packet_size = 8000;
sls::UdpRxSocket s{port, packet_size};
UdpRxSocket s{port, packet_size};
CHECK(s.getPacketSize() == packet_size);
}
@ -60,7 +62,7 @@ TEST_CASE("Receive data from a vector") {
ssize_t packet_size =
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
sls::UdpRxSocket udpsock{port, packet_size};
UdpRxSocket udpsock{port, packet_size};
int fd = open_socket(port);
auto n = write(fd, data_to_send.data(), packet_size);
@ -74,13 +76,13 @@ TEST_CASE("Receive data from a vector") {
TEST_CASE("Shutdown socket without hanging when waiting for data") {
constexpr int port = 50001;
constexpr ssize_t packet_size = 8000;
sls::UdpRxSocket s{port, packet_size};
UdpRxSocket s{port, packet_size};
char buff[packet_size];
// Start a thread and wait for package
// if the socket is left open we would block
std::future<bool> ret =
std::async(&sls::UdpRxSocket::ReceivePacket, &s, (char *)&buff);
std::async(&UdpRxSocket::ReceivePacket, &s, (char *)&buff);
s.Shutdown();
auto r = ret.get();
@ -90,7 +92,7 @@ TEST_CASE("Shutdown socket without hanging when waiting for data") {
TEST_CASE("Too small packet") {
constexpr int port = 50001;
sls::UdpRxSocket s(port, 2 * sizeof(uint32_t));
UdpRxSocket s(port, 2 * sizeof(uint32_t));
auto fd = open_socket(port);
uint32_t val = 10;
write(fd, &val, sizeof(val));
@ -103,8 +105,10 @@ TEST_CASE("Receive an int to an external buffer") {
int to_send = 5;
int received = -1;
auto fd = open_socket(default_port);
sls::UdpRxSocket s(default_port, sizeof(int));
UdpRxSocket s(default_port, sizeof(int));
write(fd, &to_send, sizeof(to_send));
CHECK(s.ReceivePacket(reinterpret_cast<char *>(&received)));
CHECK(received == to_send);
}
} // namespace sls