RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/testing/test_active_endpoint_not_responding.cc
00001 /* endpoint_test.cc
00002    Jeremy Barnes, 31 January 2011
00003    Copyright (c) 2011 Datacratic.  All rights reserved.
00004 
00005    Tests for the endpoints.
00006 */
00007 
00008 #define BOOST_TEST_MAIN
00009 #define BOOST_TEST_DYN_LINK
00010 
00011 #include <boost/test/unit_test.hpp>
00012 #include "soa/service/http_endpoint.h"
00013 #include "soa/service/active_endpoint.h"
00014 #include "soa/service/passive_endpoint.h"
00015 #include <sys/socket.h>
00016 #include "jml/utils/guard.h"
00017 #include "jml/arch/exception_handler.h"
00018 #include "jml/utils/testing/watchdog.h"
00019 #include "jml/utils/testing/fd_exhauster.h"
00020 #include "test_connection_error.h"
00021 
00022 using namespace std;
00023 using namespace ML;
00024 using namespace Datacratic;
00025 
00026 
00027 BOOST_AUTO_TEST_CASE( test_active_endpoint_not_responding )
00028 {
00029     BOOST_REQUIRE_EQUAL(TransportBase::created, TransportBase::destroyed);
00030     BOOST_REQUIRE_EQUAL(ConnectionHandler::created,
00031                         ConnectionHandler::destroyed);
00032 
00033     Watchdog watchdog(5.0);
00034     
00035     for (unsigned i = 0;  i < 1;  ++i) {
00036         // Listen but don't respond to connections.  When the backlog is
00037         // full, connections will hang and we can test the timeout
00038         // behaviour.
00039         int port = 12942;
00040         int backlog = 10;
00041 
00042         int s = socket(AF_INET, SOCK_STREAM, 0);
00043         BOOST_REQUIRE(s > 0);
00044 
00045         for (int j = 0;  j < 100;  ++j, ++port) {
00046             struct sockaddr_in addr = { AF_INET, htons(port), { INADDR_ANY } }; 
00047             errno = 0;
00048             int b = ::bind(s, reinterpret_cast<sockaddr *>(&addr), sizeof(addr));
00049             if (b == -1) {
00050                 cerr << "couldn't bind to port " << port << ": "
00051                      << strerror(errno) << endl;
00052                 continue;
00053             }
00054             BOOST_REQUIRE_EQUAL(b, 0);
00055             break;
00056         }
00057         
00058         BOOST_REQUIRE_EQUAL(string(strerror(errno)), string(strerror(0)));
00059 
00060         int r = listen(s, backlog);
00061         BOOST_REQUIRE(r == 0);
00062 
00063         // Connector to use up all of the connections
00064         ActiveEndpointT<SocketTransport> connector2("connector2");
00065         connector2.init(port, "localhost", 20, 1, true,
00066                         false /* throw on error */);
00067         
00068         cerr << "got " << connector2.numActiveConnections() << " active and "
00069              << connector2.numInactiveConnections() << " inactive connections"
00070              << endl;
00071 
00072         BOOST_CHECK_LT(connector2.numInactiveConnections(), 20);
00073         BOOST_CHECK_EQUAL(connector2.numActiveConnections(), 0);
00074 
00075         cerr << endl << "iter " << i << endl;
00076         ActiveEndpointT<SocketTransport> connector("connector");
00077         connector.init(port, "localhost", 0, 1, true);
00078         doTestConnectionError(connector, "connection timed out",
00079                               "Timer expired");
00080         cerr << "test returned" << endl;
00081         connector.shutdown();
00082         cerr << "connector down" << endl;
00083         connector2.shutdown();
00084         cerr << "connector2 down" << endl;
00085 
00086         BOOST_CHECK_EQUAL(connector2.numConnections(), 0);
00087         BOOST_CHECK_EQUAL(connector2.numInactiveConnections(), 0);
00088         BOOST_CHECK_EQUAL(connector2.numActiveConnections(), 0);
00089 
00090         BOOST_CHECK_EQUAL(TransportBase::created, TransportBase::destroyed);
00091         BOOST_CHECK_EQUAL(ConnectionHandler::created,
00092                           ConnectionHandler::destroyed);
00093     }
00094 
00095     BOOST_CHECK_EQUAL(TransportBase::created, TransportBase::destroyed);
00096     BOOST_CHECK_EQUAL(ConnectionHandler::created,
00097                       ConnectionHandler::destroyed);
00098 }
00099 
00100 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator