Skip to content
This repository
tree: bf6919a956
Fetching contributors…

Cannot retrieve contributors at this time

file 52 lines (43 sloc) 2.067 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import tornado.ioloop
import time
import logging

import test_shunt
import asyncmongo

TEST_TIMESTAMP = int(time.time())

class SlaveOnlyTest(test_shunt.MongoTest):
    mongod_options = [
        ('--port', '27017', '--master'),
        ('--port', '27018', '--slave', '--source', '127.0.0.1:27017'),
    ]
    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
Something went wrong with that request. Please try again.