From 00d8ed71fde25ea4b5766abf0b2947895e3730fb Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 7 May 2016 07:08:21 -0400 Subject: [PATCH] Tidy up test sources: Test support classes are moved to beast/extras/test. --- CMakeLists.txt | 17 ++-- examples/CMakeLists.txt | 8 +- examples/http_server.cpp | 4 +- {test => extras/beast/test}/fail_stream.hpp | 2 +- extras/beast/test/sig_wait.hpp | 36 ++++++++ {test => extras/beast/test}/string_stream.hpp | 7 +- {test => extras/beast/test}/yield_to.hpp | 23 +++++- {test => extras/beast/unit_test}/main.cpp | 0 test/CMakeLists.txt | 82 +------------------ test/Jamfile | 8 +- test/http/CMakeLists.txt | 44 ++++++++++ test/http/read.cpp | 7 +- test/sig_wait.hpp | 42 ---------- test/websocket/CMakeLists.txt | 33 ++++++++ test/websocket/stream.cpp | 7 +- test/websocket/websocket_echo.cpp | 4 +- 16 files changed, 173 insertions(+), 151 deletions(-) rename {test => extras/beast/test}/fail_stream.hpp (99%) create mode 100644 extras/beast/test/sig_wait.hpp rename {test => extras/beast/test}/string_stream.hpp (92%) rename {test => extras/beast/test}/yield_to.hpp (74%) rename {test => extras/beast/unit_test}/main.cpp (100%) create mode 100644 test/http/CMakeLists.txt delete mode 100644 test/sig_wait.hpp create mode 100644 test/websocket/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index b084040b..dab07e92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,20 +25,25 @@ endif() message ("cxx Flags: " ${CMAKE_CXX_FLAGS}) -macro(GroupSources curdir) +function(DoGroupSources curdir rootdir folder) file(GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*) foreach(child ${children}) if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child}) - GroupSources(${curdir}/${child}) + DoGroupSources(${curdir}/${child} ${rootdir} ${folder}) elseif(${child} STREQUAL "CMakeLists.txt") source_group("" FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child}) else() - string(REPLACE "/" "\\" groupname ${curdir}) - string(REPLACE "src" "Common" groupname ${groupname}) + string(REGEX REPLACE ^${rootdir} ${folder} groupname ${curdir}) + #set(groupname ${curdir}) + string(REPLACE "/" "\\" groupname ${groupname}) source_group(${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child}) endif() endforeach() -endmacro() +endfunction() + +function(GroupSources curdir folder) + DoGroupSources(${curdir} ${curdir} ${folder}) +endfunction() include_directories (extras) include_directories (include) @@ -52,5 +57,7 @@ file(GLOB_RECURSE BEAST_INCLUDES add_subdirectory (examples) add_subdirectory (test) +add_subdirectory (test/http) +add_subdirectory (test/websocket) #enable_testing() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4f2e7adc..5b3d0972 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,8 +1,9 @@ # Part of Beast -GroupSources(include/beast) -GroupSources(examples) -GroupSources(test) +GroupSources(extras/beast beast) +GroupSources(include/beast beast) + +GroupSources(examples "/") add_executable (http-crawl ${BEAST_INCLUDES} @@ -22,7 +23,6 @@ add_executable (http-server http_stream.hpp http_stream.ipp http_sync_server.hpp - ../test/sig_wait.hpp http_server.cpp ) diff --git a/examples/http_server.cpp b/examples/http_server.cpp index ed191043..26aca29f 100644 --- a/examples/http_server.cpp +++ b/examples/http_server.cpp @@ -19,8 +19,8 @@ #include "http_async_server.hpp" #include "http_sync_server.hpp" -#include "../test/sig_wait.hpp" +#include #include #include @@ -72,5 +72,5 @@ int main(int ac, char const* av[]) http_sync_server server(ep, root); else http_async_server server(ep, threads, root); - sig_wait(); + beast::test::sig_wait(); } diff --git a/test/fail_stream.hpp b/extras/beast/test/fail_stream.hpp similarity index 99% rename from test/fail_stream.hpp rename to extras/beast/test/fail_stream.hpp index 079d34d4..622e7fb2 100644 --- a/test/fail_stream.hpp +++ b/extras/beast/test/fail_stream.hpp @@ -29,7 +29,7 @@ namespace beast { namespace test { -/* A stream wrapper that fails. +/** A stream wrapper that fails. On the Nth operation, the stream will fail with the specified error code, or the default error code of invalid_argument. diff --git a/extras/beast/test/sig_wait.hpp b/extras/beast/test/sig_wait.hpp new file mode 100644 index 00000000..5abcdec5 --- /dev/null +++ b/extras/beast/test/sig_wait.hpp @@ -0,0 +1,36 @@ +// +// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BEAST_TEST_SIG_WAIT_HPP +#define BEAST_TEST_SIG_WAIT_HPP + +#include +#include +#include + +namespace beast { +namespace test { + +/// Block until SIGINT or SIGTERM is received. +inline +void +sig_wait() +{ + boost::asio::io_service ios; + boost::asio::signal_set signals( + ios, SIGINT, SIGTERM); + signals.async_wait( + [&](boost::system::error_code const&, int) + { + }); + ios.run(); +} + +} // test +} // beast + +#endif diff --git a/test/string_stream.hpp b/extras/beast/test/string_stream.hpp similarity index 92% rename from test/string_stream.hpp rename to extras/beast/test/string_stream.hpp index 284074f1..6082f86b 100644 --- a/test/string_stream.hpp +++ b/extras/beast/test/string_stream.hpp @@ -17,7 +17,12 @@ namespace beast { namespace test { -// meets the requirements of AsyncStream, SyncStream +/** A SyncStream and AsyncStream that reads from a string. + + This class behaves like a socket, except that written data is simply + discarded, and when data is read it comes from a string provided + at construction. +*/ class string_stream { std::string s_; diff --git a/test/yield_to.hpp b/extras/beast/test/yield_to.hpp similarity index 74% rename from test/yield_to.hpp rename to extras/beast/test/yield_to.hpp index 1ea4710b..0bd897ed 100644 --- a/test/yield_to.hpp +++ b/extras/beast/test/yield_to.hpp @@ -5,8 +5,8 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_TEST_ENABLE_YIELD_TO_HPP -#define BEAST_TEST_ENABLE_YIELD_TO_HPP +#ifndef BEAST_TEST_YIELD_TO_HPP +#define BEAST_TEST_YIELD_TO_HPP #include #include @@ -19,7 +19,12 @@ namespace beast { namespace test { -// Mix-in to support tests using coroutines +/** Mix-in to support tests using asio coroutines. + + Derive from this class and use yield_to to launch test functions + inside coroutines. This is handy for testing asynchronous asio + code. +*/ class enable_yield_to { protected: @@ -33,6 +38,7 @@ private: bool running_ = false;; protected: + /// The type of yield context passed to functions. using yield_context = boost::asio::yield_context; @@ -52,7 +58,16 @@ protected: thread_.join(); } - // Run a function in a coroutine + /** Run a function in a coroutine. + + This call will block until the coroutine terminates. + + Function will be called with this signature: + + @code + void f(yield_context); + @endcode + */ template void yield_to(Function&& f); diff --git a/test/main.cpp b/extras/beast/unit_test/main.cpp similarity index 100% rename from test/main.cpp rename to extras/beast/unit_test/main.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2559f1fb..603fa573 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,12 +1,12 @@ # Part of Beast -GroupSources(include/beast) -GroupSources(extras) -GroupSources(test) +GroupSources(extras/beast beast) +GroupSources(include/beast beast) +GroupSources(test "/") add_executable (core-tests ${BEAST_INCLUDES} - main.cpp + ../extras/beast/unit_test/main.cpp async_completion.cpp basic_streambuf.cpp bind_handler.cpp @@ -37,77 +37,3 @@ add_executable (core-tests if (NOT WIN32) target_link_libraries(core-tests ${Boost_LIBRARIES}) endif() - -add_executable (http-tests - ${BEAST_INCLUDES} - fail_stream.hpp - string_stream.hpp - yield_to.hpp - main.cpp - http/basic_headers.cpp - http/basic_parser_v1.cpp - http/body_type.cpp - http/empty_body.cpp - http/headers.cpp - http/message.cpp - http/message_v1.cpp - http/parse_error.cpp - http/parser_v1.cpp - http/read.cpp - http/reason.cpp - http/resume_context.cpp - http/rfc2616.cpp - http/rfc7230.cpp - http/status.cpp - http/streambuf_body.cpp - http/string_body.cpp - http/type_check.cpp - http/write.cpp -) - -if (NOT WIN32) - target_link_libraries(http-tests ${Boost_LIBRARIES}) -endif() - -add_executable (bench-tests - ${BEAST_INCLUDES} - main.cpp - http/nodejs_parser.cpp - http/parser_bench.cpp -) - -if (NOT WIN32) - target_link_libraries(parser-bench ${Boost_LIBRARIES}) -endif() - -add_executable (websocket-tests - ${BEAST_INCLUDES} - fail_stream.hpp - string_stream.hpp - yield_to.hpp - websocket/websocket_async_echo_peer.hpp - websocket/websocket_sync_echo_peer.hpp - main.cpp - websocket/error.cpp - websocket/option.cpp - websocket/rfc6455.cpp - websocket/stream.cpp - websocket/teardown.cpp - websocket/utf8_checker.cpp -) - -if (NOT WIN32) - target_link_libraries(websocket-tests ${Boost_LIBRARIES}) -endif() - -add_executable (websocket-echo - ${BEAST_INCLUDES} - sig_wait.hpp - websocket/websocket_async_echo_peer.hpp - websocket/websocket_sync_echo_peer.hpp - websocket/websocket_echo.cpp -) - -if (NOT WIN32) - target_link_libraries(websocket-echo ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) -endif() diff --git a/test/Jamfile b/test/Jamfile index 668be933..7061be39 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -8,7 +8,7 @@ import os ; unit-test core-tests : - main.cpp + ../extras/beast/unit_test/main.cpp async_completion.cpp basic_streambuf.cpp bind_handler.cpp @@ -37,7 +37,7 @@ unit-test core-tests : ; unit-test http-tests : - main.cpp + ../extras/beast/unit_test/main.cpp http/basic_headers.cpp http/basic_parser_v1.cpp http/body_type.cpp @@ -60,13 +60,13 @@ unit-test http-tests : ; unit-test bench-tests : - main.cpp + ../extras/beast/unit_test/main.cpp http/nodejs_parser.cpp http/parser_bench.cpp ; unit-test websocket-tests : - main.cpp + ../extras/beast/unit_test/main.cpp websocket/error.cpp websocket/option.cpp websocket/rfc6455.cpp diff --git a/test/http/CMakeLists.txt b/test/http/CMakeLists.txt new file mode 100644 index 00000000..8c4749b0 --- /dev/null +++ b/test/http/CMakeLists.txt @@ -0,0 +1,44 @@ +# Part of Beast + +GroupSources(extras/beast beast) +GroupSources(include/beast beast) +GroupSources(test/http "/") + +add_executable (http-tests + ${BEAST_INCLUDES} + ../../extras/beast/unit_test/main.cpp + basic_headers.cpp + basic_parser_v1.cpp + body_type.cpp + empty_body.cpp + headers.cpp + message.cpp + message_v1.cpp + parse_error.cpp + parser_v1.cpp + read.cpp + reason.cpp + resume_context.cpp + rfc2616.cpp + rfc7230.cpp + status.cpp + streambuf_body.cpp + string_body.cpp + type_check.cpp + write.cpp +) + +if (NOT WIN32) + target_link_libraries(http-tests ${Boost_LIBRARIES}) +endif() + +add_executable (bench-tests + ${BEAST_INCLUDES} + ../../extras/beast/unit_test/main.cpp + nodejs_parser.cpp + parser_bench.cpp +) + +if (NOT WIN32) + target_link_libraries(parser-bench ${Boost_LIBRARIES}) +endif() diff --git a/test/http/read.cpp b/test/http/read.cpp index 0cdde345..e8e07e2e 100644 --- a/test/http/read.cpp +++ b/test/http/read.cpp @@ -8,11 +8,10 @@ // Test that header file is self-contained. #include -#include "../fail_stream.hpp" -#include "../string_stream.hpp" -#include "../yield_to.hpp" - #include +#include +#include +#include #include #include diff --git a/test/sig_wait.hpp b/test/sig_wait.hpp deleted file mode 100644 index 9efd3a78..00000000 --- a/test/sig_wait.hpp +++ /dev/null @@ -1,42 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#ifndef BEAST_TEST_SIG_WAIT_HPP -#define BEAST_TEST_SIG_WAIT_HPP - -#include -#include -#include - -// Block until SIGINT or SIGTERM -inline -void -sig_wait() -{ - boost::asio::io_service ios; - boost::asio::signal_set signals( - ios, SIGINT, SIGTERM); - signals.async_wait( - [&](boost::system::error_code const&, int) - { - }); - ios.run(); -} - -#endif diff --git a/test/websocket/CMakeLists.txt b/test/websocket/CMakeLists.txt new file mode 100644 index 00000000..f9fbd268 --- /dev/null +++ b/test/websocket/CMakeLists.txt @@ -0,0 +1,33 @@ +# Part of Beast + +GroupSources(extras/beast beast) +GroupSources(include/beast beast) +GroupSources(test/websocket "/") + +add_executable (websocket-tests + ${BEAST_INCLUDES} + ../../extras/beast/unit_test/main.cpp + websocket_async_echo_peer.hpp + websocket_sync_echo_peer.hpp + error.cpp + option.cpp + rfc6455.cpp + stream.cpp + teardown.cpp + utf8_checker.cpp +) + +if (NOT WIN32) + target_link_libraries(websocket-tests ${Boost_LIBRARIES}) +endif() + +add_executable (websocket-echo + ${BEAST_INCLUDES} + websocket_async_echo_peer.hpp + websocket_sync_echo_peer.hpp + websocket_echo.cpp +) + +if (NOT WIN32) + target_link_libraries(websocket-echo ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) +endif() diff --git a/test/websocket/stream.cpp b/test/websocket/stream.cpp index 1efee21e..14dae27a 100644 --- a/test/websocket/stream.cpp +++ b/test/websocket/stream.cpp @@ -8,15 +8,14 @@ // Test that header file is self-contained. #include -#include "../fail_stream.hpp" -#include "../string_stream.hpp" -#include "../yield_to.hpp" - #include "websocket_async_echo_peer.hpp" #include "websocket_sync_echo_peer.hpp" #include #include +#include +#include +#include #include #include #include diff --git a/test/websocket/websocket_echo.cpp b/test/websocket/websocket_echo.cpp index 2f965b20..0b4195df 100644 --- a/test/websocket/websocket_echo.cpp +++ b/test/websocket/websocket_echo.cpp @@ -19,7 +19,7 @@ #include "websocket_async_echo_peer.hpp" #include "websocket_sync_echo_peer.hpp" -#include "../sig_wait.hpp" +#include int main() { @@ -32,5 +32,5 @@ int main() beast::websocket::sync_echo_peer s2(true, endpoint_type{ address_type::from_string("127.0.0.1"), 6001 }); - sig_wait(); + beast::test::sig_wait(); }