RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/testing/endpoint_unit_test.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 #if 0 // zeromq aborts when it can't get an fd
00027 BOOST_AUTO_TEST_CASE( test_passive_endpoint_create_no_fds )
00028 {
00029     Watchdog watchdog;
00030     FDExhauster exhaust_fds;
00031 
00032     PassiveEndpointT<SocketTransport> endpoint;
00033 
00034     {
00035         JML_TRACE_EXCEPTIONS(false);
00036         BOOST_CHECK_THROW(endpoint.init(), ML::Exception);
00037     }
00038 
00039     endpoint.shutdown();
00040 }
00041 #endif
00042 
00043 #if 0  // we need more than one FD for each connection now; test unreliable
00044 BOOST_AUTO_TEST_CASE( test_active_endpoint_create_no_fds )
00045 {
00046     BOOST_REQUIRE_EQUAL(TransportBase::created, TransportBase::destroyed);
00047     BOOST_REQUIRE_EQUAL(ConnectionHandler::created,
00048                         ConnectionHandler::destroyed);
00049 
00050     Watchdog watchdog;
00051     
00052     ActiveEndpointT<SocketTransport> connector("connector");
00053     connector.init(9997, "localhost", 0, 1, true, false /* throw on error */);
00054 
00055     {
00056         FDExhauster exhaust_fds;
00057         
00058         cerr << "doing connection error" << endl;
00059 
00060         doTestConnectionError(connector, "Too many open files");
00061 
00062         cerr << "done connection error" << endl;
00063     }
00064 
00065     connector.shutdown();
00066 
00067     BOOST_CHECK_EQUAL(connector.numActiveConnections(), 0);
00068     BOOST_CHECK_EQUAL(connector.numInactiveConnections(), 0);
00069     BOOST_CHECK_EQUAL(connector.threadsActive(), 0);
00070     BOOST_CHECK_EQUAL(TransportBase::created, TransportBase::destroyed);
00071     BOOST_CHECK_EQUAL(ConnectionHandler::created,
00072                       ConnectionHandler::destroyed);
00073 }
00074 #endif
00075 
00076 #if 0 // no way yet to make work be done until sem acquired
00077 BOOST_AUTO_TEST_CASE( test_active_endpoint_no_threads )
00078 {
00079     Watchdog watchdog;
00080     
00081     ActiveEndpointT<SocketTransport> connector;
00082     connector.init(9997, "localhost", 0, 0, true);
00083 
00084     doTestConnectionError(connector, "not connected");
00085 }
00086 #endif
00087 
00088 
00089 BOOST_AUTO_TEST_CASE( test_passive_endpoint )
00090 {
00091     BOOST_REQUIRE_EQUAL(TransportBase::created, TransportBase::destroyed);
00092     BOOST_REQUIRE_EQUAL(ConnectionHandler::created,
00093                         ConnectionHandler::destroyed);
00094 
00095     Watchdog watchdog(5.0);
00096 
00097     string connectionError;
00098 
00099     PassiveEndpointT<SocketTransport> acceptor("acceptor");
00100     int port = acceptor.init();
00101 
00102     BOOST_CHECK_NE(port, -1);
00103 
00104     acceptor.shutdown();
00105 
00106     BOOST_CHECK_EQUAL(TransportBase::created, TransportBase::destroyed);
00107     BOOST_CHECK_EQUAL(ConnectionHandler::created,
00108                       ConnectionHandler::destroyed);
00109 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator