Skip to content
Snippets Groups Projects
Commit f07745aa authored by Carsten Patzke's avatar Carsten Patzke
Browse files

tests && added new_thread to system io

parent 78b2db23
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ function(gtest target test_source_files test_libraries)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(test-${target} ${test_source_files})
target_link_libraries(test-${target} gtest gmock gtest_main ${CMAKE_THREAD_LIBS_INIT})
if (NOT ${test_libraries} STREQUAL "")
if (NOT "${test_libraries}" STREQUAL "")
target_link_libraries(test-${target} ${test_libraries})
endif ()
add_test(NAME test-${target} COMMAND test-${target})
......
......@@ -6,6 +6,7 @@
#include <chrono>
#include <sys/socket.h>
#include <memory>
#include <thread>
#include "common/file_info.h"
......@@ -45,6 +46,12 @@ typedef int FileDescriptor;
class IO {
public:
/*
* System
*/
template<typename Function, typename... Args>
std::thread* new_thread(Function&& f, Args&&... args) {return nullptr;};
/*
* Network
*/
......
......@@ -8,6 +8,12 @@ namespace hidra2 {
class SystemIO final : public IO {
public:
/*
* System
*/
template<typename Function, typename... Args>
std::thread* new_thread(Function&& f, Args&&... args);
/*
* Network
*/
......
......@@ -163,6 +163,10 @@ std::vector<FileInfo> SystemIO::FilesInFolder(const string& folder, IOErrors* er
return files;
}
template<typename Function, typename... Args>
std::thread* hidra2::SystemIO::new_thread(Function &&f, Args &&... args) {
return new std::thread(std::forward(f), std::forward(args)...);
}
hidra2::FileDescriptor hidra2::SystemIO::create_socket(hidra2::AddressFamilies address_family,
hidra2::SocketTypes socket_type,
......@@ -380,4 +384,3 @@ size_t hidra2::SystemIO::send(hidra2::FileDescriptor socket_fd,
return already_sent;
}
......@@ -18,7 +18,7 @@ set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
################################
# Testing
################################
set(TEST_SOURCE_FILES unittests/test_receiver.cpp)
set(TEST_LIBRARIES producer-api)
set(TEST_SOURCE_FILES unittests/test_receiver.cpp src/receiver.h src/receiver.cpp src/network_producer_peer.h src/network_producer_peer.cpp src/network_producer_peer_handlers.cpp src/file_refernce_handler.h src/file_refernce_handler.cpp)
set(TEST_LIBRARIES common pthread)
gtest(${TARGET_NAME} ${TEST_SOURCE_FILES} ${TEST_LIBRARIES})
gtest(${TARGET_NAME} "${TEST_SOURCE_FILES}" "${TEST_LIBRARIES}")
......@@ -21,6 +21,8 @@ void hidra2::Receiver::start_listener(std::string listener_address, uint16_t por
FileDescriptor listener_fd = io->create_socket(AddressFamilies::INET, SocketTypes::STREAM, SocketProtocols::IP,
&io_error);
if(io_error != IOErrors::NO_ERROR) {
*err = ReceiverError::FAILED_CREATING_SOCKET;
listener_running_ = false;
std::cerr << "Fail to create socket" << std::endl;
return;
}
......@@ -28,6 +30,8 @@ void hidra2::Receiver::start_listener(std::string listener_address, uint16_t por
io->inet_bind(listener_fd, listener_address, port, &io_error);
if(io_error != IOErrors::NO_ERROR) {
io->deprecated_close(listener_fd);
*err = ReceiverError::FAILED_CREATING_SOCKET;
listener_running_ = false;
std::cerr << "Fail to bind socket" << std::endl;
return;
}
......@@ -35,6 +39,8 @@ void hidra2::Receiver::start_listener(std::string listener_address, uint16_t por
io->listen(listener_fd, kMaxUnacceptedConnectionsBacklog, &io_error);
if(io_error != IOErrors::NO_ERROR) {
io->deprecated_close(listener_fd);
*err = ReceiverError::FAILED_CREATING_SOCKET;
listener_running_ = false;
std::cerr << "Fail to start listen" << std::endl;
return;
}
......
......@@ -13,13 +13,11 @@ namespace hidra2 {
enum class ReceiverError {
NO_ERROR,
ALREADY_LISTEING,
FAILD_CREATING_SOCKET,
FAILED_CREATING_SOCKET,
};
class Receiver : HasIO {
class Receiver : public HasIO {
private:
static const int kMaxUnacceptedConnectionsBacklog;
bool listener_running_ = false;
FileDescriptor listener_fd_;
std::thread* accept_thread_ = nullptr;
......@@ -28,6 +26,8 @@ class Receiver : HasIO {
std::list<std::unique_ptr<NetworkProducerPeer>> peer_list_;
std::unique_ptr<NetworkProducerPeer> on_new_peer_(int peer_socket_fd, std::string address);
public:
static const int kMaxUnacceptedConnectionsBacklog;
Receiver(const Receiver&) = delete;
Receiver& operator=(const Receiver&) = delete;
Receiver() = default;
......
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <system_wrappers/io.h>
#include "../src/receiver.h"
namespace {
TEST(EMPTY, REMOVEME) {
EXPECT_EQ(1, 1);
using ::testing::Return;
using ::testing::_;
using ::testing::DoAll;
using ::testing::SetArgReferee;
using ::testing::Gt;
using ::testing::Mock;
using ::testing::InSequence;
class MockIO : public hidra2::IO {
public:
MOCK_METHOD4(create_socket,
hidra2::FileDescriptor(hidra2::AddressFamilies address_family, hidra2::SocketTypes socket_type,
hidra2::SocketProtocols socket_protocol, hidra2::IOErrors* err));
MOCK_METHOD3(listen,
void(hidra2::FileDescriptor socket_fd, int backlog, hidra2::IOErrors* err));
MOCK_METHOD4(inet_bind,
void(hidra2::FileDescriptor socket_fd, const std::string& address, uint16_t port, hidra2::IOErrors* err));
MOCK_METHOD2(inet_accept,
std::unique_ptr<std::tuple<std::string, hidra2::FileDescriptor>>(hidra2::FileDescriptor socket_fd,
hidra2::IOErrors* err));
MOCK_METHOD3(inet_connect,
void(hidra2::FileDescriptor socket_fd, const std::string& address, hidra2::IOErrors* err));
MOCK_METHOD2(create_and_connect_ip_tcp_socket,
hidra2::FileDescriptor(const std::string& address, hidra2::IOErrors* err));
MOCK_METHOD4(receive,
size_t(hidra2::FileDescriptor socket_fd, void* buf, size_t length, hidra2::IOErrors* err));
MOCK_METHOD5(receive_timeout,
size_t(hidra2::FileDescriptor socket_fd, void* buf, size_t length, uint16_t timeout_in_sec, hidra2::IOErrors* err));
MOCK_METHOD4(send,
size_t(hidra2::FileDescriptor socket_fd, const void* buf, size_t length, hidra2::IOErrors* err));
MOCK_METHOD2(deprecated_open,
int(const char* __file, int __oflag));
MOCK_METHOD1(deprecated_close,
int(int __fd));
MOCK_METHOD3(deprecated_read,
ssize_t(int __fd, void* buf, size_t count));
MOCK_METHOD3(deprecated_write,
ssize_t(int __fd, const void* __buf, size_t __n));
MOCK_METHOD2(GetDataFromFile,
hidra2::FileData(const std::string& fname, hidra2::IOErrors* err));
MOCK_METHOD2(FilesInFolder,
std::vector<hidra2::FileInfo>(const std::string& folder, hidra2::IOErrors* err));
};
hidra2::Receiver receiver;
hidra2::FileDescriptor expected_socket_fd = 1338;
TEST(Receiver, start_listener__create_socket_fail) {
MockIO mockIO;
receiver.__set_io(&mockIO);
InSequence sequence;
std::string expected_address = "127.0.0.1";
uint16_t expected_port = 9876;
EXPECT_CALL(mockIO, create_socket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, hidra2::SocketProtocols::IP, _))
.Times(1)
.WillOnce(
DoAll(
testing::SetArgPointee<3>(hidra2::IOErrors::UNKNOWN_ERROR),
Return(-1)
));
hidra2::ReceiverError receiver_error;
receiver.start_listener(expected_address, expected_port, &receiver_error);
EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET);
Mock::VerifyAndClearExpectations(&mockIO);
}
TEST(Receiver, start_listener__inet_bind_fail) {
MockIO mockIO;
receiver.__set_io(&mockIO);
InSequence sequence;
std::string expected_address = "127.0.0.1";
uint16_t expected_port = 9876;
EXPECT_CALL(mockIO, create_socket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, hidra2::SocketProtocols::IP, _))
.Times(1)
.WillOnce(
DoAll(
testing::SetArgPointee<3>(hidra2::IOErrors::NO_ERROR),
Return(expected_socket_fd)
));
EXPECT_CALL(mockIO, inet_bind(expected_socket_fd, expected_address, expected_port, _))
.Times(1)
.WillOnce(testing::SetArgPointee<3>(hidra2::IOErrors::ADDRESS_ALREADY_IN_USE));
hidra2::ReceiverError receiver_error;
receiver.start_listener(expected_address, expected_port, &receiver_error);
EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET);
Mock::VerifyAndClearExpectations(&mockIO);
}
TEST(Receiver, start_listener__listen_fail) {
MockIO mockIO;
receiver.__set_io(&mockIO);
InSequence sequence;
std::string expected_address = "127.0.0.1";
uint16_t expected_port = 9876;
EXPECT_CALL(mockIO, create_socket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, hidra2::SocketProtocols::IP, _))
.Times(1)
.WillOnce(
DoAll(
testing::SetArgPointee<3>(hidra2::IOErrors::NO_ERROR),
Return(expected_socket_fd)
));
EXPECT_CALL(mockIO, inet_bind(expected_socket_fd, expected_address, expected_port, _))
.Times(1)
.WillOnce(testing::SetArgPointee<3>(hidra2::IOErrors::NO_ERROR));
EXPECT_CALL(mockIO, listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _))
.Times(1)
.WillOnce(testing::SetArgPointee<2>(hidra2::IOErrors::BAD_FILE_NUMBER));
hidra2::ReceiverError receiver_error;
receiver.start_listener(expected_address, expected_port, &receiver_error);
EXPECT_EQ(receiver_error, hidra2::ReceiverError::FAILED_CREATING_SOCKET);
Mock::VerifyAndClearExpectations(&mockIO);
}
TEST(Receiver, start_listener) {
MockIO mockIO;
receiver.__set_io(&mockIO);
InSequence sequence;
std::string expected_address = "127.0.0.1";
uint16_t expected_port = 9876;
EXPECT_CALL(mockIO, create_socket(hidra2::AddressFamilies::INET, hidra2::SocketTypes::STREAM, hidra2::SocketProtocols::IP, _))
.Times(1)
.WillOnce(
DoAll(
testing::SetArgPointee<3>(hidra2::IOErrors::NO_ERROR),
Return(expected_socket_fd)
));
EXPECT_CALL(mockIO, inet_bind(expected_socket_fd, expected_address, expected_port, _))
.Times(1)
.WillOnce(testing::SetArgPointee<3>(hidra2::IOErrors::NO_ERROR));
EXPECT_CALL(mockIO, listen(expected_socket_fd, receiver.kMaxUnacceptedConnectionsBacklog, _))
.Times(1)
.WillOnce(testing::SetArgPointee<2>(hidra2::IOErrors::NO_ERROR));
hidra2::ReceiverError receiver_error;
receiver.start_listener(expected_address, expected_port, &receiver_error);
EXPECT_EQ(receiver_error, hidra2::ReceiverError::NO_ERROR);
Mock::VerifyAndClearExpectations(&mockIO);
}
TEST(Receiver, start_listener_already_listening) {
MockIO mockIO;
receiver.__set_io(&mockIO);
InSequence sequence;
std::string expected_address = "127.0.0.1";
uint16_t expected_port = 9876;
hidra2::ReceiverError receiver_error;
receiver.start_listener(expected_address, expected_port, &receiver_error);
EXPECT_EQ(receiver_error, hidra2::ReceiverError::ALREADY_LISTEING);
Mock::VerifyAndClearExpectations(&mockIO);
}
}
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