RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/testing/test_endpoint_accept_speed.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 #include "ping_pong.h"
00022 #include <poll.h>
00023 #include "jml/utils/exc_assert.h"
00024 
00025 
00026 using namespace std;
00027 using namespace ML;
00028 using namespace Datacratic;
00029 
00030 void runAcceptSpeedTest()
00031 {
00032     string connectionError;
00033 
00034     PassiveEndpointT<SocketTransport> acceptor("acceptor");
00035     
00036     acceptor.onMakeNewHandler = [&] ()
00037         {
00038             return ML::make_std_sp(new PongConnectionHandler(connectionError));
00039         };
00040     
00041     int port = acceptor.init();
00042 
00043     cerr << "port = " << port << endl;
00044 
00045     BOOST_CHECK_EQUAL(acceptor.numConnections(), 0);
00046 
00047     int nconnections = 100;
00048 
00049     Date before = Date::now();
00050 
00051     vector<int> sockets;
00052 
00053     /* Open all the connections */
00054     for (unsigned i = 0;  i < nconnections;  ++i) {
00055         int s = socket(AF_INET, SOCK_STREAM, 0);
00056         if (s == -1)
00057             throw Exception("socket");
00058 
00059         //cerr << "i = " << i << " s = " << s << " sockets.size() = "
00060         //     << sockets.size() << endl;
00061 
00062         struct sockaddr_in addr = { AF_INET, htons(port), { INADDR_ANY } }; 
00063         //cerr << "before connect on " << s << endl;
00064         int res = connect(s, reinterpret_cast<const sockaddr *>(&addr),
00065                           sizeof(addr));
00066         //cerr << "after connect on " << s << endl;
00067 
00068         if (res == -1) {
00069             cerr << "connect error: " << strerror(errno) << endl;
00070             close(s);
00071         }
00072         else {
00073             sockets.push_back(s);
00074         }
00075     }
00076 
00077     /* Write to each and get a response back.  This makes sure that all are open. */
00078     for (unsigned i = 0;  i < sockets.size();  ++i) {
00079         int s = sockets[i];
00080         int res = write(s, "hello", 5);
00081         ExcAssertEqual(res, 5);
00082         
00083         char buf[16];
00084         
00085         res = read(s, buf, 16);
00086         ExcAssertEqual(res, 4);
00087         if (res > 0) {
00088             ExcAssertEqual(string(buf, buf + res), "Hi!!");
00089         }
00090     }
00091 
00092     Date after = Date::now();
00093 
00094     BOOST_CHECK_LT(after.secondsSince(before), 1);
00095 
00096     BOOST_CHECK_EQUAL(sockets.size(), nconnections);
00097 
00098 
00099 
00100     BOOST_CHECK_EQUAL(acceptor.numConnections(), nconnections);
00101 
00102     acceptor.closePeer();
00103 
00104     for (unsigned i = 0;  i < sockets.size();  ++i) {
00105         close(sockets[i]);
00106     }
00107 
00108     acceptor.shutdown();
00109 }
00110 
00111 
00112 BOOST_AUTO_TEST_CASE( test_accept_speed )
00113 {
00114     BOOST_REQUIRE_EQUAL(TransportBase::created, TransportBase::destroyed);
00115     BOOST_REQUIRE_EQUAL(ConnectionHandler::created,
00116                         ConnectionHandler::destroyed);
00117 
00118     Watchdog watchdog(50.0);
00119 
00120     int ntests = 1;
00121     //ntests = 1000;  // stress test
00122 
00123     for (unsigned i = 0;  i < ntests;  ++i) {
00124         runAcceptSpeedTest();
00125     }
00126 
00127     BOOST_CHECK_EQUAL(TransportBase::created, TransportBase::destroyed);
00128     BOOST_CHECK_EQUAL(ConnectionHandler::created,
00129                       ConnectionHandler::destroyed);
00130 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator