From 59f01a158bfbde38fbd257760706dbf4a224acd2 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 1 May 2018 12:39:47 -0700 Subject: [PATCH] Add test::error to experimental --- CHANGELOG.md | 1 + .../beast/experimental/test/detail/error.hpp | 60 ++++++++++ .../boost/beast/experimental/test/error.hpp | 37 +++++++ .../beast/experimental/test/impl/error.ipp | 62 +++++++++++ .../boost/beast/websocket/detail/error.hpp | 4 +- test/beast/core/buffered_read_stream.cpp | 8 +- test/beast/experimental/CMakeLists.txt | 1 + test/beast/experimental/Jamfile | 1 + test/beast/experimental/error.cpp | 11 ++ test/beast/http/read.cpp | 20 ++-- test/beast/http/write.cpp | 8 +- test/beast/websocket/read1.cpp | 2 +- test/beast/websocket/test.hpp | 6 +- .../include/boost/beast/test/fail_counter.hpp | 104 +----------------- 14 files changed, 200 insertions(+), 125 deletions(-) create mode 100644 include/boost/beast/experimental/test/detail/error.hpp create mode 100644 include/boost/beast/experimental/test/error.hpp create mode 100644 include/boost/beast/experimental/test/impl/error.ipp create mode 100644 test/beast/experimental/error.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ddbffea..d661f80d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version 170: * Add flat_stream to experimental * Add ssl_stream to experimental +* Add test::error to experimental -------------------------------------------------------------------------------- diff --git a/include/boost/beast/experimental/test/detail/error.hpp b/include/boost/beast/experimental/test/detail/error.hpp new file mode 100644 index 00000000..d8ffbc13 --- /dev/null +++ b/include/boost/beast/experimental/test/detail/error.hpp @@ -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 +#include + +namespace boost { + +namespace beast { +namespace test { +enum class error; +} // test +} // beast + +namespace system { +template<> +struct is_error_code_enum +{ + 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 diff --git a/include/boost/beast/experimental/test/error.hpp b/include/boost/beast/experimental/test/error.hpp new file mode 100644 index 00000000..99ac6a9d --- /dev/null +++ b/include/boost/beast/experimental/test/error.hpp @@ -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 +#include + +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 + +#endif diff --git a/include/boost/beast/experimental/test/impl/error.ipp b/include/boost/beast/experimental/test/impl/error.ipp new file mode 100644 index 00000000..285947df --- /dev/null +++ b/include/boost/beast/experimental/test/impl/error.ipp @@ -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(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::type>(e), cat}; +} + +} // test +} // beast +} // boost + +#endif diff --git a/include/boost/beast/websocket/detail/error.hpp b/include/boost/beast/websocket/detail/error.hpp index 57b837bd..1cf63fc6 100644 --- a/include/boost/beast/websocket/detail/error.hpp +++ b/include/boost/beast/websocket/detail/error.hpp @@ -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 #include diff --git a/test/beast/core/buffered_read_stream.cpp b/test/beast/core/buffered_read_stream.cpp index bbdbb5a0..055ed29c 100644 --- a/test/beast/core/buffered_read_stream.cpp +++ b/test/beast/core/buffered_read_stream.cpp @@ -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) diff --git a/test/beast/experimental/CMakeLists.txt b/test/beast/experimental/CMakeLists.txt index 8180577a..801b9f93 100644 --- a/test/beast/experimental/CMakeLists.txt +++ b/test/beast/experimental/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable (tests-beast-experimental ${EXTRAS_FILES} ${TEST_MAIN} Jamfile + error.cpp flat_stream.cpp ssl_stream.cpp ) diff --git a/test/beast/experimental/Jamfile b/test/beast/experimental/Jamfile index 849265f5..fd53dae4 100644 --- a/test/beast/experimental/Jamfile +++ b/test/beast/experimental/Jamfile @@ -8,6 +8,7 @@ # local SOURCES = + error.cpp flat_stream.cpp ssl_stream.cpp ; diff --git a/test/beast/experimental/error.cpp b/test/beast/experimental/error.cpp new file mode 100644 index 00000000..b5b8287a --- /dev/null +++ b/test/beast/experimental/error.cpp @@ -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 diff --git a/test/beast/http/read.cpp b/test/beast/http/read.cpp index b207ab59..04e9e504 100644 --- a/test/beast/http/read.cpp +++ b/test/beast/http/read.cpp @@ -51,7 +51,7 @@ public: test::fail_counter fc(n); test::stream ts{ioc_, fc}; test_parser 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 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 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 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 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 req; read(c, b, req, ec); @@ -286,7 +286,7 @@ public: "\r\n" }; request 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 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 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; diff --git a/test/beast/http/write.cpp b/test/beast/http/write.cpp index 8ac1b1c5..d981cac1 100644 --- a/test/beast/http/write.cpp +++ b/test/beast/http/write.cpp @@ -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) { diff --git a/test/beast/websocket/read1.cpp b/test/beast/websocket/read1.cpp index 6af7abec..befdcfd7 100644 --- a/test/beast/websocket/read1.cpp +++ b/test/beast/websocket/read1.cpp @@ -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(), diff --git a/test/beast/websocket/test.hpp b/test/beast/websocket/test.hpp index 6fe1f141..79dca2ce 100644 --- a/test/beast/websocket/test.hpp +++ b/test/beast/websocket/test.hpp @@ -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) diff --git a/test/extras/include/boost/beast/test/fail_counter.hpp b/test/extras/include/boost/beast/test/fail_counter.hpp index 6850a3ca..a783d61c 100644 --- a/test/extras/include/boost/beast/test/fail_counter.hpp +++ b/test/extras/include/boost/beast/test/fail_counter.hpp @@ -11,105 +11,17 @@ #define BOOST_BEAST_TEST_FAIL_COUNTER_HPP #include +#include #include 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(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::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 - fail_error_code(Arg0&& arg0, ArgN&&... argn) - : error_code(arg0, std::forward(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 -{ - static bool const value = true; -}; -} // system -} // boost - #endif