TEST_F(DummyServerFixture, InvalidateBadConnEvenWhenPoolIsFull) {
        mongo::pool.setMaxPoolSize(2);

        ScopedDbConnection conn1(TARGET_HOST);
        ScopedDbConnection conn2(TARGET_HOST);
        ScopedDbConnection conn3(TARGET_HOST);

        conn1.done();
        conn3.done();

        const uint64_t badCreationTime = mongo::curTimeMicros64();

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::alwaysOn);

        try {
            conn2->query("test.user", mongo::Query());
        }
        catch (const mongo::SocketException&) {
        }

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::off);
        conn2.done();

        checkNewConns(assertGreaterThan, badCreationTime, 2);
    }
    TEST_F(DummyServerFixture, DontReturnConnGoneBadToPool) {
        ScopedDbConnection conn1(TARGET_HOST);

        const uint64_t conn1CreationTime = conn1->getSockCreationMicroSec();

        uint64_t conn2CreationTime = 0;

        {
            ScopedDbConnection conn2(TARGET_HOST);
            conn2CreationTime = conn2->getSockCreationMicroSec();

            conn1.done();
            // conn2 gets out of scope without calling done()
        }

        // conn2 should not have been put back into the pool but it should
        // also not invalidate older connections since it didn't encounter
        // a socket exception.

        ScopedDbConnection conn1Again(TARGET_HOST);
        ASSERT_EQUALS(conn1CreationTime, conn1Again->getSockCreationMicroSec());

        checkNewConns(assertNotEqual, conn2CreationTime, 10);

        conn1Again.done();
    }
Example #3
0
    TEST_F(ShardConnFixture, KilledGoodConnShouldNotClearPool) {
        ShardConnection conn1(TARGET_HOST, "test.user");
        ShardConnection conn2(TARGET_HOST, "test.user");
        ShardConnection conn3(TARGET_HOST, "test.user");

        const uint64_t upperBoundCreationTime =
                conn3.get()->getSockCreationMicroSec();
        conn3.done();

        const uint64_t badCreationTime = conn1.get()->getSockCreationMicroSec();
        conn1.kill();

        conn2.done();

        ShardConnection conn4(TARGET_HOST, "test.user");
        ShardConnection conn5(TARGET_HOST, "test.user");

        ASSERT_GREATER_THAN(conn4.get()->getSockCreationMicroSec(), badCreationTime);
        ASSERT_LESS_THAN_OR_EQUALS(conn4.get()->getSockCreationMicroSec(),
                upperBoundCreationTime);

        ASSERT_GREATER_THAN(conn5.get()->getSockCreationMicroSec(), badCreationTime);
        ASSERT_LESS_THAN_OR_EQUALS(conn5.get()->getSockCreationMicroSec(),
                upperBoundCreationTime);

        checkNewConns(assertGreaterThan, upperBoundCreationTime, 10);
    }
    TEST_F(DummyServerFixture, DontReturnKnownBadConnToPool) {
        ScopedDbConnection conn1(TARGET_HOST);
        ScopedDbConnection conn2(TARGET_HOST);
        ScopedDbConnection conn3(TARGET_HOST);

        conn1.done();

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::alwaysOn);

        try {
            conn3->query("test.user", mongo::Query());
        }
        catch (const mongo::SocketException&) {
        }

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::off);

        const uint64_t badCreationTime = conn3->getSockCreationMicroSec();
        conn3.done();
        // attempting to put a 'bad' connection back to the pool
        conn2.done();

        checkNewConns(assertGreaterThan, badCreationTime, 10);
    }
Example #5
0
    TEST_F(DummyServerFixture, InvalidateBadConnInPool) {
        scoped_ptr<ScopedDbConnection> conn1(
            ScopedDbConnection::getScopedDbConnection(TARGET_HOST));
        scoped_ptr<ScopedDbConnection> conn2(
                ScopedDbConnection::getScopedDbConnection(TARGET_HOST));
        scoped_ptr<ScopedDbConnection> conn3(
                ScopedDbConnection::getScopedDbConnection(TARGET_HOST));

        conn1->done();
        conn3->done();

        const uint64_t badCreationTime = mongo::curTimeMicros64();

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::alwaysOn);

        try {
            conn2->get()->query("test.user", mongo::Query());
        }
        catch (const mongo::SocketException&) {
        }

        mongo::getGlobalFailPointRegistry()->getFailPoint("throwSockExcep")->
                setMode(FailPoint::off);
        conn2->done();

        checkNewConns(assertGreaterThan, badCreationTime, 10);
    }
Example #6
0
TEST(ConnectXTest, consCheck) //Check possible condition cases for constructor.
{
	ConnectX conn1(4,5,3);
	ConnectX conn2(-4,5,2);
	ConnectX conn3(4,-5,3);	
	ConnectX conn4(4,5,-3);
}
Example #7
0
    //==============================================================================
    void initialise (const String& commandLine) override
    {
        // This method is where you should put your application's initialisation code..

    try 
    {
        const char* headers[] = 
        {
            "Connection", "close",
                "Content-type", "application/x-www-form-urlencoded",
                "Accept", "text/plain",
                0
        };
        
            const char* body = "answer=42&name=Bubba";
            
            happyhttp::Connection conn1( "posttestserver.com", 80 );
            conn1.setcallbacks( OnBegin, OnData, OnComplete, 0 );
            conn1.request( "PUT",
                    "/post.php",
                    headers,
                    (const unsigned char*)body,
                    strlen(body) );
            
            while( conn1.outstanding() )
            {
                conn1.pump();        
            }

            const char* cbliteHeaders[] = 
            {
                "Connection", "close",
                "Authorization", "Basic Y2JsaXRlOnBhc3N3b3Jk",
                0
            };

            happyhttp::Connection conn2( "localhost", 5984 );
            conn2.setcallbacks( OnBegin, OnData, OnComplete, 0 );

            conn2.request( "GET", "/", cbliteHeaders, 0, 0 );

            while( conn2.outstanding() )
                conn2.pump();                    
        }
        catch( happyhttp::Wobbly& e )
        {
            DBG ("Exception:\n" << e.what() );
        }

        
        mainWindow = new MainWindow (getApplicationName());
    }
    TEST_F(DummyServerFixture, BasicScopedDbConnection) {
        ScopedDbConnection conn1(TARGET_HOST);
        ScopedDbConnection conn2(TARGET_HOST);

        DBClientBase* conn1Ptr = conn1.get();
        conn1.done();

        ScopedDbConnection conn3(TARGET_HOST);
        ASSERT_EQUALS(conn1Ptr, conn3.get());

        conn2.done();
        conn3.done();
    }
Example #9
0
    TEST_F(ShardConnFixture, BasicShardConnection) {
        ShardConnection conn1(TARGET_HOST, "test.user");
        ShardConnection conn2(TARGET_HOST, "test.user");

        DBClientBase* conn1Ptr = conn1.get();
        conn1.done();

        ShardConnection conn3(TARGET_HOST, "test.user");
        ASSERT_EQUALS(conn1Ptr, conn3.get());

        conn2.done();
        conn3.done();
    }
Example #10
0
    TEST_F(DummyServerFixture, BasicScopedDbConnection) {
        scoped_ptr<ScopedDbConnection> conn1(
                ScopedDbConnection::getScopedDbConnection(TARGET_HOST));
        scoped_ptr<ScopedDbConnection> conn2(
                ScopedDbConnection::getScopedDbConnection(TARGET_HOST));

        DBClientBase* conn1Ptr = conn1->get();
        conn1->done();

        scoped_ptr<ScopedDbConnection> conn3(
                ScopedDbConnection::getScopedDbConnection(TARGET_HOST));
        ASSERT_EQUALS(conn1Ptr, conn3->get());

        conn2->done();
        conn3->done();
    }
CASE_TEST(happ_connection, basic)
{
    AutoPtr<hiredis::happ::connection> conn1(new hiredis::happ::connection());
    AutoPtr<hiredis::happ::connection> conn2(new hiredis::happ::connection());

    CASE_EXPECT_EQ(conn1->get_sequence() + 1, conn2->get_sequence());

    hiredis::happ::holder_t h;
    redisAsyncContext vir_context;

    conn1->init(h, "127.0.0.2", 1234);
    conn2->init(h, "127.0.0.3", 1234);

    CASE_EXPECT_TRUE("127.0.0.2" == conn1->get_key().ip);
    CASE_EXPECT_TRUE(1234 == conn1->get_key().port);
    CASE_EXPECT_TRUE("127.0.0.2:1234" == conn1->get_key().name);

    CASE_EXPECT_EQ(hiredis::happ::connection::status::DISCONNECTED, conn1->conn_status);
    hiredis::happ::cmd_exec* cmd = hiredis::happ::cmd_exec::create(h, NULL, &vir_context, 0);

    int res = conn1->redis_cmd(cmd, NULL);
    CASE_EXPECT_EQ(hiredis::happ::error_code::REDIS_HAPP_CREATE, res);

    conn1->set_connecting(&vir_context);

    CASE_EXPECT_EQ(conn1->get_context(), &vir_context);
    CASE_EXPECT_EQ(hiredis::happ::connection::status::CONNECTING, conn1->conn_status);
    conn1->set_connected();
    CASE_EXPECT_EQ(hiredis::happ::connection::status::CONNECTED, conn1->conn_status);

    CASE_EXPECT_EQ(conn1->get_context(), &vir_context);

    cmd->pri_data = conn2.get();
    conn2->set_connecting(&vir_context);

    conn1->release(false);
    conn2->release(false);

    CASE_EXPECT_EQ(conn1->get_context(), NULL);
    CASE_EXPECT_EQ(conn2->get_context(), NULL);

    hiredis::happ::cmd_exec::destroy(cmd);
}
Example #12
0
static int lsp_connect(lua_State *L) {
  char ebuf[100];
  SOCKET sock;

  if (lua_isstring(L, -3) && lua_isnumber(L, -2) && lua_isnumber(L, -1)) {
    sock = conn2(lua_tostring(L, -3), (int) lua_tonumber(L, -2),
                 (int) lua_tonumber(L, -1), ebuf, sizeof(ebuf));
    if (sock == INVALID_SOCKET) {
      return luaL_error(L, ebuf);
    } else {
      lua_newtable(L);
      reg_int(L, "sock", sock);
      reg_string(L, "host", lua_tostring(L, -4));
      luaL_getmetatable(L, LUASOCKET);
      lua_setmetatable(L, -2);
    }
  } else {
    return luaL_error(L, "connect(host,port,is_ssl): invalid parameter given.");
  }
  return 1;
}
Example #13
0
struct mg_connection *mg_connect(const char *host, int port, int use_ssl,
                                 char *ebuf, size_t ebuf_len) {
  static struct mg_context fake_ctx;
  struct mg_connection *conn = NULL;
  SOCKET sock;

  if ((sock = conn2(host, port, use_ssl, ebuf, ebuf_len)) == INVALID_SOCKET) {
  } else if ((conn = (struct mg_connection *)
              calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) {
    snprintf(ebuf, ebuf_len, "calloc(): %s", strerror(ERRNO));
    closesocket(sock);
#ifndef NO_SSL
  } else if (use_ssl && (conn->client_ssl_ctx =
                         SSL_CTX_new(SSLv23_client_method())) == NULL) {
    snprintf(ebuf, ebuf_len, "SSL_CTX_new error");
    closesocket(sock);
    free(conn);
    conn = NULL;
#endif // NO_SSL
  } else {
    socklen_t len = sizeof(struct sockaddr);
    conn->buf_size = MAX_REQUEST_SIZE;
    conn->buf = (char *) (conn + 1);
    conn->ctx = &fake_ctx;
    conn->client.sock = sock;
    getsockname(sock, &conn->client.rsa.sa, &len);
    conn->client.is_ssl = use_ssl;
#ifndef NO_SSL
    if (use_ssl) {
      // SSL_CTX_set_verify call is needed to switch off server certificate
      // checking, which is off by default in OpenSSL and on in yaSSL.
      SSL_CTX_set_verify(conn->client_ssl_ctx, 0, 0);
      sslize(conn, conn->client_ssl_ctx, SSL_connect);
    }
#endif
  }

  return conn;
}
Example #14
0
    TEST_F(ShardConnFixture, InvalidateBadConnInPool) {
        ShardConnection conn1(TARGET_HOST, "test.user");
        ShardConnection conn2(TARGET_HOST, "test.user");
        ShardConnection conn3(TARGET_HOST, "test.user");

        conn1.done();
        conn3.done();

        const uint64_t badCreationTime = mongo::curTimeMicros64();
        killServer();

        try {
            conn2.get()->query("test.user", mongo::Query());
        }
        catch (const mongo::SocketException&) {
        }

        conn2.done();

        restartServer();
        checkNewConns(assertGreaterThan, badCreationTime, 10);
    }
Example #15
0
    TEST_F(ShardConnFixture, BadConnClearsPoolWhenKilled) {
        ShardConnection conn1(TARGET_HOST, "test.user");
        ShardConnection conn2(TARGET_HOST, "test.user");
        ShardConnection conn3(TARGET_HOST, "test.user");

        conn1.done();
        killServer();

        try {
            conn3.get()->query("test.user", mongo::Query());
        }
        catch (const mongo::SocketException&) {
        }

        restartServer();

        const uint64_t badCreationTime = conn3.get()->getSockCreationMicroSec();
        conn3.kill();
        // attempting to put a 'bad' connection back to the pool
        conn2.done();

        checkNewConns(assertGreaterThan, badCreationTime, 10);
    }
    TEST(MockDBClientConnTest, Restart) {
        MockRemoteDBServer server("test");
        server.setCommandReply("serverStatus", BSON("ok" << 1));

        MockDBClientConnection conn1(&server);

        // Do some queries and commands then check the counters later that
        // new instance still has it
        conn1.query("test.user");
        BSONObj response;
        conn1.runCommand("test.user", BSON("serverStatus" << 1), response);

        server.shutdown();
        ASSERT_THROWS(conn1.query("test.user"), mongo::SocketException);

        // New connections shouldn't work either
        MockDBClientConnection conn2(&server);
        ASSERT_THROWS(conn2.query("test.user"), mongo::SocketException);

        ASSERT_EQUALS(1U, server.getQueryCount());
        ASSERT_EQUALS(1U, server.getCmdCount());

        server.reboot();
        ASSERT(server.isRunning());

        {
            MockDBClientConnection conn(&server);
            conn.query("test.user");
        }

        // Old connections still shouldn't work
        ASSERT_THROWS(conn1.query("test.user"), mongo::SocketException);
        ASSERT_THROWS(conn2.query("test.user"), mongo::SocketException);

        ASSERT_EQUALS(2U, server.getQueryCount());
        ASSERT_EQUALS(1U, server.getCmdCount());
    }