Пример #1
0
    def test_query(self):
        logging.info('in test_query')
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='test_query',
                               host='127.0.0.1',
                               port=27017,
                               dbname='test',
                               mincached=3)

        def insert_callback(response, error):
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')
            tornado.ioloop.IOLoop.instance().stop()

        db.test_users.insert({"_id": "test_connection.%d" % TEST_TIMESTAMP},
                             safe=True,
                             callback=insert_callback)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')

        def callback(response, error):
            assert len(response) == 1
            test_shunt.register_called('got_record')
            tornado.ioloop.IOLoop.instance().stop()

        db.test_users.find({}, limit=1, callback=callback)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called("got_record")
Пример #2
0
    def test_query(self):
        logging.info('in test_query')
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27018, dbname='test', mincached=3)
        
        def insert_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')

        db.test_users.insert({"_id" : "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')
        
        def callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            assert len(response) == 1
            test_shunt.register_called('got_record')

        db.test_users.find({}, limit=1, callback=callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called("got_record")
Пример #3
0
    def test_authentication(self):
        try:
            test_shunt.setup()
            db = asyncmongo.Client(pool_id='testauth', host='127.0.0.1', port=27017, dbname='test', dbuser='******', dbpass='******', maxconnections=2)
        
            def update_callback(response, error):
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')
                tornado.ioloop.IOLoop.instance().stop()
        
            db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
        
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            def query_callback(response, error):
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')
                tornado.ioloop.IOLoop.instance().stop()
        
            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #4
0
def test_query():
    test_shunt.setup()
    db = asyncmongo.Client(pool_id='test_query', host='10.22.206.203', port=27017, dbname='test', mincached=3)

    def insert_callback(response, error):
        logging.info('insert: %s', response)
        assert len(response) == 1
        test_shunt.register_called('inserted')
        tornado.ioloop.IOLoop.instance().stop()
    
    def find_callback(response, error):
        logging.info('find: %s', response)
        # assert len(response) == 1
        test_shunt.register_called('find')        
        tornado.ioloop.IOLoop.instance().stop()
    
    db.test_users.insert({"_id" : "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called('inserted')
    
    db.album.find_one({}, callback=find_callback)
    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called('find')
    
    def callback(response, error):
        assert len(response) == 1
        test_shunt.register_called('got_record')
        tornado.ioloop.IOLoop.instance().stop()
    
    db.test_users.find({}, limit=1, callback=callback)
    
    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called("got_record")
    def test_duplicate_insert(self):
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='dup_insert', host='127.0.0.1', port=27017, dbname='test')
        
        def insert_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')

        db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')
        
        def duplicate_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            if error:
                test_shunt.register_called('dupe')

        db.test_users.insert({"_id" : "duplicate_insert.%d" % TEST_TIMESTAMP}, callback=duplicate_callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('dupe')
Пример #6
0
    def test_authentication(self):
        try:
            test_shunt.setup()
            db = asyncmongo.Client(pool_id='testauth', host='127.0.0.1', port=27018, dbname='test', dbuser='******',
                                   dbpass='******', maxconnections=2)
        
            def update_callback(response, error):
                logging.info("UPDATE:")
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')

            db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True,
                                 callback=update_callback)

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')

            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #7
0
    def test_duplicate_insert(self):
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='dup_insert',
                               host='127.0.0.1',
                               port=27017,
                               dbname='test')

        def insert_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')

        db.test_users.insert({"_id": "duplicate_insert.%d" % TEST_TIMESTAMP},
                             callback=insert_callback)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')

        def duplicate_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            if error:
                test_shunt.register_called('dupe')

        db.test_users.insert({"_id": "duplicate_insert.%d" % TEST_TIMESTAMP},
                             callback=duplicate_callback)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('dupe')
Пример #8
0
 def test_update_safe(self):
     test_shunt.setup()
     db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test', maxconnections=2)
     
     def update_callback(response, error):
         logging.info(response)
         assert len(response) == 1
         test_shunt.register_called('update')
         tornado.ioloop.IOLoop.instance().stop()
     
     # all of these should be called, but only one should have a callback
     # we also are checking that connections in the pool never increases >1 with max_connections=2
     # this is because connections for safe=False calls get put back in the pool immediated
     db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
     db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
     db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
     db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, safe=False, upsert=True)
     db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
     
     tornado.ioloop.IOLoop.instance().start()
     test_shunt.assert_called('update')
     
     def query_callback(response, error):
         logging.info(response)
         assert isinstance(response, dict)
         assert response['_id'] == TEST_TIMESTAMP
         assert response['test_count'] == 5
         test_shunt.register_called('retrieved')
         tornado.ioloop.IOLoop.instance().stop()
     
     db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
     tornado.ioloop.IOLoop.instance().start()
     test_shunt.assert_called('retrieved')
Пример #9
0
    def test_insert(self):
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test')
        
        def insert_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')

        db.test_users.insert({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')
        
        def query_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('retrieved')

        db.test_users.find_one({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=query_callback)
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('retrieved')

        
        def delete_callback(response, error):
            tornado.ioloop.IOLoop.instance().stop()
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('deleted')

        db.test_users.remove({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=delete_callback)
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('deleted')
Пример #10
0
    def test_insert(self):
        test_shunt.setup()
        db = asyncmongo.Client(pool_id='testinsert', host='127.0.0.1', port=27017, dbname='test')
        
        def insert_callback(response, error):
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')
            tornado.ioloop.IOLoop.instance().stop()
        
        db.test_users.insert({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=insert_callback)
        
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')
        
        def query_callback(response, error):
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('retrieved')
            tornado.ioloop.IOLoop.instance().stop()
        
        db.test_users.find_one({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=query_callback)
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('retrieved')

        
        def delete_callback(response, error):
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('deleted')
            tornado.ioloop.IOLoop.instance().stop()
        
        db.test_users.remove({"_id" : "insert.%d" % TEST_TIMESTAMP}, callback=delete_callback)
        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('deleted')
Пример #11
0
def test_duplicate_insert():
    test_shunt.setup()
    db = asyncmongo.Client(pool_id="dup_insert", host="127.0.0.1", port=27017, dbname="test")

    def insert_callback(response, error):
        logging.info(response)
        assert len(response) == 1
        test_shunt.register_called("inserted")
        tornado.ioloop.IOLoop.instance().stop()

    db.test_users.insert({"_id": "duplicate_insert.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)

    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called("inserted")

    def duplicate_callback(response, error):
        logging.info(response)
        if error:
            test_shunt.register_called("dupe")
        tornado.ioloop.IOLoop.instance().stop()

    db.test_users.insert({"_id": "duplicate_insert.%d" % TEST_TIMESTAMP}, safe=True, callback=duplicate_callback)

    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called("dupe")
Пример #12
0
def test_update_safe():
    test_shunt.setup()
    db = asyncmongo.Client(pool_id='testinsert',
                           host='127.0.0.1',
                           port=27017,
                           dbname='test',
                           maxconnections=2)

    def update_callback(response, error):
        logging.info(response)
        assert len(response) == 1
        test_shunt.register_called('update')
        tornado.ioloop.IOLoop.instance().stop()

    # all of these should be called, but only one should have a callback
    # we also are checking that connections in the pool never increases >1 with max_connections=2
    # this is because connections for safe=False calls get put back in the pool immediated
    db.test_stats.update({"_id": TEST_TIMESTAMP}, {'$inc': {
        'test_count': 1
    }},
                         safe=False,
                         upsert=True)
    db.test_stats.update({"_id": TEST_TIMESTAMP}, {'$inc': {
        'test_count': 1
    }},
                         safe=False,
                         upsert=True)
    db.test_stats.update({"_id": TEST_TIMESTAMP}, {'$inc': {
        'test_count': 1
    }},
                         safe=False,
                         upsert=True)
    db.test_stats.update({"_id": TEST_TIMESTAMP}, {'$inc': {
        'test_count': 1
    }},
                         safe=False,
                         upsert=True)
    db.test_stats.update({"_id": TEST_TIMESTAMP}, {'$inc': {
        'test_count': 1
    }},
                         upsert=True,
                         callback=update_callback)

    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called('update')

    def query_callback(response, error):
        logging.info(response)
        assert isinstance(response, dict)
        assert response['_id'] == TEST_TIMESTAMP
        assert response['test_count'] == 5
        test_shunt.register_called('retrieved')
        tornado.ioloop.IOLoop.instance().stop()

    db.test_stats.find_one({"_id": TEST_TIMESTAMP}, callback=query_callback)
    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called('retrieved')
Пример #13
0
    def test_query_slave(self):
        try:
            test_shunt.setup()
            masterdb = asyncmongo.Client(pool_id='testquerymaster',
                                         host='127.0.0.1',
                                         port=27017,
                                         dbname='test',
                                         maxconnections=2)
            slavedb = asyncmongo.Client(pool_id='testqueryslave',
                                        host='127.0.0.1',
                                        port=27018,
                                        dbname='test',
                                        maxconnections=2,
                                        slave_okay=True)
            logging.debug(
                'waiting for replication to start (sleeping 4 seconds)')
            time.sleep(4)

            def update_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')

            masterdb.test_stats.update({"_id": TEST_TIMESTAMP},
                                       {'$inc': {
                                           'test_count': 1
                                       }},
                                       upsert=True,
                                       callback=update_callback)

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            # wait for the insert to get to the slave
            time.sleep(2.5)

            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')

            slavedb.test_stats.find_one({"_id": TEST_TIMESTAMP},
                                        callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #14
0
    def test_pooled_db(self):
        """
        This tests simply verifies that we can grab two different connections from the pool
        and use them independantly.
        """
        print asyncmongo.__file__
        test_shunt.setup()
        client = asyncmongo.Client('id1',
                                   maxconnections=5,
                                   host='127.0.0.1',
                                   port=27017,
                                   dbname='test')
        test_users_collection = client.connection('test_users')

        def insert_callback(response, error):
            logging.info(response)
            assert len(response) == 1
            test_shunt.register_called('inserted')
            tornado.ioloop.IOLoop.instance().stop()

        test_users_collection.insert(
            {"_id": "record_test.%d" % TEST_TIMESTAMP},
            safe=True,
            callback=insert_callback)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('inserted')

        def pool_callback(response, error):
            assert len(response) == 1
            test_shunt.register_called('pool1')
            if test_shunt.is_called('pool2'):
                tornado.ioloop.IOLoop.instance().stop()

        def pool_callback2(response, error):
            assert len(response) == 1
            test_shunt.register_called('pool2')

            if test_shunt.is_called('pool1'):
                # don't expect 2 finishes second
                tornado.ioloop.IOLoop.instance().stop()

        test_users_collection.find({}, limit=1, callback=pool_callback)
        test_users_collection.find({}, limit=1, callback=pool_callback2)

        tornado.ioloop.IOLoop.instance().start()
        test_shunt.assert_called('pool1')
        test_shunt.assert_called('pool2')
    def test_authentication(self):
        try:
            test_shunt.setup()
            db = asyncmongo.Client(
                pool_id="testauth",
                host="127.0.0.1",
                port=27018,
                dbname="test",
                dbuser="******",
                dbpass="******",
                maxconnections=2,
            )

            def update_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called("update")

            db.test_stats.update(
                {"_id": TEST_TIMESTAMP}, {"$inc": {"test_count": 1}}, upsert=True, callback=update_callback
            )

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called("update")

            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response["_id"] == TEST_TIMESTAMP
                assert response["test_count"] == 1
                test_shunt.register_called("retrieved")

            db.test_stats.find_one({"_id": TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called("retrieved")
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #16
0
    def test_failed_auth(self):
        try:
            test_shunt.setup()
            db = asyncmongo.Client(pool_id='testauth_f', host='127.0.0.1', port=27018, dbname='test', dbuser='******',
                                   dbpass='******', maxconnections=2)

            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert isinstance(error, asyncmongo.AuthenticationError)
                assert response is None
                test_shunt.register_called('auth_failed')

            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('auth_failed')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #17
0
 def test_pooled_db(self):
     """
     This tests simply verifies that we can grab two different connections from the pool
     and use them independantly.
     """
     print asyncmongo.__file__
     test_shunt.setup()
     client = asyncmongo.Client('id1', maxconnections=5, host='127.0.0.1', port=27017, dbname='test')
     test_users_collection = client.connection('test_users')
     
     def insert_callback(response, error):
         logging.info(response)
         assert len(response) == 1
         test_shunt.register_called('inserted')
         tornado.ioloop.IOLoop.instance().stop()
     
     test_users_collection.insert({"_id" : "record_test.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)
     
     tornado.ioloop.IOLoop.instance().start()
     test_shunt.assert_called('inserted')
     
     def pool_callback(response, error):
         assert len(response) == 1
         test_shunt.register_called('pool1')
         if test_shunt.is_called('pool2'):
             tornado.ioloop.IOLoop.instance().stop()
     
     def pool_callback2(response, error):
         assert len(response) == 1
         test_shunt.register_called('pool2')
         
         if test_shunt.is_called('pool1'):
             # don't expect 2 finishes second
             tornado.ioloop.IOLoop.instance().stop()
     
     test_users_collection.find({}, limit=1, callback=pool_callback)
     test_users_collection.find({}, limit=1, callback=pool_callback2)
     
     tornado.ioloop.IOLoop.instance().start()
     test_shunt.assert_called('pool1')
     test_shunt.assert_called('pool2')
Пример #18
0
    def test_query_slave(self):
        try:
            test_shunt.setup()
            masterdb = asyncmongo.Client(pool_id='testquerymaster', host='127.0.0.1', port=27017, dbname='test', maxconnections=2)
            slavedb = asyncmongo.Client(pool_id='testqueryslave', host='127.0.0.1', port=27018, dbname='test', maxconnections=2, slave_okay=True)
            logging.debug('waiting for replication to start (sleeping 4 seconds)')
            time.sleep(4)
        
            def update_callback(response, error):
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')
                tornado.ioloop.IOLoop.instance().stop()
        
            masterdb.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)
        
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            # wait for the insert to get to the slave
            time.sleep(2.5)
        
            def query_callback(response, error):
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')
                tornado.ioloop.IOLoop.instance().stop()
        
            slavedb.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #19
0
def test_query():
    test_shunt.setup()
    db = asyncmongo.Client(pool_id="test_query", host="127.0.0.1", port=27017, dbname="test")

    def insert_callback(response, error):
        logging.info(response)
        assert len(response) == 1
        test_shunt.register_called("inserted")
        tornado.ioloop.IOLoop.instance().stop()

    db.test_users.insert({"_id": "test_connection.%d" % TEST_TIMESTAMP}, safe=True, callback=insert_callback)

    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called("inserted")

    def callback(response, error):
        assert len(response) == 1
        test_shunt.register_called("got_record")
        tornado.ioloop.IOLoop.instance().stop()

    db.test_users.find({}, limit=1, callback=callback)

    tornado.ioloop.IOLoop.instance().start()
    test_shunt.assert_called("got_record")
Пример #20
0
    def test_update(self):
        try:
            test_shunt.setup()
            db = asyncmongo.Client(pool_id='testrs', rs="rs0", seed=[("127.0.0.1", 27020)], dbname='test', maxconnections=2)

            # Update
            def update_callback(response, error):
                logging.info("UPDATE:")
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')

            db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback)

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            # Retrieve the updated value
            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')

            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')

            # Switch the master
            self.mongo_cmd(
                "cfg = rs.conf(); cfg.members[1].priority = 1; cfg.members[0].priority = 2; rs.reconfig(cfg);",
                27019, "reconnected to server")
            self.wait_master(27018)

            # Expect the connection to be closed
            def query_err_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert isinstance(error, Exception)

            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_err_callback)
            tornado.ioloop.IOLoop.instance().start()

            # Retrieve the updated value again, from the new master
            def query_again_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved_again')

            db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_again_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved_again')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise
Пример #21
0
    def test_update(self):
        try:
            test_shunt.setup()

            db = asyncmongo.Client(pool_id='testrs_f',
                                   rs="wrong_rs",
                                   seed=[("127.0.0.1", 27020)],
                                   dbname='test',
                                   maxconnections=2)

            # Try to update with a wrong replica set name
            def update_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert isinstance(error, asyncmongo.RSConnectionError)
                test_shunt.register_called('update_f')

            db.test_stats.update({"_id": TEST_TIMESTAMP},
                                 {'$inc': {
                                     'test_count': 1
                                 }},
                                 callback=update_callback)

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update_f')

            db = asyncmongo.Client(pool_id='testrs',
                                   rs="rs0",
                                   seed=[("127.0.0.1", 27020)],
                                   dbname='test',
                                   maxconnections=2)

            # Update
            def update_callback(response, error):
                logging.info("UPDATE:")
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                assert len(response) == 1
                test_shunt.register_called('update')

            db.test_stats.update({"_id": TEST_TIMESTAMP},
                                 {'$inc': {
                                     'test_count': 1
                                 }},
                                 upsert=True,
                                 callback=update_callback)

            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('update')

            # Retrieve the updated value
            def query_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved')

            db.test_stats.find_one({"_id": TEST_TIMESTAMP},
                                   callback=query_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved')

            # Switch the master
            self.mongo_cmd(
                "cfg = rs.conf(); cfg.members[1].priority = 1; cfg.members[0].priority = 2; rs.reconfig(cfg);",
                27019, "reconnected to server")
            self.wait_master(27018)

            # Expect the connection to be closed
            def query_err_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert isinstance(error, Exception)

            db.test_stats.find_one({"_id": TEST_TIMESTAMP},
                                   callback=query_err_callback)
            tornado.ioloop.IOLoop.instance().start()

            # Retrieve the updated value again, from the new master
            def query_again_callback(response, error):
                tornado.ioloop.IOLoop.instance().stop()
                logging.info(response)
                logging.info(error)
                assert error is None
                assert isinstance(response, dict)
                assert response['_id'] == TEST_TIMESTAMP
                assert response['test_count'] == 1
                test_shunt.register_called('retrieved_again')

            db.test_stats.find_one({"_id": TEST_TIMESTAMP},
                                   callback=query_again_callback)
            tornado.ioloop.IOLoop.instance().start()
            test_shunt.assert_called('retrieved_again')
        except:
            tornado.ioloop.IOLoop.instance().stop()
            raise