Add test::fail_count to experimental

This commit is contained in:
Vinnie Falco
2018-05-02 08:32:09 -07:00
parent 59f01a158b
commit e4c2ae6384
18 changed files with 208 additions and 149 deletions
@@ -1,84 +0,0 @@
//
// 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_FAIL_COUNTER_HPP
#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 {
/** 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::test_failure.
*/
class fail_counter
{
std::size_t n_;
std::size_t i_ = 0;
error_code ec_;
public:
fail_counter(fail_counter&&) = default;
/** Construct a counter.
@param The 0-based index of the operation to fail on or after.
*/
explicit
fail_counter(std::size_t n,
error_code ev = make_error_code(error::test_failure))
: n_(n)
, ec_(ev)
{
}
/// Returns the fail index
std::size_t
count() const
{
return n_;
}
/// Throw an exception on the Nth failure
void
fail()
{
if(i_ < n_)
++i_;
if(i_ == n_)
BOOST_THROW_EXCEPTION(system_error{ec_});
}
/// Set an error code on the Nth failure
bool
fail(error_code& ec)
{
if(i_ < n_)
++i_;
if(i_ == n_)
{
ec = ec_;
return true;
}
ec.assign(0, ec.category());
return false;
}
};
} // test
} // beast
} // boost
#endif
@@ -16,7 +16,7 @@
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/type_traits.hpp>
#include <boost/beast/websocket/teardown.hpp>
#include <boost/beast/test/fail_counter.hpp>
#include <boost/beast/experimental/test/fail_count.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/executor_work_guard.hpp>
@@ -73,7 +73,7 @@ class stream
std::unique_ptr<read_op> op;
boost::asio::io_context& ioc;
status code = status::ok;
fail_counter* fc = nullptr;
fail_count* fc = nullptr;
std::size_t nread = 0;
std::size_t nwrite = 0;
std::size_t read_max =
@@ -89,7 +89,7 @@ class stream
explicit
state(
boost::asio::io_context& ioc_,
fail_counter* fc_)
fail_count* fc_)
: ioc(ioc_)
, fc(fc_)
{
@@ -170,7 +170,7 @@ public:
/// Constructor
stream(
boost::asio::io_context& ioc,
fail_counter& fc)
fail_count& fc)
: in_(std::make_shared<state>(ioc, &fc))
{
}
@@ -191,7 +191,7 @@ public:
/// Constructor
stream(
boost::asio::io_context& ioc,
fail_counter& fc,
fail_count& fc,
string_view s)
: in_(std::make_shared<state>(ioc, &fc))
{