This commit is contained in:
Erik Frojdh 2019-06-05 17:30:21 +02:00
parent 3d6404952a
commit 03f8b389ad
5 changed files with 1347 additions and 2 deletions

1264
catch/clara.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -79,6 +79,8 @@ int DataSocket::sendData(const void *buffer, size_t size) {
break;
dataSent += thisSend;
}
if(dataSent != size)
throw SocketError("Could not send\n");
return dataSent;
}

View File

@ -7,6 +7,16 @@ set(SLS_TEST_SOURCES
test.cpp
)
add_executable(testclient src/testclient.cpp)
target_link_libraries(testclient slsSupportLib)
set_target_properties(testclient PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(testserver src/testserver.cpp)
target_link_libraries(testserver slsSupportLib)
set_target_properties(testserver PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_executable(tests ${SLS_TEST_SOURCES})
target_link_libraries(tests
slsProjectOptions
@ -31,8 +41,9 @@ endif (SLS_USE_RECEIVER)
set_target_properties(tests PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
include(CTest)
include(Catch)
catch_discover_tests(tests)
# #TODO! Move to automatic test discovery
# add_test(test ${CMAKE_BINARY_DIR}/bin/testSlsReceiver)

35
tests/src/testclient.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "ClientSocket.h"
#include "clara.hpp"
#include <iostream>
#include "container_utils.h"
int main(int argc, char** argv) {
std::cout << "Test client\n";
std::string hostname{"localhost"};
int port = 2345;
auto cli =
clara::Opt(hostname, "hostname")["-hn"]["--hostname"]("Hostname") |
clara::Opt(port, "port")["-p"]["--port"]("Port to send to");
auto result = cli.parse(clara::Args(argc, argv));
if (!result) {
std::cerr << "Error in command line: " << result.errorMessage()
<< std::endl;
exit(1);
}
std::cout << "Sending to: " << hostname << ":" << port << "\n";
auto data = sls::make_unique<char[]>(5000);
for (int64_t i = 0; i!=10; ++i){
std::cout << "Sending: " << i << "\n";
auto socket = sls::ClientSocket("test", hostname, port);
std::cout << "Sent: " << socket.sendData(i) << " bytes\n";
std::cout << "Sent: " << socket.sendData(data.get(), 5000) << " bytes\n";
}
}

33
tests/src/testserver.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "ServerSocket.h"
#include "clara.hpp"
#include <iostream>
#include "container_utils.h"
int main(int argc, char **argv) {
std::cout << "Test server\n";
int port = 2345;
auto cli = clara::Opt(port, "port")["-p"]["--port"]("Port to send to");
auto result = cli.parse(clara::Args(argc, argv));
if (!result) {
std::cerr << "Error in command line: " << result.errorMessage()
<< std::endl;
exit(1);
}
std::cout << "Listening to port: " << port << "\n";
auto server = sls::ServerSocket(port);
auto data = sls::make_unique<char[]>(5000);
while (true) {
try {
auto socket = server.accept();
auto val = socket.receive<long>();
std::cout << "Value: " << val << "\n";
std::cout << "Read: " << socket.receiveData(data.get(), 5000) << " bytes";
} catch (const sls::RuntimeError &e) {
}
}
}