|
| 1 | +#define BOOST_TEST_MODULE HTTP Asynchronous Server Tests |
| 2 | + |
| 3 | +#include <boost/network/include/http/server.hpp> |
| 4 | +#include <boost/test/unit_test.hpp> |
| 5 | +#include <boost/thread.hpp> |
| 6 | + |
| 7 | +namespace http = boost::network::http; |
| 8 | +namespace util = boost::network::utils; |
| 9 | + |
| 10 | +struct dummy_async_handler; |
| 11 | +typedef http::async_server<dummy_async_handler> async_server; |
| 12 | + |
| 13 | +struct dummy_async_handler { |
| 14 | + void operator()(async_server::request const & req, |
| 15 | + async_server::connection_ptr conn) { |
| 16 | + // Really, this is just for testing purposes |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +// In this batch of tests we ensure that calling run and stop on an async_server, in any sequence, is thread safe. |
| 21 | + |
| 22 | +int main(int argc, char * argv[]) { |
| 23 | + dummy_async_handler async_handler; |
| 24 | + |
| 25 | +#define ASYNC_SERVER_TEST_CONFIG \ |
| 26 | + http::_address = "127.0.0.1", \ |
| 27 | + http::_port = "80", \ |
| 28 | + http::_handler = async_handler, \ |
| 29 | + http::_thread_pool = pool, \ |
| 30 | + http::_reuse_address = true |
| 31 | + |
| 32 | +#define ASYNC_SERVER_SLEEP_TIME \ |
| 33 | + boost::posix_time::milliseconds(100) |
| 34 | + |
| 35 | + // stop from main thread |
| 36 | + { |
| 37 | + BOOST_NETWORK_MESSAGE("TEST: stop without running"); |
| 38 | + util::thread_pool pool; |
| 39 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 40 | + server_instance.stop(); |
| 41 | + } |
| 42 | + |
| 43 | + // run-stop from main thread |
| 44 | + { |
| 45 | + BOOST_NETWORK_MESSAGE("TEST: stop from main thread"); |
| 46 | + util::thread_pool pool; |
| 47 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 48 | + |
| 49 | + boost::thread running_thread(boost::bind(&async_server::run, &server_instance)); |
| 50 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 51 | + |
| 52 | + server_instance.stop(); |
| 53 | + running_thread.join(); |
| 54 | + } |
| 55 | + |
| 56 | + // run-stop from another thread |
| 57 | + { |
| 58 | + BOOST_NETWORK_MESSAGE("TEST: stop from another thread"); |
| 59 | + util::thread_pool pool; |
| 60 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 61 | + |
| 62 | + boost::thread running_thread(boost::bind(&async_server::run, &server_instance)); |
| 63 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 64 | + |
| 65 | + boost::thread stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 66 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 67 | + |
| 68 | + stopping_thread.join(); |
| 69 | + running_thread.join(); |
| 70 | + } |
| 71 | + |
| 72 | + // run-stop-run-stop from another thread |
| 73 | + { |
| 74 | + BOOST_NETWORK_MESSAGE("TEST: run-stop-run-stop from another thread"); |
| 75 | + util::thread_pool pool; |
| 76 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 77 | + |
| 78 | + boost::thread running_thread(boost::bind(&async_server::run, &server_instance)); |
| 79 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 80 | + |
| 81 | + boost::thread stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 82 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 83 | + |
| 84 | + boost::thread second_running_thread(boost::bind(&async_server::run, &server_instance)); |
| 85 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 86 | + |
| 87 | + boost::thread second_stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 88 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 89 | + |
| 90 | + stopping_thread.join(); |
| 91 | + running_thread.join(); |
| 92 | + second_stopping_thread.join(); |
| 93 | + second_running_thread.join(); |
| 94 | + } |
| 95 | + |
| 96 | + // run-run-stop from another thread |
| 97 | + { |
| 98 | + BOOST_NETWORK_MESSAGE("TEST: run-run-stop from another thread"); |
| 99 | + util::thread_pool pool; |
| 100 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 101 | + |
| 102 | + boost::thread running_thread(boost::bind(&async_server::run, &server_instance)); |
| 103 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 104 | + |
| 105 | + boost::thread second_running_thread(boost::bind(&async_server::run, &server_instance)); |
| 106 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 107 | + |
| 108 | + boost::thread stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 109 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 110 | + |
| 111 | + stopping_thread.join(); |
| 112 | + running_thread.join(); |
| 113 | + second_running_thread.join(); |
| 114 | + } |
| 115 | + |
| 116 | + // run-stop-stop from another thread |
| 117 | + { |
| 118 | + BOOST_NETWORK_MESSAGE("TEST: run-stop-stop from another thread"); |
| 119 | + util::thread_pool pool; |
| 120 | + async_server server_instance(ASYNC_SERVER_TEST_CONFIG); |
| 121 | + |
| 122 | + boost::thread running_thread(boost::bind(&async_server::run, &server_instance)); |
| 123 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 124 | + |
| 125 | + boost::thread stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 126 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 127 | + |
| 128 | + boost::thread second_stopping_thread(boost::bind(&async_server::stop, &server_instance)); |
| 129 | + boost::this_thread::sleep(ASYNC_SERVER_SLEEP_TIME); |
| 130 | + |
| 131 | + stopping_thread.join(); |
| 132 | + second_stopping_thread.join(); |
| 133 | + running_thread.join(); |
| 134 | + } |
| 135 | + |
| 136 | +#undef ASYNC_SERVER_TEST_CONFIG |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
0 commit comments