def test_removes_old_sessions_and_associated_collections(self,
                                                             datetime_mock):
        dates = [
            datetime(2012, 7, 2),
            datetime(2012, 7, 3),
            datetime(2012, 7, 4),
            datetime(2012, 7, 5),
        ]
        for session_res_id, date in enumerate(dates):
            collections = []
            for collection in xrange(3):
                collections.append(collection)
                collection_name = to_coll_name(session_res_id,
                                                         collection)
                self.db[collection_name].insert({'foo': 'barr'})

            self.db.clients.insert({
                'session_id': session_res_id,
                'res_id': session_res_id,
                'collections': collections,
                'timestamp': date,
            })

        # Want to get rid of everything before 2012/7/4
        delta = timedelta(seconds=crontab.EXPIRE_SESSION_DURATION)
        datetime_mock.now.return_value = datetime(2012, 7, 4) - delta

        crontab.expire_sessions(self.real_app)

        # Should grab all remaining records we inserted
        res = self.db.clients.find({
            'timestamp': {'$lt': self.test_before_date}
        })
        self.assertEqual(res.count(), 2)
        actual_dates = [r['timestamp'] for r in res]
        expected_dates = dates[-2:]  # Only the last two should survive
        self.assertItemsEqual(actual_dates, expected_dates)

        # Make sure collections were dropped
        coll_names = self.db.collection_names()
        self.assertNotIn('00', coll_names)
        self.assertNotIn('01', coll_names)
        self.assertNotIn('02', coll_names)
        self.assertNotIn('10', coll_names)
        self.assertNotIn('11', coll_names)
        self.assertNotIn('12', coll_names)

        self.assertIn('20', coll_names)
        self.assertIn('21', coll_names)
        self.assertIn('22', coll_names)
        self.assertIn('30', coll_names)
        self.assertIn('31', coll_names)
        self.assertIn('32', coll_names)

        for name in ['20', '21', '22', '30', '31', '32']:
            self.db[name].drop()
示例#2
0
    def setUp(self):
        super(DBCollectionTestCase, self).setUp()

        self.coll_name = 'test_collection'
        self.internal_coll_name = to_coll_name(self.res_id, self.coll_name)
        self.db = get_db()
        self.db_collection = self.db[self.internal_coll_name]

        self.make_request_url = '/mws/%s/db/%s/%%s' % \
                                (self.res_id, self.coll_name)
示例#3
0
    def setUp(self):
        super(DBCollectionTestCase, self).setUp()

        self.coll_name = 'test_collection'
        self.internal_coll_name = to_coll_name(self.res_id,
                                                         self.coll_name)
        self.db = get_db()
        self.db_collection = self.db[self.internal_coll_name]

        self.make_request_url = '/mws/%s/db/%s/%%s' % \
                                (self.res_id, self.coll_name)
def expire_sessions(app):
    with app.app_context():
        db = get_db(MWSExceptions=False)
        delta = timedelta(seconds=EXPIRE_SESSION_DURATION)
        exp = datetime.now() - delta
        sessions = db.clients.find({'timestamp': {'$lt': exp}})
        for sess in sessions:
            db.clients.remove(sess)
            # Todo: Only remove collections if no one else is using this res_id
            res_id = sess['res_id']
            for c in sess['collections']:
                db.drop_collection(to_coll_name(res_id, c))
        app.logger.info('Timed out expired sessions dead before %s' % exp)
示例#5
0
    def test_drop_db(self):
        testdoc = {'name': 'Mongo'}
        colls = ['a', 'b', 'c']
        update = {'$addToSet': {'collections': {'$each': colls}}}
        self.db[CLIENTS_COLLECTION].update({'res_id': self.res_id}, update)
        colls = [to_coll_name(self.res_id, c) for c in colls]
        for col in colls:
            self.db[col].insert(testdoc)

        actual_colls = self.db.collection_names()
        for col in colls:
            self.assertIn(col, actual_colls)

        self.make_db_drop_request()

        actual_colls = self.db.collection_names()
        for col in colls:
            self.assertNotIn(col, actual_colls)

        self.assertItemsEqual(get_collection_names(self.res_id), [])
示例#6
0
    def test_drop_db(self):
        testdoc = {'name': 'Mongo'}
        colls = ['a', 'b', 'c']
        update = {'$addToSet': {'collections': {'$each': colls}}}
        self.db[CLIENTS_COLLECTION].update({'res_id': self.res_id}, update)
        colls = [to_coll_name(self.res_id, c) for c in colls]
        for col in colls:
            self.db[col].insert(testdoc)

        actual_colls = self.db.collection_names()
        for col in colls:
            self.assertIn(col, actual_colls)

        self.make_db_drop_request()

        actual_colls = self.db.collection_names()
        for col in colls:
            self.assertNotIn(col, actual_colls)

        self.assertItemsEqual(get_collection_names(self.res_id), [])