RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/testing/test_connection_error.h
00001 /* test_connection_error.h                                         -*- C++ -*-
00002    Jeremy Barnes, 16 May 2011
00003    Copyright (c) 2011 Datacratic.  All rights reserved.
00004 
00005    Testing include for endpoint connection error tests.
00006 */
00007 
00008 using namespace std;
00009 using namespace ML;
00010 using namespace Datacratic;
00011 
00012 
00013 template<typename Endpoint>
00014 void doTestConnectionError(Endpoint & connector,
00015                            const std::string & errorRequired,
00016                            const std::string & errorRequired2 = "")
00017 {
00018     BOOST_CHECK_EQUAL(connector.numActiveConnections(), 0);
00019     BOOST_CHECK_EQUAL(connector.numInactiveConnections(), 0);
00020     BOOST_CHECK_EQUAL(connector.threadsActive(), 1);
00021 
00022     string errorMsg;
00023     bool succeeded = false;
00024     ACE_Semaphore sem(0);
00025 
00026     auto onNewConnection
00027         = [&] (const std::shared_ptr<TransportBase> & transport)
00028         {
00029             cerr << "new connection" << endl;
00030             BOOST_CHECK_EQUAL(typeid(*transport).name(),
00031                               typeid(SocketTransport).name());
00032 
00033             succeeded = true;
00034             sem.release();
00035             transport->closeAsync();
00036         };
00037 
00038     auto onConnectionError = [&] (std::string error)
00039         {
00040             cerr << "connection error " << error << endl;
00041             errorMsg = error;
00042             sem.release();
00043         };
00044 
00045     connector.getConnection(onNewConnection, onConnectionError,
00046                             1.0);
00047 
00048     int sem_acquire_res = sem.acquire();
00049 
00050     BOOST_CHECK_EQUAL(sem_acquire_res, 0);
00051 
00052     BOOST_CHECK_EQUAL(succeeded, false);
00053     cerr << "errorMsg = " << errorMsg << endl;
00054     cerr << "errorRequired = " << errorRequired << endl;
00055     bool found1 = errorMsg.find(errorRequired) != string::npos;
00056     bool found2 = errorRequired2 != ""
00057         && errorMsg.find(errorRequired2) != string::npos;
00058     BOOST_CHECK_EQUAL(found1 || found2, true);
00059 
00060     BOOST_CHECK_EQUAL(connector.numActiveConnections(), 0);
00061     BOOST_CHECK_EQUAL(connector.numInactiveConnections(), 0);
00062     BOOST_CHECK_EQUAL(connector.threadsActive(), 1);
00063 }
00064 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator