Tidy up test sources:

Test support classes are moved to beast/extras/test.
This commit is contained in:
Vinnie Falco
2016-05-07 07:08:21 -04:00
parent 24612eba4c
commit 036c3098f3
16 changed files with 173 additions and 151 deletions

View File

@@ -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()

View File

@@ -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

View File

@@ -1,206 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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_FAIL_STREAM_HPP
#define BEAST_TEST_FAIL_STREAM_HPP
#include <beast/async_completion.hpp>
#include <beast/bind_handler.hpp>
#include <beast/error.hpp>
#include <beast/websocket/teardown.hpp>
#include <beast/detail/get_lowest_layer.hpp>
namespace beast {
namespace test {
/* 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.
*/
template<class NextLayer>
class fail_stream
{
error_code ec_;
std::size_t n_ = 0;
NextLayer next_layer_;
void
fail()
{
if(n_ > 0)
--n_;
if(! n_)
throw system_error{ec_};
}
bool
fail(error_code& ec)
{
if(n_ > 0)
--n_;
if(! n_)
{
ec = ec_;
return true;
}
return false;
}
public:
using next_layer_type =
typename std::remove_reference<NextLayer>::type;
using lowest_layer_type =
typename beast::detail::get_lowest_layer<
next_layer_type>::type;
fail_stream(fail_stream&&) = default;
fail_stream& operator=(fail_stream&&) = default;
template<class... Args>
explicit
fail_stream(std::size_t n, Args&&... args)
: ec_(boost::system::errc::make_error_code(
boost::system::errc::errc_t::invalid_argument))
, n_(n)
, next_layer_(std::forward<Args>(args)...)
{
}
next_layer_type&
next_layer()
{
return next_layer_;
}
lowest_layer_type&
lowest_layer()
{
return next_layer_.lowest_layer();
}
lowest_layer_type const&
lowest_layer() const
{
return next_layer_.lowest_layer();
}
boost::asio::io_service&
get_io_service()
{
return next_layer_.get_io_service();
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers)
{
fail();
return next_layer_.read_some(buffers);
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers, error_code& ec)
{
if(fail(ec))
return 0;
return next_layer_.read_some(buffers, ec);
}
template<class MutableBufferSequence, class ReadHandler>
typename async_completion<
ReadHandler, void(error_code)>::result_type
async_read_some(MutableBufferSequence const& buffers,
ReadHandler&& handler)
{
error_code ec;
if(fail(ec))
{
async_completion<
ReadHandler, void(error_code, std::size_t)
> completion(handler);
next_layer_.get_io_service().post(
bind_handler(completion.handler, ec, 0));
return completion.result.get();
}
return next_layer_.async_read_some(buffers,
std::forward<ReadHandler>(handler));
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers)
{
fail();
return next_layer_.write_some(buffers);
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers, error_code& ec)
{
if(fail(ec))
return 0;
return next_layer_.write_some(buffers, ec);
}
template<class ConstBufferSequence, class WriteHandler>
typename async_completion<
WriteHandler, void(error_code)>::result_type
async_write_some(ConstBufferSequence const& buffers,
WriteHandler&& handler)
{
error_code ec;
if(fail(ec))
{
async_completion<
WriteHandler, void(error_code, std::size_t)
> completion(handler);
next_layer_.get_io_service().post(
bind_handler(completion.handler, ec, 0));
return completion.result.get();
}
return next_layer_.async_write_some(buffers,
std::forward<WriteHandler>(handler));
}
};
template<class NextLayer>
void
teardown(fail_stream<NextLayer>& stream,
boost::system::error_code& ec)
{
websocket_helpers::call_teardown(stream.next_layer(), ec);
}
template<class NextLayer, class TeardownHandler>
void
async_teardown(fail_stream<NextLayer>& stream,
TeardownHandler&& handler)
{
websocket_helpers::call_async_teardown(
stream.next_layer(), std::forward<TeardownHandler>(handler));
}
} // test
} // beast
#endif

44
test/http/CMakeLists.txt Normal file
View File

@@ -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()

View File

@@ -8,11 +8,10 @@
// Test that header file is self-contained.
#include <beast/http/read.hpp>
#include "../fail_stream.hpp"
#include "../string_stream.hpp"
#include "../yield_to.hpp"
#include <beast/http/streambuf_body.hpp>
#include <beast/test/fail_stream.hpp>
#include <beast/test/string_stream.hpp>
#include <beast/test/yield_to.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/spawn.hpp>

View File

@@ -1,138 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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.
*/
//==============================================================================
#include <beast/unit_test/amount.hpp>
#include <beast/unit_test/global_suites.hpp>
#include <beast/unit_test/match.hpp>
#include <beast/unit_test/reporter.hpp>
#include <beast/unit_test/suite.hpp>
#include <beast/unit_test/debug_ostream.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
#ifdef _MSC_VER
# ifndef WIN32_LEAN_AND_MEAN // VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# undef WIN32_LEAN_AND_MEAN
# else
# include <windows.h>
# endif
#endif
#include <cstdlib>
namespace beast {
namespace unit_test {
std::string
prefix(suite_info const& s)
{
if (s.manual())
return "|M| ";
return " ";
}
template<class Log>
void
print(Log& log, suite_list const& c)
{
std::size_t manual = 0;
for(auto const& s : c)
{
log <<
prefix (s) <<
s.full_name();
if(s.manual())
++manual;
}
log <<
amount(c.size(), "suite") << " total, " <<
amount(manual, "manual suite")
;
}
template<class Log>
void
print(Log& log)
{
log << "------------------------------------------";
print(log, global_suites());
log << "------------------------------------------";
}
} // unit_test
} // beast
// Simple main used to produce stand
// alone executables that run unit tests.
int main(int ac, char const* av[])
{
using namespace std;
using namespace beast::unit_test;
#ifdef _MSC_VER
{
int flags = _CrtSetDbgFlag (_CRTDBG_REPORT_FLAG);
flags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag (flags);
}
#endif
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("help,h", "Produce a help message")
("print,r", "Print the list of available test suites")
("suites,s", po::value<string>(), "suites to run")
;
po::positional_options_description p;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
beast::debug_ostream log;
if(vm.count("help"))
{
log << desc;
}
else if(vm.count("print"))
{
print(log);
}
else
{
std::string suites;
if(vm.count("suites") > 0)
suites = vm["suites"].as<string>();
reporter r(log);
bool failed;
if(! suites.empty())
failed = r.run_each_if(global_suites(),
match_auto(suites));
else
failed = r.run_each(global_suites());
if (failed)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
}

View File

@@ -1,42 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
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 <boost/asio.hpp>
#include <condition_variable>
#include <mutex>
// 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

View File

@@ -1,114 +0,0 @@
//
// 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_STRING_STREAM_HPP
#define BEAST_TEST_STRING_STREAM_HPP
#include <beast/bind_handler.hpp>
#include <beast/error.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_service.hpp>
#include <string>
namespace beast {
namespace test {
// meets the requirements of AsyncStream, SyncStream
class string_stream
{
std::string s_;
boost::asio::io_service& ios_;
public:
string_stream(boost::asio::io_service& ios,
std::string s)
: s_(std::move(s))
, ios_(ios)
{
}
boost::asio::io_service&
get_io_service()
{
return ios_;
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers)
{
error_code ec;
auto const n = read_some(buffers, ec);
if(ec)
throw boost::system::system_error{ec};
return n;
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers,
error_code& ec)
{
auto const n = boost::asio::buffer_copy(
buffers, boost::asio::buffer(s_));
s_.erase(0, n);
return n;
}
template<class MutableBufferSequence, class ReadHandler>
typename async_completion<ReadHandler,
void(error_code, std::size_t)>::result_type
async_read_some(MutableBufferSequence const& buffers,
ReadHandler&& handler)
{
auto const n = boost::asio::buffer_copy(
buffers, boost::asio::buffer(s_));
s_.erase(0, n);
async_completion<ReadHandler,
void(error_code, std::size_t)> completion(handler);
ios_.post(bind_handler(
completion.handler, error_code{}, n));
return completion.result.get();
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers)
{
error_code ec;
auto const n = write_some(buffers, ec);
if(ec)
throw boost::system::system_error{ec};
return n;
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers,
error_code&)
{
return boost::asio::buffer_size(buffers);
}
template<class ConstBuffeSequence, class WriteHandler>
typename async_completion<WriteHandler,
void(error_code, std::size_t)>::result_type
async_write_some(ConstBuffeSequence const& buffers,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code, std::size_t)> completion(handler);
ios_.post(bind_handler(completion.handler,
error_code{}, boost::asio::buffer_size(buffers)));
return completion.result.get();
}
};
} // test
} // beast
#endif

View File

@@ -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()

View File

@@ -8,15 +8,14 @@
// Test that header file is self-contained.
#include <beast/websocket/stream.hpp>
#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 <beast/streambuf.hpp>
#include <beast/to_string.hpp>
#include <beast/test/fail_stream.hpp>
#include <beast/test/string_stream.hpp>
#include <beast/test/yield_to.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>

View File

@@ -19,7 +19,7 @@
#include "websocket_async_echo_peer.hpp"
#include "websocket_sync_echo_peer.hpp"
#include "../sig_wait.hpp"
#include <beast/test/sig_wait.hpp>
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();
}

View File

@@ -1,86 +0,0 @@
//
// 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_ENABLE_YIELD_TO_HPP
#define BEAST_TEST_ENABLE_YIELD_TO_HPP
#include <boost/asio/io_service.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/optional.hpp>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>
namespace beast {
namespace test {
// Mix-in to support tests using coroutines
class enable_yield_to
{
protected:
boost::asio::io_service ios_;
private:
boost::optional<boost::asio::io_service::work> work_;
std::thread thread_;
std::mutex m_;
std::condition_variable cv_;
bool running_ = false;;
protected:
using yield_context =
boost::asio::yield_context;
enable_yield_to()
: work_(ios_)
, thread_([&]
{
ios_.run();
}
)
{
}
~enable_yield_to()
{
work_ = boost::none;
thread_.join();
}
// Run a function in a coroutine
template<class Function>
void
yield_to(Function&& f);
};
template<class Function>
void
enable_yield_to::yield_to(Function&& f)
{
{
std::lock_guard<std::mutex> lock(m_);
running_ = true;
}
boost::asio::spawn(ios_,
[&](boost::asio::yield_context do_yield)
{
f(do_yield);
std::lock_guard<std::mutex> lock(m_);
running_ = false;
cv_.notify_all();
}
, boost::coroutines::attributes(2 * 1024 * 1024));
std::unique_lock<std::mutex> lock(m_);
cv_.wait(lock, [&]{ return ! running_; });
}
} // test
} // beast
#endif