Example #1
0
    def config(self, callback=None):
        ismaster = SON([('ismaster', 1)])

        response = None
        try:
            connection = Connection(host=self.host, port=self.port)
            response, error = yield gen.Task(self.database.command, ismaster,
                connection=connection)
            connection.close()
        except InterfaceError, ie:
            logger.error('oops, database node {host}:{port} is unavailable: {error}' \
                .format(host=self.host, port=self.port, error=ie))
Example #2
0
    def test_raises_interface_error_when_cant_reconnect(self):
        """[ConnectionTestCase] - Raises InterfaceError when connection was lost and autoreconnect is False"""

        self.conn = Connection(host="localhost",
                               port=27027,
                               autoreconnect=False)

        self.conn.close()

        self.conn.send_message.when.called_with('shouldBeMessage',
                                                callback=None).should.throw(
                                                    InterfaceError,
                                                    "connection is closed")
Example #3
0
    def config(self, callback=None):
        ismaster = SON([('ismaster', 1)])

        response = None
        try:
            try:
                connection = yield gen.Task(self.connection)
            except TooManyConnections:
                # create a connection on the fly if pool is full
                connection = Connection(host=self.host, port=self.port)
            response, error = yield gen.Task(self.database._command, ismaster,
                connection=connection)
            if not connection._pool:  # if connection is created on the fly
                connection.close()
        except InterfaceError, ie:
            logger.error('oops, database node {host}:{port} is unavailable: {error}' \
                .format(host=self.host, port=self.port, error=ie))
Example #4
0
    def test_raises_interface_error_when_cant_reconnect(self):
        """[ConnectionTestCase] - Raises InterfaceError when connection was lost and autoreconnect is False"""

        self.conn = Connection(host="localhost", port=27027, autoreconnect=False)

        self.conn.close()

        self.conn.send_message.when.called_with('shouldBeMessage',
            callback=None).should.throw(InterfaceError, "connection is closed")
Example #5
0
    def config(self, callback=None):
        ismaster = SON([('ismaster', 1)])

        response = None
        try:
            try:
                connection = yield gen.Task(self.connection)
            except TooManyConnections:
                # create a connection on the fly if pool is full
                connection = Connection(host=self.host, port=self.port)
            response, error = yield gen.Task(self.database._command,
                                             ismaster,
                                             connection=connection)
            if not connection._pool:  # if connection is created on the fly
                connection.close()
        except InterfaceError, ie:
            logger.error(
                'oops, database node {host}:{port} is unavailable: {error}'.
                format(host=self.host, port=self.port, error=ie))
Example #6
0
 def setUp(self):
     super(ConnectionTestCase, self).setUp()
     self.conn = Connection(host="localhost", port=27027)
Example #7
0
class ConnectionTestCase(testing.AsyncTestCase):
    def get_new_ioloop(self):
        return IOLoop.instance()

    def setUp(self):
        super(ConnectionTestCase, self).setUp()
        self.conn = Connection(host="localhost", port=27027)

    def tearDown(self):
        super(ConnectionTestCase, self).tearDown()
        self.conn.close()

    def test_not_connect_to_mongo_raises_error(self):
        """[ConnectionTestCase] - Raises error when can't connect to mongo"""

        Connection.when.called_with(host="localhost", port=27000) \
            .should.throw(InterfaceError, "Connection refused")

    def test_connect_to_mongo(self):
        """[ConnectionTestCase] - Can stabilish connection to mongo"""

        self.conn._connected.should.be.ok

    def test_send_test_message_to_mongo(self):
        """[ConnectionTestCase] - Send message to test driver connection"""

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
                                     {'driverOIDTest': object_id})

        self.conn.send_message_with_response(message_test, callback=self.stop)
        response, _ = self.wait()

        response = helpers._unpack_response(response)
        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))

    def test_close_connection_to_mongo(self):
        """[ConnectionTestCase] - Can close connection to mongo"""

        self.conn.close()

        self.conn._connected.should_not.be.ok
        self.conn._stream.closed().should.be.ok

    def test_return_integrity_error_when_mongo_return_err(self):
        """[ConnectionTestCase] - Returns IntegrityError when mongo return a message with err"""

        object_id = ObjectId()
        message_insert = message.insert('mongotor_test.articles',
                                        [{
                                            '_id': object_id
                                        }], False, True, {})

        self.conn.send_message(message_insert, True, callback=self.stop)
        self.wait()

        self.conn.send_message(message_insert, True, callback=self.stop)
        self.wait.when.called_with().throw(IntegrityError)

    @fudge.patch('mongotor.connection.helpers')
    def test_raises_error_when_cant_unpack_response(self, fake_helpers):
        """[ConnectionTestCase] - Returns DatabaseError when can't unpack response from mongo"""

        fake_helpers.provides('_unpack_response') \
            .raises(DatabaseError('database error'))

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
                                     {'driverOIDTest': object_id})

        self.conn.send_message(message_test,
                               with_last_error=True,
                               callback=self.stop)

        self.wait.when.called_with().throw(DatabaseError, 'database error')

    def test_reconnect_when_connection_was_lost(self):
        """[ConnectionTestCase] - Reconnect to mongo when connection was lost"""

        self.conn.close()
        self.conn._callback = self.stop
        self.wait()

        self.test_send_test_message_to_mongo()

    def test_raises_interface_error_when_cant_reconnect(self):
        """[ConnectionTestCase] - Raises InterfaceError when connection was lost and autoreconnect is False"""

        self.conn = Connection(host="localhost",
                               port=27027,
                               autoreconnect=False)

        self.conn.close()

        self.conn.send_message.when.called_with('shouldBeMessage',
                                                callback=None).should.throw(
                                                    InterfaceError,
                                                    "connection is closed")

    def test_raises_error_when_stream_reaise_ioerror(self):
        """[ConnectionTestCase] - Raises IOError when stream throw error"""
        fake_stream = fudge.Fake()
        fake_stream.expects('write').raises(IOError())

        with fudge.patched_context(self.conn, '_stream', fake_stream):

            self.conn.send_message.when.called_with((0, ''), callback=None) \
                .throw(IOError)
Example #8
0
 def setUp(self):
     super(ConnectionTestCase, self).setUp()
     self.conn = Connection(host="localhost", port=27027)
Example #9
0
class ConnectionTestCase(testing.AsyncTestCase):

    def get_new_ioloop(self):
        return IOLoop.instance()

    def setUp(self):
        super(ConnectionTestCase, self).setUp()
        self.conn = Connection(host="localhost", port=27027)

    def tearDown(self):
        super(ConnectionTestCase, self).tearDown()
        self.conn.close()

    def test_not_connect_to_mongo_raises_error(self):
        """[ConnectionTestCase] - Raises error when can't connect to mongo"""

        Connection.when.called_with(host="localhost", port=27000) \
            .should.throw(InterfaceError, "[Errno 61] Connection refused")

    def test_connect_to_mongo(self):
        """[ConnectionTestCase] - Can stabilish connection to mongo"""

        self.conn._connected.should.be.ok

    def test_send_test_message_to_mongo(self):
        """[ConnectionTestCase] - Send message to test driver connection"""

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        result = response['data'][0]

        result['oid'].should.be(object_id)
        result['ok'].should.be(1.0)
        result['str'].should.be(str(object_id))

    def test_close_connection_to_mongo(self):
        """[ConnectionTestCase] - Can close connection to mongo"""

        self.conn.close()

        self.conn._connected.should_not.be.ok
        self.conn._stream.closed().should.be.ok

    @fudge.patch('mongotor.connection.helpers')
    def test_return_integrity_error_when_mongo_return_err(self, fake_helpers):
        """[ConnectionTestCase] - Returns IntegrityError when mongo return a message with err"""

        fake_helpers.expects('_unpack_response') \
            .returns({'data': [{'err': 'shouldBeError', 'code': 1000}]})

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        error.should.be.a('mongotor.errors.IntegrityError')
        error.msg.should.be.equal('shouldBeError')
        error.code.should.be.equal(1000)

    @fudge.patch('mongotor.connection.helpers')
    def test_raises_error_when_cant_unpack_response(self, fake_helpers):
        """[ConnectionTestCase] - Returns DatabaseError when can't unpack response from mongo"""

        fake_helpers.provides('_unpack_response') \
            .raises(DatabaseError('database error'))

        object_id = ObjectId()
        message_test = message.query(0, 'mongotor_test.$cmd', 0, 1,
            {'driverOIDTest': object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        response.should.be.none
        error.should.be.a('mongotor.errors.DatabaseError')

    def test_reconnect_when_connection_was_lost(self):
        """[ConnectionTestCase] - Reconnect to mongo when connection was lost"""

        self.conn.close()
        self.test_send_test_message_to_mongo()

    def test_raises_interface_error_when_cant_reconnect(self):
        """[ConnectionTestCase] - Raises InterfaceError when connection was lost and autoreconnect is False"""

        self.conn = Connection(host="localhost", port=27027, autoreconnect=False)

        self.conn.close()

        self.conn.send_message.when.called_with('shouldBeMessage',
            callback=None).should.throw(InterfaceError, "connection is closed")

    def test_raises_error_when_stream_reaise_ioerror(self):
        """[ConnectionTestCase] - Raises IOError when stream throw error"""
        fake_stream = fudge.Fake()
        fake_stream.expects('write').raises(IOError())

        with fudge.patched_context(self.conn, '_stream', fake_stream):

            self.conn.send_message.when.called_with((0, ''), callback=None) \
                .throw(IOError)
Example #10
0
 def setUp(self):
     super(ConnectionTestCase, self).setUp()
     self.conn = Connection(pool=FakeConnectionPool(),
                            host="localhost",
                            port=27017)
Example #11
0
 def _create_connection(self):
     log.debug('{0} creating new connection'.format(self))
     return Connection(host=self._host, port=self._port, pool=self,
                       autoreconnect=self._autoreconnect)
Example #12
0
 def _create_connection(self):
     host, port = self._addresses[0]
     return Connection(self, host, port, self._autoreconnnect)
Example #13
0
 def setUp(self):
     super(ConnectionTestCase, self).setUp()
     self.conn = Connection(pool=FakeConnectionPool(), host="localhost", port=27017)
Example #14
0
class ConnectionTestCase(testing.AsyncTestCase):
    def get_new_ioloop(self):
        return IOLoop.instance()

    def setUp(self):
        super(ConnectionTestCase, self).setUp()
        self.conn = Connection(pool=FakeConnectionPool(), host="localhost", port=27017)

    def tearDown(self):
        super(ConnectionTestCase, self).tearDown()
        self.conn.close()

    def test_not_connect_to_mongo_raises_error(self):
        """[ConnectionTestCase] - Raises error when can't connect to mongo"""

        Connection.when.called_with(pool=FakeConnectionPool(), host="localhost", port=27000).should.throw(
            InterfaceError, "[Errno 61] Connection refused"
        )

    def test_connect_to_mongo(self):
        """[ConnectionTestCase] - Can stabilish connection to mongo"""

        self.conn._connected.should.be.ok

    def test_send_test_message_to_mongo(self):
        """[ConnectionTestCase] - Send message to test driver connection"""

        object_id = ObjectId()
        message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        result = response["data"][0]

        result["oid"].should.be(object_id)
        result["ok"].should.be(1.0)
        result["str"].should.be(str(object_id))

    def test_close_connection_to_mongo(self):
        """[ConnectionTestCase] - Can close connection to mongo"""

        self.conn.close()

        self.conn._connected.should_not.be.ok
        self.conn._stream.closed().should.be.ok

    @fudge.patch("mongotor.connection.helpers")
    def test_return_integrity_error_when_mongo_return_err(self, fake_helpers):
        """[ConnectionTestCase] - Returns IntegrityError when mongo return a message with err"""

        fake_helpers.expects("_unpack_response").returns({"data": [{"err": "shouldBeError", "code": 1000}]})

        object_id = ObjectId()
        message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        error.should.be.a("mongotor.errors.IntegrityError")
        error.msg.should.be.equal("shouldBeError")
        error.code.should.be.equal(1000)

    @fudge.patch("mongotor.connection.helpers")
    def test_raises_error_when_cant_unpack_response(self, fake_helpers):
        """[ConnectionTestCase] - Returns DatabaseError when can't unpack response from mongo"""

        fake_helpers.provides("_unpack_response").raises(DatabaseError("database error"))

        object_id = ObjectId()
        message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id})

        self.conn.send_message(message_test, callback=self.stop)
        response, error = self.wait()

        response.should.be.none
        error.should.be.a("mongotor.errors.DatabaseError")

    def test_reconnect_when_connection_was_lost(self):
        """[ConnectionTestCase] - Reconnect to mongo when connection was lost"""

        self.conn.close()
        self.test_send_test_message_to_mongo()

    def test_raises_interface_error_when_cant_reconnect(self):
        """[ConnectionTestCase] - Raises InterfaceError when connection was lost and autoreconnect is False"""

        self.conn = Connection(pool=FakeConnectionPool(), host="localhost", port=27017, auto_reconnect=False)

        self.conn.close()

        self.conn.send_message.when.called_with("shouldBeMessage", callback=None).should.throw(
            InterfaceError, "connection is closed"
        )

    def test_raises_error_when_stream_reaise_ioerror(self):
        """[ConnectionTestCase] - Raises IOError when stream throw error"""
        fake_stream = fudge.Fake()
        fake_stream.expects("write").raises(IOError())

        with fudge.patched_context(self.conn, "_stream", fake_stream):

            self.conn.send_message.when.called_with((0, ""), callback=None).throw(IOError)