diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 7 | ||||
-rw-r--r-- | test/src/test-eventdelay.cpp | 2 | ||||
-rw-r--r-- | test/src/test-sockets.cpp | 98 |
3 files changed, 105 insertions, 2 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 10c1213..62b8749 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -117,7 +117,7 @@ set_target_properties(test-url PROPERTIES FOLDER "Tests") add_executable(test-cmdline-parsing src/test-cmdline-parsing.cpp) target_link_libraries(test-cmdline-parsing uscxml) # add_test(test-cmdline-parsing ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-cmdline-parsing) -# set_target_properties(test-cmdline-parsing PROPERTIES FOLDER "Tests") +set_target_properties(test-cmdline-parsing PROPERTIES FOLDER "Tests") # add_executable(test-initial-config src/test-initial-config.cpp) # target_link_libraries(test-initial-config uscxml) @@ -129,6 +129,11 @@ target_link_libraries(test-datamodel uscxml) add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-datamodel) set_target_properties(test-datamodel PROPERTIES FOLDER "Tests") +add_executable(test-sockets src/test-sockets.cpp) +target_link_libraries(test-sockets uscxml) +# add_test(test-datamodel ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test-sockets) +set_target_properties(test-sockets PROPERTIES FOLDER "Tests") + # if (NOT WIN32) # add_executable(test-mmi src/test-mmi.cpp) # target_link_libraries(test-mmi uscxml) diff --git a/test/src/test-eventdelay.cpp b/test/src/test-eventdelay.cpp index 12cc751..ce6c923 100644 --- a/test/src/test-eventdelay.cpp +++ b/test/src/test-eventdelay.cpp @@ -1,4 +1,4 @@ -#include "uscxml/concurrency/eventqueue/DelayedEventQueue.h" +#include "uscxml/concurrency/DelayedEventQueue.h" #include <iostream> int eventCalled = 0; diff --git a/test/src/test-sockets.cpp b/test/src/test-sockets.cpp new file mode 100644 index 0000000..a712da0 --- /dev/null +++ b/test/src/test-sockets.cpp @@ -0,0 +1,98 @@ +#include "uscxml/config.h" +#include "uscxml/server/Socket.h" +#include <iostream> + +#include <event2/event.h> +#include "event2/thread.h" + +#ifdef HAS_SIGNAL_H +#include <signal.h> +#endif + +#include "uscxml/concurrency/tinythread.h" + +using namespace uscxml; + +class TestServer : public ServerSocket { +public: + TestServer(int domain, int type, int protocol) : ServerSocket(domain, type, protocol) {} + virtual void readCallback(const char* data, size_t size, Connection& conn) { + std::string content(data, size); +// std::cout << "Server got: " << content << std::endl; + std::string urghs("hi!"); + conn.reply(urghs.data(), urghs.size()); + }; +}; + +class TestClient : public ClientSocket { +public: + TestClient(int domain, int type, int protocol) : ClientSocket(domain, type, protocol) {} + virtual void readCallback(const char* data, size_t size) { + std::string content(data, size); + }; +}; + +int main(int argc, char** argv) { + +#if defined(HAS_SIGNAL_H) && !defined(WIN32) + signal(SIGPIPE, SIG_IGN); +#endif + +#ifndef _WIN32 + evthread_use_pthreads(); +#else + evthread_use_windows_threads(); +#endif + + if (0) { + // start server socket and connect + int iterations = 100; + + TestServer server(PF_INET, SOCK_STREAM, 0); + try { + server.listen("*", 1234); + + while(iterations--) { + std::cout << iterations << std::endl; + TestClient client(PF_INET, SOCK_STREAM, 0); + client.connect("127.0.0.1", 1234); + + std::string hello("hello"); + client.write(hello.data(), hello.size()); + + tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20)); + } + + } catch (std::runtime_error e) { + std::cout << e.what() << std::endl; + } + } + + { + // connect client to server and kill server + int iterations = 100; + + try { + + while(iterations--) { + std::cout << iterations << std::endl; + TestServer* server = new TestServer(PF_INET, SOCK_STREAM, 0); + server->listen("*", 1236 + iterations); + + TestClient client(PF_INET, SOCK_STREAM, 0); + client.connect("127.0.0.1", 1236 + iterations); + + std::string hello("hello"); + client.write(hello.data(), hello.size()); + + delete server; + + tthread::this_thread::sleep_for(tthread::chrono::milliseconds(20)); + } + + } catch (std::runtime_error e) { + std::cout << e.what() << std::endl; + } + + } +}
\ No newline at end of file |