def for_handler( cls, handler_cls, # Type[BaseHTTPFactory] ap_settings, # type: AutopushSettings db=None, # type: Optional[DatabaseManager] **kwargs): # type: (...) -> BaseHTTPFactory """Create a cyclone app around a specific handler_cls for tests. Creates an uninitialized (no setup() called) DatabaseManager from settings if one isn't specified. handler_cls must be included in ap_handlers or a ValueError is thrown. """ if 'handlers' in kwargs: # pragma: nocover raise ValueError("handler_cls incompatibile with handlers kwarg") for pattern, handler in cls.ap_handlers + cls.health_ap_handlers: if handler is handler_cls: if db is None: db = DatabaseManager.from_settings(ap_settings) return cls._for_handler(ap_settings, db=db, handlers=[(pattern, handler)], **kwargs) raise ValueError( "{!r} not in ap_handlers".format(handler_cls)) # pragma: nocover
def test_update_rotating_tables(self): from autopush.db import get_month settings = AutopushSettings(hostname="example.com", resolve_hostname=True) db = DatabaseManager.from_settings(settings) db.create_initial_message_tables() # Erase the tables it has on init, and move current month back one last_month = get_month(-1) db.current_month = last_month.month db.message_tables = {} # Create the next month's table, just in case today is the day before # a new month, in which case the lack of keys will cause an error in # update_rotating_tables next_month = get_month(1) db.message_tables[next_month.month] = None # Get the deferred back e = Deferred() d = db.update_rotating_tables() def check_tables(result): eq_(len(db.message_tables), 2) eq_(db.current_month, get_month().month) d.addCallback(check_tables) d.addBoth(lambda x: e.callback(True)) return e
def __init__(self, sysargs, use_files=True): args = self._load_args(sysargs, use_files) self._settings = settings = AutopushSettings( crypto_key=args.crypto_key, router_tablename=args.router_tablename, storage_tablename=args.storage_tablename, message_tablename=args.message_tablename, statsd_host=None, ) self.db = DatabaseManager.from_settings(settings) self.db.setup(settings.preflight_uaid) self._endpoint = args.endpoint self._pp = pprint.PrettyPrinter(indent=4)
def test_update_rotating_tables_month_end(self): """Test that rotating adds next months table This test is intended to ensure that if the next day is a new month, then update_rotating_tables realizes this and add's the new table to the message_tables. A pre-requisite is that today cannot be the last day of the current month. Therefore, we first sub in _tomorrow to ensure it always appears as next month, and then remove the new table create_initial_tables made so we can observe update_rotating_tables add the new one. Note that sorting message table keys to find the last month does *not work* since the month digit is not zero-padded. """ today = datetime.date.today() next_month = today.month + 1 next_year = today.year if next_month > 12: # pragma: nocover next_month = 1 next_year += 1 tomorrow = datetime.datetime(year=next_year, month=next_month, day=1) settings = AutopushSettings(hostname="example.com", resolve_hostname=True) db = DatabaseManager.from_settings(settings) db._tomorrow = Mock(return_value=tomorrow) db.create_initial_message_tables() # We should have 3 tables, one for next/this/last month eq_(len(db.message_tables), 3) # Grab next month's table name and remove it next_month = get_rotating_message_table(db._message_prefix, delta=1) db.message_tables.pop(next_month.table_name) # Get the deferred back d = db.update_rotating_tables() def check_tables(result): eq_(len(db.message_tables), 3) ok_(next_month.table_name in db.message_tables) d.addCallback(check_tables) return d
def __init__(self, settings): # type: (AutopushSettings) -> None super(AutopushMultiService, self).__init__() self.settings = settings self.db = DatabaseManager.from_settings(settings) self.agent = agent_from_settings(settings)