mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Tidy up test sources:
Test support classes are moved to beast/extras/test.
This commit is contained in:
206
extras/beast/test/fail_stream.hpp
Normal file
206
extras/beast/test/fail_stream.hpp
Normal file
@@ -0,0 +1,206 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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
|
||||
36
extras/beast/test/sig_wait.hpp
Normal file
36
extras/beast/test/sig_wait.hpp
Normal file
@@ -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 <boost/asio.hpp>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
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
|
||||
119
extras/beast/test/string_stream.hpp
Normal file
119
extras/beast/test/string_stream.hpp
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// 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 {
|
||||
|
||||
/** 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_;
|
||||
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
|
||||
101
extras/beast/test/yield_to.hpp
Normal file
101
extras/beast/test/yield_to.hpp
Normal file
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// 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_YIELD_TO_HPP
|
||||
#define BEAST_TEST_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 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:
|
||||
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:
|
||||
/// The type of yield context passed to functions.
|
||||
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.
|
||||
|
||||
This call will block until the coroutine terminates.
|
||||
|
||||
Function will be called with this signature:
|
||||
|
||||
@code
|
||||
void f(yield_context);
|
||||
@endcode
|
||||
*/
|
||||
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
|
||||
138
extras/beast/unit_test/main.cpp
Normal file
138
extras/beast/unit_test/main.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user