Add test::error to experimental

This commit is contained in:
Vinnie Falco
2018-05-01 12:39:47 -07:00
parent 292801fef7
commit 59f01a158b
14 changed files with 200 additions and 125 deletions

View File

@@ -2,6 +2,7 @@ Version 170:
* Add flat_stream to experimental
* Add ssl_stream to experimental
* Add test::error to experimental
--------------------------------------------------------------------------------

View File

@@ -0,0 +1,60 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_DETAIL_ERROR_HPP
#define BOOST_BEAST_TEST_DETAIL_ERROR_HPP
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/string.hpp>
namespace boost {
namespace beast {
namespace test {
enum class error;
} // test
} // beast
namespace system {
template<>
struct is_error_code_enum<beast::test::error>
{
static bool const value = true;
};
} // system
namespace beast {
namespace test {
namespace detail {
class error_codes : public error_category
{
public:
const char*
name() const noexcept override;
std::string
message(int ev) const override;
error_condition
default_error_condition(int ev) const noexcept override;
};
} // detail
error_code
make_error_code(error e);
} // test
} // beast
} // boost
#endif

View File

@@ -0,0 +1,37 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_ERROR_HPP
#define BOOST_BEAST_TEST_ERROR_HPP
#include <boost/beast/core/error.hpp>
#include <boost/beast/experimental/test/detail/error.hpp>
namespace boost {
namespace beast {
namespace test {
/// Error codes returned from unit testing algorithms
enum class error
{
/** The test stream generated a simulated testing error
This error is returned by the test @ref stream when it
generates a simulated error.
*/
test_failure = 1
};
} // test
} // beast
} // boost
#include <boost/beast/experimental/test/impl/error.ipp>
#endif

View File

@@ -0,0 +1,62 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_TEST_IMPL_ERROR_IPP
#define BOOST_BEAST_TEST_IMPL_ERROR_IPP
namespace boost {
namespace beast {
namespace test {
namespace detail {
inline
const char*
error_codes::
name() const noexcept
{
return "boost.beast.test";
}
inline
std::string
error_codes::
message(int ev) const
{
switch(static_cast<error>(ev))
{
default:
case error::test_failure: return "The test stream generated a simulated error";
}
}
inline
error_condition
error_codes::
default_error_condition(int ev) const noexcept
{
return error_condition{ev, *this};
}
} // detail
inline
error_code
make_error_code(error e)
{
static detail::error_codes const cat{};
return error_code{static_cast<
std::underlying_type<error>::type>(e), cat};
}
} // test
} // beast
} // boost
#endif

View File

@@ -7,8 +7,8 @@
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_ERROR_IPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_ERROR_IPP
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_ERROR_HPP
#define BOOST_BEAST_WEBSOCKET_DETAIL_ERROR_HPP
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/string.hpp>

View File

@@ -139,7 +139,7 @@ public:
test::stream&, multi_buffer> srs(ts);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
@@ -158,7 +158,7 @@ public:
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
boost::asio::read(srs, buffer(&s[0], s.size()), ec);
if(! ec)
{
@@ -176,7 +176,7 @@ public:
test::stream&, multi_buffer> srs(ts);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)
@@ -196,7 +196,7 @@ public:
srs.capacity(3);
srs.buffer().commit(buffer_copy(
srs.buffer().prepare(5), buffer("Hello", 5)));
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
boost::asio::async_read(
srs, buffer(&s[0], s.size()), do_yield[ec]);
if(! ec)

View File

@@ -17,6 +17,7 @@ add_executable (tests-beast-experimental
${EXTRAS_FILES}
${TEST_MAIN}
Jamfile
error.cpp
flat_stream.cpp
ssl_stream.cpp
)

View File

@@ -8,6 +8,7 @@
#
local SOURCES =
error.cpp
flat_stream.cpp
ssl_stream.cpp
;

View File

@@ -0,0 +1,11 @@
//
// Copyright (c) 2016-2017 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)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <boost/beast/experimental/test/error.hpp>

View File

@@ -51,7 +51,7 @@ public:
test::fail_counter fc(n);
test::stream ts{ioc_, fc};
test_parser<isRequest> p(fc);
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
ts.close_remote();
read(ts, b, p, ec);
if(! ec)
@@ -68,7 +68,7 @@ public:
test::stream ts{ioc_, fc,
std::string(s + pre, len - pre)};
test_parser<isRequest> p(fc);
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
ts.close_remote();
read(ts, b, p, ec);
if(! ec)
@@ -83,7 +83,7 @@ public:
test::fail_counter fc(n);
test::stream ts{ioc_, fc};
test_parser<isRequest> p(fc);
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
ts.close_remote();
async_read(ts, b, p, do_yield[ec]);
if(! ec)
@@ -98,7 +98,7 @@ public:
test::fail_counter fc(n);
test::stream ts{ioc_, fc};
test_parser<isRequest> p(fc);
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
ts.close_remote();
async_read_header(ts, b, p, do_yield[ec]);
if(! ec)
@@ -115,7 +115,7 @@ public:
test::stream ts(ioc_, fc,
std::string{s + pre, len - pre});
test_parser<isRequest> p(fc);
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
ts.close_remote();
async_read(ts, b, p, do_yield[ec]);
if(! ec)
@@ -178,7 +178,7 @@ public:
"10\r\n"
"****************\r\n"
"0\r\n\r\n";
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
flat_static_buffer<10> b;
request<string_body> req;
read(c, b, req, ec);
@@ -286,7 +286,7 @@ public:
"\r\n"
};
request<dynamic_body> m;
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
multi_buffer b;
read(ts, b, m, ec);
if(! ec)
@@ -305,7 +305,7 @@ public:
"\r\n"
};
request<dynamic_body> m;
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
multi_buffer b;
async_read(c, b, m, do_yield[ec]);
if(! ec)
@@ -324,7 +324,7 @@ public:
"\r\n"
};
request_parser<dynamic_body> m;
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
multi_buffer b;
async_read_some(c, b, m, do_yield[ec]);
if(! ec)
@@ -435,7 +435,7 @@ public:
for(std::size_t n = 1; n < s.size() - 1; ++n)
{
Parser p;
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
flat_buffer b;
test::stream ts{ioc_};
ostream(ts.buffer()) << s;

View File

@@ -390,7 +390,7 @@ public:
m.set(field::user_agent, "test");
m.set(field::transfer_encoding, "chunked");
m.body() = "*****";
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
write(ts, m, ec);
if(! ec)
{
@@ -421,7 +421,7 @@ public:
m.set(field::user_agent, "test");
m.set(field::transfer_encoding, "chunked");
m.body() = "*****";
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
async_write(ts, m, do_yield[ec]);
if(! ec)
{
@@ -453,7 +453,7 @@ public:
m.set(field::connection, "keep-alive");
m.set(field::content_length, "5");
m.body() = "*****";
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
write(ts, m, ec);
if(! ec)
{
@@ -480,7 +480,7 @@ public:
m.set(field::connection, "keep-alive");
m.set(field::content_length, "5");
m.body() = "*****";
error_code ec = test::error::fail_error;
error_code ec = test::error::test_failure;
async_write(ts, m, do_yield[ec]);
if(! ec)
{

View File

@@ -464,7 +464,7 @@ public:
}
catch(system_error const& se)
{
if(se.code() == test::error::fail_error)
if(se.code() == test::error::test_failure)
throw;
BEAST_EXPECTS(se.code().category() ==
zlib::detail::get_error_category(),

View File

@@ -296,7 +296,7 @@ public:
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == test::error::fail_error,
se.code() == test::error::test_failure,
se.code().message());
}
}
@@ -350,7 +350,7 @@ public:
{
ts.close();
if( ! BEAST_EXPECTS(
ec == test::error::fail_error,
ec == test::error::test_failure,
ec.message()))
BOOST_THROW_EXCEPTION(system_error{ec});
continue;
@@ -364,7 +364,7 @@ public:
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == test::error::fail_error,
se.code() == test::error::test_failure,
se.code().message());
}
catch(std::exception const& e)

View File

@@ -11,105 +11,17 @@
#define BOOST_BEAST_TEST_FAIL_COUNTER_HPP
#include <boost/beast/core/error.hpp>
#include <boost/beast/experimental/test/error.hpp>
#include <boost/throw_exception.hpp>
namespace boost {
namespace beast {
namespace test {
enum class error
{
fail_error = 1
};
namespace detail {
class fail_error_category : public boost::system::error_category
{
public:
const char*
name() const noexcept override
{
return "test";
}
std::string
message(int ev) const override
{
switch(static_cast<error>(ev))
{
default:
case error::fail_error:
return "test error";
}
}
boost::system::error_condition
default_error_condition(int ev) const noexcept override
{
return boost::system::error_condition{ev, *this};
}
bool
equivalent(int ev,
boost::system::error_condition const& condition
) const noexcept override
{
return condition.value() == ev &&
&condition.category() == this;
}
bool
equivalent(error_code const& error, int ev) const noexcept override
{
return error.value() == ev &&
&error.category() == this;
}
};
inline
boost::system::error_category const&
get_error_category()
{
static fail_error_category const cat{};
return cat;
}
} // detail
inline
error_code
make_error_code(error ev)
{
return error_code{
static_cast<std::underlying_type<error>::type>(ev),
detail::get_error_category()};
}
/** An error code with an error set on default construction
Default constructed versions of this object will have
an error code set right away. This helps tests find code
which forgets to clear the error code on success.
*/
struct fail_error_code : error_code
{
fail_error_code()
: error_code(make_error_code(error::fail_error))
{
}
template<class Arg0, class... ArgN>
fail_error_code(Arg0&& arg0, ArgN&&... argn)
: error_code(arg0, std::forward<ArgN>(argn)...)
{
}
};
/** A countdown to simulated failure.
On the Nth operation, the class will fail with the specified
error code, or the default error code of @ref error::fail_error.
error code, or the default error code of @ref error::test_failure.
*/
class fail_counter
{
@@ -126,7 +38,7 @@ public:
*/
explicit
fail_counter(std::size_t n,
error_code ev = make_error_code(error::fail_error))
error_code ev = make_error_code(error::test_failure))
: n_(n)
, ec_(ev)
{
@@ -169,14 +81,4 @@ public:
} // beast
} // boost
namespace boost {
namespace system {
template<>
struct is_error_code_enum<beast::test::error>
{
static bool const value = true;
};
} // system
} // boost
#endif