Testing and faking ASIO I/O objects
How is people around here testing code which uses ASIO? I guess something like GIVEN("SOMETHING THAT STARTS BY DOING A NAME RESOLVER IS STARTED") fakeResolver.async_resolve("www.google.com", "80", HandlerCallingConnect); // This stores the completion handler (and an executor_work_guard) internally WHEN("THE RESOLVER RETURNS WITHOUT ERROR") fakeResolver.completeWithFakeResult("1.2.3.4", error_code()); // This post() the completion handler with the arguments you give it here io_context.poll(); THEN("A CONNECTION IS ATTEMPTED") EXPECT_CALL(fakeSocket, async_connect()); would be nice. But the "stores the completion handler" part doesn't look that simple. I have found some attempts online, but they seems to store the completion handlers into a vector of std::function and post them through std::bind, so possibly using the wrong executor, etc. There is any good set of ASIO I/O object fakes available?
On Wed, Aug 29, 2018 at 2:03 PM Cristian Morales Vega via Boost-users
There is any good set of ASIO I/O object fakes available?
Beast experimental interfaces provide testing facilities which meet the requirements of the following Asio concepts: SyncReadStream SyncWriteStream AsyncReadStream AsyncWriteStream This is provided by the boost::beast::test::stream class, which works with the test::fail_counter class to allow for automated failure testing. Docs: https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__b... https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__b... https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__b... https://www.boost.org/doc/libs/1_68_0/libs/beast/doc/html/beast/ref/boost__b... Includes: https://github.com/boostorg/beast/tree/develop/include/boost/beast/experimen... The Beast unit tests make heavy use of the test::stream class. Example: https://github.com/boostorg/beast/blob/1da229a27c6f0539d422bcedbcf47cfe25171... If you have questions feel free to open an issue. Regards
On 31 August 2018 at 02:03, Vinnie Falco via Boost-users
On Wed, Aug 29, 2018 at 2:03 PM Cristian Morales Vega via Boost-users
wrote: There is any good set of ASIO I/O object fakes available?
Beast experimental interfaces provide testing facilities which meet the requirements of the following Asio concepts:
SyncReadStream SyncWriteStream AsyncReadStream AsyncWriteStream
This is provided by the boost::beast::test::stream class, which works with the test::fail_counter class to allow for automated failure testing.
This is really useful! I guess the main things people will miss are: - Fake Timer: it must be extreme common, to implement network timeouts - Fake Resolver/Acceptor/Connect to Protocol::endpoint But it also gives an example of how to implement fakes, so people should be able to do so for those extra things. I see you don't allocate read_op using the handler associated allocator. Is this on purpose? I don't see any problem with this but I also fail to see other, real, problems when looking at ASIO code and was afraid of doing it myself.
participants (2)
-
Cristian Morales Vega
-
Vinnie Falco