]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/libs/beast/test/beast/websocket/close.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / beast / test / beast / websocket / close.cpp
index 0e5f26c88e1fd787c2f7bdd519df3ab9692c41c1..1b891b339b5d11c8234c11a086ae731a6df6397a 100644 (file)
@@ -1,5 +1,5 @@
 //
-// Copyright (w) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
+// Copyright (c) 2016-2019 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)
 // Test that header file is self-contained.
 #include <boost/beast/websocket/stream.hpp>
 
+#include <boost/beast/_experimental/test/stream.hpp>
+#include <boost/beast/_experimental/test/tcp.hpp>
 #include "test.hpp"
 
-#include <boost/asio/io_service.hpp>
+#include <boost/asio/io_context.hpp>
 #include <boost/asio/strand.hpp>
 
 namespace boost {
@@ -65,7 +67,7 @@ public:
             catch(system_error const& se)
             {
                 BEAST_EXPECTS(
-                    se.code() == boost::asio::error::operation_aborted,
+                    se.code() == net::error::operation_aborted,
                     se.code().message());
             }
         }
@@ -169,15 +171,121 @@ public:
     }
 
     void
-    testSuspend()
+    testTimeout()
     {
-        using boost::asio::buffer;
+        using tcp = net::ip::tcp;
+
+        net::io_context ioc;
+
+        // success
+
+        {
+            stream<tcp::socket> ws1(ioc);
+            stream<tcp::socket> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.async_close({}, test::success_handler());
+            ws2.async_close({}, test::success_handler());
+            test::run(ioc);
+        }
+
+        {
+            stream<test::stream> ws1(ioc);
+            stream<test::stream> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.async_close({}, test::success_handler());
+            ws2.async_close({}, test::success_handler());
+            test::run(ioc);
+        }
+
+        // success, timeout enabled
+
+        {
+            stream<tcp::socket> ws1(ioc);
+            stream<tcp::socket> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.set_option(stream_base::timeout{
+                std::chrono::milliseconds(50),
+                stream_base::none(),
+                false});
+            ws1.async_close({}, test::success_handler());
+            ws2.async_close({}, test::success_handler());
+            test::run(ioc);
+        }
+
+        {
+            stream<test::stream> ws1(ioc);
+            stream<test::stream> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.set_option(stream_base::timeout{
+                std::chrono::milliseconds(50),
+                stream_base::none(),
+                false});
+            ws1.async_close({}, test::success_handler());
+            ws2.async_close({}, test::success_handler());
+            test::run(ioc);
+        }
 
+        // timeout
+
+        {
+            stream<tcp::socket> ws1(ioc);
+            stream<tcp::socket> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.set_option(stream_base::timeout{
+                std::chrono::milliseconds(50),
+                stream_base::none(),
+                false});
+            ws1.async_close({}, test::fail_handler(
+                beast::error::timeout));
+            test::run(ioc);
+        }
+
+        {
+            stream<test::stream> ws1(ioc);
+            stream<test::stream> ws2(ioc);
+            test::connect(ws1.next_layer(), ws2.next_layer());
+            ws1.async_handshake("test", "/", test::success_handler());
+            ws2.async_accept(test::success_handler());
+            test::run(ioc);
+
+            ws1.set_option(stream_base::timeout{
+                std::chrono::milliseconds(50),
+                stream_base::none(),
+                false});
+            ws1.async_close({}, test::fail_handler(
+                beast::error::timeout));
+            test::run(ioc);
+        }
+    }
+
+    void
+    testSuspend()
+    {
         // suspend on ping
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -190,7 +298,7 @@ public:
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                 });
-            BEAST_EXPECT(ws.wr_block_.is_locked());
+            BEAST_EXPECT(ws.impl_->wr_block.is_locked());
             BEAST_EXPECT(count == 0);
             ws.async_close({},
                 [&](error_code ec)
@@ -206,10 +314,10 @@ public:
         });
 
         // suspend on write
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -223,7 +331,7 @@ public:
                             system_error{ec});
                     BEAST_EXPECT(n == 1);
                 });
-            BEAST_EXPECT(ws.wr_block_.is_locked());
+            BEAST_EXPECT(ws.impl_->wr_block.is_locked());
             BEAST_EXPECT(count == 0);
             ws.async_close({},
                 [&](error_code ec)
@@ -239,10 +347,11 @@ public:
         });
 
         // suspend on read ping + message
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -250,7 +359,6 @@ public:
             ws.next_layer().append(string_view{
                 "\x89\x00" "\x81\x01*", 5});
             std::size_t count = 0;
-            multi_buffer b;
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
@@ -259,7 +367,7 @@ public:
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                 });
-            while(! ws.wr_block_.is_locked())
+            while(! ws.impl_->wr_block.is_locked())
             {
                 ioc.run_one();
                 if(! BEAST_EXPECT(! ioc.stopped()))
@@ -280,10 +388,11 @@ public:
         });
 
         // suspend on read bad message
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -291,7 +400,6 @@ public:
             ws.next_layer().append(string_view{
                 "\x09\x00", 2});
             std::size_t count = 0;
-            multi_buffer b;
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
@@ -300,7 +408,7 @@ public:
                             system_error{ec});
                     BEAST_EXPECT(++count == 1);
                 });
-            while(! ws.wr_block_.is_locked())
+            while(! ws.impl_->wr_block.is_locked())
             {
                 ioc.run_one();
                 if(! BEAST_EXPECT(! ioc.stopped()))
@@ -310,7 +418,7 @@ public:
             ws.async_close({},
                 [&](error_code ec)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     BEAST_EXPECT(++count == 2);
@@ -321,10 +429,11 @@ public:
         });
 
         // suspend on read close #1
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -332,7 +441,6 @@ public:
             ws.next_layer().append(string_view{
                 "\x88\x00", 2});
             std::size_t count = 0;
-            multi_buffer b;
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
@@ -341,7 +449,7 @@ public:
                             system_error{ec});
                     BEAST_EXPECT(++count == 1);
                 });
-            while(! ws.wr_block_.is_locked())
+            while(! ws.impl_->wr_block.is_locked())
             {
                 ioc.run_one();
                 if(! BEAST_EXPECT(! ioc.stopped()))
@@ -351,7 +459,7 @@ public:
             ws.async_close({},
                 [&](error_code ec)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     BEAST_EXPECT(++count == 2);
@@ -362,10 +470,12 @@ public:
         });
 
         // teardown on received close
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            std::string const s = "Hello, world!";
+            multi_buffer b;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -373,8 +483,7 @@ public:
             ws.next_layer().append(string_view{
                 "\x88\x00", 2});
             std::size_t count = 0;
-            std::string const s = "Hello, world!";
-            ws.async_write(buffer(s),
+            ws.async_write(net::buffer(s),
                 [&](error_code ec, std::size_t n)
                 {
                     if(ec)
@@ -383,11 +492,10 @@ public:
                     BEAST_EXPECT(n == s.size());
                     BEAST_EXPECT(++count == 1);
                 });
-            multi_buffer b;
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     BEAST_EXPECT(++count == 3);
@@ -406,10 +514,12 @@ public:
         });
 
         // check for deadlock
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            std::string const s = "Hello, world!";
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
@@ -417,9 +527,7 @@ public:
             ws.next_layer().append(string_view{
                 "\x89\x00", 2});
             std::size_t count = 0;
-            multi_buffer b;
-            std::string const s = "Hello, world!";
-            ws.async_write(buffer(s),
+            ws.async_write(net::buffer(s),
                 [&](error_code ec, std::size_t n)
                 {
                     if(ec)
@@ -431,12 +539,12 @@ public:
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     BEAST_EXPECT(++count == 3);
                 });
-            BEAST_EXPECT(ws.rd_block_.is_locked());
+            BEAST_EXPECT(ws.impl_->rd_block.is_locked());
             ws.async_close({},
                 [&](error_code ec)
                 {
@@ -446,23 +554,23 @@ public:
                     BEAST_EXPECT(++count == 2);
                 });
             BEAST_EXPECT(ws.is_open());
-            BEAST_EXPECT(ws.wr_block_.is_locked());
+            BEAST_EXPECT(ws.impl_->wr_block.is_locked());
             BEAST_EXPECT(count == 0);
             ioc.run();
             BEAST_EXPECT(count == 3);
         });
 
         // Four-way: close, read, write, ping
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            std::string const s = "Hello, world!";
+            multi_buffer b;
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
             std::size_t count = 0;
-            std::string const s = "Hello, world!";
-            multi_buffer b;
             ws.async_close({},
                 [&](error_code ec)
                 {
@@ -474,15 +582,15 @@ public:
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     ++count;
                 });
-            ws.async_write(buffer(s),
+            ws.async_write(net::buffer(s),
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     ++count;
@@ -490,7 +598,7 @@ public:
             ws.async_ping({},
                 [&](error_code ec)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     ++count;
@@ -501,33 +609,33 @@ public:
         });
 
         // Four-way: read, write, ping, close
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            std::string const s = "Hello, world!";
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
             std::size_t count = 0;
-            std::string const s = "Hello, world!";
-            multi_buffer b;
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec && ec != boost::asio::error::operation_aborted)
+                    if(ec && ec != net::error::operation_aborted)
                     {
                         BEAST_EXPECTS(ec, ec.message());
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     }
                     if(! ec)
-                        BEAST_EXPECT(to_string(b.data()) == s);
+                        BEAST_EXPECT(buffers_to_string(b.data()) == s);
                     ++count;
                     if(count == 4)
                         BEAST_EXPECT(
-                            ec == boost::asio::error::operation_aborted);
+                            ec == net::error::operation_aborted);
                 });
-            ws.async_write(buffer(s),
+            ws.async_write(net::buffer(s),
                 [&](error_code ec, std::size_t n)
                 {
                     if(ec)
@@ -539,7 +647,7 @@ public:
             ws.async_ping({},
                 [&](error_code ec)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                     {
                         BEAST_EXPECTS(ec, ec.message());
                         BOOST_THROW_EXCEPTION(
@@ -562,16 +670,16 @@ public:
         });
 
         // Four-way: ping, read, write, close
-        doFailLoop([&](test::fail_counter& fc)
+        doFailLoop([&](test::fail_count& fc)
         {
             echo_server es{log};
-            boost::asio::io_context ioc;
+            multi_buffer b;
+            std::string const s = "Hello, world!";
+            net::io_context ioc;
             stream<test::stream> ws{ioc, fc};
             ws.next_layer().connect(es.stream());
             ws.handshake("localhost", "/");
             std::size_t count = 0;
-            std::string const s = "Hello, world!";
-            multi_buffer b;
             ws.async_ping({},
                 [&](error_code ec)
                 {
@@ -583,15 +691,15 @@ public:
             ws.async_read(b,
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     ++count;
                 });
-            ws.async_write(buffer(s),
+            ws.async_write(net::buffer(s),
                 [&](error_code ec, std::size_t)
                 {
-                    if(ec != boost::asio::error::operation_aborted)
+                    if(ec != net::error::operation_aborted)
                         BOOST_THROW_EXCEPTION(
                             system_error{ec});
                     ++count;
@@ -610,25 +718,10 @@ public:
         });
     }
 
-    void
-    testContHook()
-    {
-        struct handler
-        {
-            void operator()(error_code) {}
-        };
-
-        stream<test::stream> ws{ioc_};
-        stream<test::stream>::close_op<handler> op{
-            handler{}, ws, {}};
-        using boost::asio::asio_handler_is_continuation;
-        asio_handler_is_continuation(&op);
-    }
-
     void
     testMoveOnly()
     {
-        boost::asio::io_context ioc;
+        net::io_context ioc;
         stream<test::stream> ws{ioc};
         ws.async_close({}, move_only_handler{});
     }
@@ -642,26 +735,13 @@ public:
         }
     };
 
-    void
-    testAsioHandlerInvoke()
-    {
-        // make sure things compile, also can set a
-        // breakpoint in asio_handler_invoke to make sure
-        // it is instantiated.
-        boost::asio::io_context ioc;
-        boost::asio::io_service::strand s{ioc};
-        stream<test::stream> ws{ioc};
-        ws.async_close({}, s.wrap(copyable_handler{}));
-    }
-
     void
     run() override
     {
         testClose();
+        testTimeout();
         testSuspend();
-        testContHook();
         testMoveOnly();
-        testAsioHandlerInvoke();
     }
 };