Mock Stream Testing
Unit testing protocol code with mock streams and error injection.
What This Example Shows
-
Using connected
test::streampairs frommake_stream_pair -
Error injection with
fuse -
Synchronous testing with
run_blocking
Source Code
#include <boost/capy.hpp>
#include <boost/capy/test/stream.hpp>
#include <boost/capy/test/fuse.hpp>
#include <boost/capy/io/any_stream.hpp>
#include <boost/capy/cond.hpp>
#include <iostream>
#include <cassert>
#include <cctype>
namespace capy = boost::capy;
// A simple protocol: read until newline, echo back uppercase
// Takes any_stream& so the function is transport-independent
capy::task<bool> echo_line_uppercase(capy::any_stream& stream)
{
std::string line;
char c;
// Read character by character until newline
while (true)
{
// ec: std::error_code, n: std::size_t
auto [ec, n] = co_await stream.read_some(capy::mutable_buffer(&c, 1));
if (n > 0)
{
if (c == '\n')
break;
line += static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
}
if (ec == capy::cond::eof)
break;
if (ec)
co_return false;
}
line += '\n';
// Echo uppercase - loop until all bytes written
std::size_t written = 0; // std::size_t - total bytes written
while (written < line.size())
{
// wec: std::error_code, wn: std::size_t
auto [wec, wn] = co_await stream.write_some(
capy::const_buffer(line.data() + written, line.size() - written));
written += wn;
if (wec)
co_return false;
}
co_return true;
}
void test_happy_path()
{
std::cout << "Test: happy path\n";
auto [a, b] = capy::test::make_stream_pair();
b.provide("hello\n");
// Wrap one end in any_stream using pointer construction for reference semantics
capy::any_stream stream{&a}; // any_stream
bool result = false; // bool
capy::test::run_blocking([&](bool r) { result = r; })(echo_line_uppercase(stream));
assert(result == true);
assert(b.data() == "HELLO\n");
std::cout << " PASSED\n";
}
void test_partial_reads()
{
std::cout << "Test: partial reads (1 byte at a time)\n";
auto [a, b] = capy::test::make_stream_pair();
a.set_max_read_size(1);
b.provide("hi\n");
capy::any_stream stream{&a}; // any_stream
bool result = false; // bool
capy::test::run_blocking([&](bool r) { result = r; })(echo_line_uppercase(stream));
assert(result == true);
assert(b.data() == "HI\n");
std::cout << " PASSED\n";
}
void test_with_error_injection()
{
std::cout << "Test: error injection\n";
int success_count = 0;
int error_count = 0;
// fuse::armed runs the test repeatedly, failing at each
// operation point until all paths are covered
capy::test::fuse f; // test::fuse
auto r = f.armed([&](capy::test::fuse&) -> capy::task<> { // fuse::result
auto [a, b] = capy::test::make_stream_pair(f);
b.provide("test\n");
capy::any_stream stream{&a}; // any_stream
// Run the protocol - fuse injects errors at each step
bool result = co_await echo_line_uppercase(stream); // bool
// Either succeeds with correct output, or fails cleanly
if (result)
{
++success_count;
assert(b.data() == "TEST\n");
}
else
{
++error_count;
}
});
// Verify that fuse testing exercised both paths
std::cout << " Runs: " << (success_count + error_count)
<< " (success=" << success_count
<< ", error=" << error_count << ")\n";
assert(r.success);
assert(success_count > 0); // At least one successful run
assert(error_count > 0); // At least one error-injected run
std::cout << " PASSED (all error paths tested)\n";
}
int main()
{
test_happy_path();
test_partial_reads();
test_with_error_injection();
std::cout << "\nAll tests passed!\n";
return 0;
}
Build
add_executable(capy_example_mock_stream_testing mock_stream_testing.cpp)
target_link_libraries(capy_example_mock_stream_testing PRIVATE Boost::capy)
Walkthrough
Mock Streams
capy::test::fuse f; // test::fuse
auto [a, b] = capy::test::make_stream_pair(f); // connected test::stream pair
b.provide("hello\n"); // supply read input on one end
test::stream is a bidirectional mock that satisfies both ReadStream and WriteStream:
-
Streams are obtained in connected pairs from
make_stream_pair(f); the sharedfuseenables error injection -
provide(data): Supplies data for the peer’s reads -
data(): Returns data the peer wrote that this end has not yet read -
set_max_read_size(n): Limits bytes returned per read, simulating chunked delivery
Type-Erased Streams
// Wrap one end in any_stream using pointer construction for reference semantics
capy::any_stream stream{&a}; // any_stream
Use pointer construction (&a) so the any_stream wrapper references the stream end without taking ownership. This allows inspecting b.data() after operations.
Synchronous Testing
bool result = false; // bool
capy::test::run_blocking([&](bool r) { result = r; })(echo_line_uppercase(stream));
run_blocking executes a coroutine synchronously, blocking until complete. Pass a completion handler to capture the result.
Error Injection
capy::test::fuse f; // test::fuse
auto r = f.armed([&](capy::test::fuse&) -> capy::task<> {
auto [a, b] = capy::test::make_stream_pair(f); // connected test::stream pair
// ... run test ...
co_return;
});
fuse::armed runs the test function repeatedly.
Each run moves the injected failure to a later operation, until a run completes with no failure.
It does this in two phases: first the failures arrive as error codes, then as exceptions.
In this program, an error-code run returns false and increments error.
The clean run that ends each phase increments success, so success is 2.
An exception leaves the test body before either counter changes, so the exception runs do not appear in Runs.
Output
Test: happy path
PASSED
Test: partial reads (1 byte at a time)
PASSED
Test: error injection
Runs: 9 (success=2, error=7)
PASSED (all error paths tested)
All tests passed!
Exercises
-
Add a test for EOF handling (what if input doesn’t end with newline?)
-
Test with different max_read_size values
-
Add a test that checks the output after an injected write error, using the shared
fuse
Next Steps
-
Type-Erased Echo — Compilation firewall pattern