def test_message_storage(self): chid = str(uuid.uuid4()) chid2 = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) message.register_channel(self.uaid, chid2) data1 = str(uuid.uuid4()) data2 = str(uuid.uuid4()) ttl = int(time.time())+100 time1, time2, time3 = self._nstime(), self._nstime(), self._nstime()+1 message.store_message(self.uaid, chid, time1, ttl, data1, {}) message.store_message(self.uaid, chid2, time2, ttl, data2, {}) message.store_message(self.uaid, chid2, time3, ttl, data1, {}) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 3) message.delete_messages_for_channel(self.uaid, chid2) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 1) message.delete_message(self.uaid, chid, time1) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 0)
def test_message_storage(self): chid = str(uuid.uuid4()) chid2 = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) message.register_channel(self.uaid, chid2) data1 = str(uuid.uuid4()) data2 = str(uuid.uuid4()) ttl = int(time.time()) + 100 time1, time2, time3 = self._nstime(), self._nstime( ), self._nstime() + 1 message.store_message(self.uaid, chid, time1, ttl, data1, {}) message.store_message(self.uaid, chid2, time2, ttl, data2, {}) message.store_message(self.uaid, chid2, time3, ttl, data1, {}) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 3) message.delete_messages_for_channel(self.uaid, chid2) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 1) message.delete_message(self.uaid, chid, time1) all_messages = list(message.fetch_messages(self.uaid)) eq_(len(all_messages), 0)
def test_message_delete_pagination(self): def make_messages(channel_id, count): m = [] t = self._nstime() ttl = int(time.time()) + 200 for i in range(count): m.append( (self.uaid, channel_id, str(uuid.uuid4()), ttl, {}, t + i)) return m chid = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) # Shove 80 messages in for message_args in make_messages(chid, 80): message.store_message(*message_args) # Verify we can see them all all_messages = list(message.fetch_messages(self.uaid, limit=100)) eq_(len(all_messages), 80) # Delete them all message.delete_messages_for_channel(self.uaid, chid) # Verify they're gone all_messages = list(message.fetch_messages(self.uaid, limit=100)) eq_(len(all_messages), 0)
def test_message_delete_pagination(self): def make_messages(channel_id, count): m = [] t = self._nstime() ttl = int(time.time())+200 for i in range(count): m.append( (self.uaid, channel_id, str(uuid.uuid4()), ttl, {}, t+i) ) return m chid = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) # Shove 80 messages in for message_args in make_messages(chid, 80): message.store_message(*message_args) # Verify we can see them all all_messages = list(message.fetch_messages(self.uaid, limit=100)) eq_(len(all_messages), 80) # Delete them all message.delete_messages_for_channel(self.uaid, chid) # Verify they're gone all_messages = list(message.fetch_messages(self.uaid, limit=100)) eq_(len(all_messages), 0)
def test_register(self): chid = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) # Verify its in the db rows = m.query_2(uaid__eq=self.uaid, chidmessageid__eq=" ") results = list(rows) assert (len(results) == 1)
def test_register(self): chid = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) # Verify its in the db rows = m.query_2(uaid__eq=self.uaid, chidmessageid__eq=" ") results = list(rows) assert(len(results) == 1)
def test_message_delete_fail_condition(self): m = get_message_table() message = Message(m, SinkMetrics()) def raise_condition(*args, **kwargs): raise ConditionalCheckFailedException(None, None) message.table = Mock() message.table.delete_item.side_effect = raise_condition result = message.delete_message(uaid="asdf", channel_id="asdf", message_id="asdf", updateid="asdf") eq_(result, False)
def test_update_message(self): chid = uuid.uuid4().hex m = get_message_table() message = Message(m, SinkMetrics()) data1 = str(uuid.uuid4()) data2 = str(uuid.uuid4()) time1 = self._nstime() time2 = self._nstime() + 100 ttl = self._nstime() + 1000 message.store_message(self.uaid, chid, time1, ttl, data1, {}) message.update_message(self.uaid, chid, time2, ttl, data2, {}) messages = list(message.fetch_messages(self.uaid)) eq_(data2, messages[0]['#dd'])
def test_update_message(self): chid = uuid.uuid4().hex m = get_message_table() message = Message(m, SinkMetrics()) data1 = str(uuid.uuid4()) data2 = str(uuid.uuid4()) time1 = self._nstime() time2 = self._nstime()+100 ttl = self._nstime()+1000 message.store_message(self.uaid, chid, time1, ttl, data1, {}) message.update_message(self.uaid, chid, time2, ttl, data2, {}) messages = list(message.fetch_messages(self.uaid)) eq_(data2, messages[0]['#dd'])
def test_save_channels(self): chid = str(uuid.uuid4()) chid2 = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) message.register_channel(self.uaid, chid2) exists, chans = message.all_channels(self.uaid) new_uaid = uuid.uuid4().hex message.save_channels(new_uaid, chans) _, new_chans = message.all_channels(new_uaid) eq_(chans, new_chans)
def test_update_message_fail(self): message = Message(get_message_table(), SinkMetrics) message.store_message(self.uaid, uuid.uuid4().hex, self._nstime(), str(uuid.uuid4()), {}) u = message.table.connection.update_item = Mock() def raise_condition(*args, **kwargs): raise ConditionalCheckFailedException(None, None) u.side_effect = raise_condition b = message.update_message(self.uaid, uuid.uuid4().hex, self._nstime(), str(uuid.uuid4()), {}) eq_(b, False)
def test_all_channels(self): chid = str(uuid.uuid4()) chid2 = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) message.register_channel(self.uaid, chid2) chans = message.all_channels(self.uaid) assert (chid in chans) assert (chid2 in chans) message.unregister_channel(self.uaid, chid2) chans = message.all_channels(self.uaid) assert (chid2 not in chans) assert (chid in chans)
def test_all_channels(self): chid = str(uuid.uuid4()) chid2 = str(uuid.uuid4()) m = get_message_table() message = Message(m, SinkMetrics()) message.register_channel(self.uaid, chid) message.register_channel(self.uaid, chid2) chans = message.all_channels(self.uaid) assert(chid in chans) assert(chid2 in chans) message.unregister_channel(self.uaid, chid2) chans = message.all_channels(self.uaid) assert(chid2 not in chans) assert(chid in chans)
def test_all_channels_no_uaid(self): m = get_message_table() message = Message(m, SinkMetrics()) assert (message.all_channels("asdf") == set([]))
def test_all_channels_no_uaid(self): m = get_message_table() message = Message(m, SinkMetrics()) assert(message.all_channels("asdf") == set([]))
def setUp(self): table = get_message_table() self.real_table = table self.real_connection = table.connection self.uaid = str(uuid.uuid4())
def __init__(self, crypto_key=None, datadog_api_key=None, datadog_app_key=None, datadog_flush_interval=None, hostname=None, port=None, router_scheme=None, router_hostname=None, router_port=None, endpoint_scheme=None, endpoint_hostname=None, endpoint_port=None, router_conf={}, router_tablename="router", router_read_throughput=5, router_write_throughput=5, storage_tablename="storage", storage_read_throughput=5, storage_write_throughput=5, message_tablename="message", message_read_throughput=5, message_write_throughput=5, statsd_host="localhost", statsd_port=8125, resolve_hostname=False, max_data=4096, # Reflected up from UDP Router wake_timeout=0, env='development', enable_cors=False, s3_bucket=DEFAULT_BUCKET, senderid_expry=SENDERID_EXPRY, senderid_list={}, hello_timeout=0, auth_key=None, ): """Initialize the Settings object Upon creation, the HTTP agent will initialize, all configured routers will be setup and started, logging will be started, and the database will have a preflight check done. """ # Use a persistent connection pool for HTTP requests. pool = HTTPConnectionPool(reactor) self.agent = Agent(reactor, connectTimeout=5, pool=pool) # Metrics setup if datadog_api_key: self.metrics = DatadogMetrics( api_key=datadog_api_key, app_key=datadog_app_key, flush_interval=datadog_flush_interval ) elif statsd_host: self.metrics = TwistedMetrics(statsd_host, statsd_port) else: self.metrics = SinkMetrics() if not crypto_key: crypto_key = [Fernet.generate_key()] if not isinstance(crypto_key, list): crypto_key = [crypto_key] self.update(crypto_key=crypto_key) self.crypto_key = crypto_key if auth_key is None: auth_key = [] if not isinstance(auth_key, list): auth_key = [auth_key] self.auth_key = auth_key self.max_data = max_data self.clients = {} # Setup hosts/ports/urls default_hostname = socket.gethostname() self.hostname = hostname or default_hostname if resolve_hostname: self.hostname = resolve_ip(self.hostname) self.port = port self.endpoint_hostname = endpoint_hostname or self.hostname self.router_hostname = router_hostname or self.hostname self.router_conf = router_conf self.router_url = canonical_url( router_scheme or 'http', self.router_hostname, router_port ) self.endpoint_url = canonical_url( endpoint_scheme or 'http', self.endpoint_hostname, endpoint_port ) # Database objects self.router_table = get_router_table(router_tablename, router_read_throughput, router_write_throughput) self.storage_table = get_storage_table(storage_tablename, storage_read_throughput, storage_write_throughput) self.message_table = get_message_table(message_tablename, message_read_throughput, message_write_throughput) self.storage = Storage(self.storage_table, self.metrics) self.router = Router(self.router_table, self.metrics) self.message = Message(self.message_table, self.metrics) # Run preflight check preflight_check(self.storage, self.router) # CORS self.cors = enable_cors # Force timeout in idle seconds self.wake_timeout = wake_timeout # Setup the routers self.routers = {} self.routers["simplepush"] = SimpleRouter( self, router_conf.get("simplepush") ) self.routers["webpush"] = WebPushRouter(self, None) if 'apns' in router_conf: self.routers["apns"] = APNSRouter(self, router_conf["apns"]) if 'gcm' in router_conf: self.routers["gcm"] = GCMRouter(self, router_conf["gcm"]) # Env self.env = env self.hello_timeout = hello_timeout
def __init__( self, crypto_key=None, datadog_api_key=None, datadog_app_key=None, datadog_flush_interval=None, hostname=None, port=None, router_scheme=None, router_hostname=None, router_port=None, endpoint_scheme=None, endpoint_hostname=None, endpoint_port=None, router_conf={}, router_tablename="router", router_read_throughput=5, router_write_throughput=5, storage_tablename="storage", storage_read_throughput=5, storage_write_throughput=5, message_tablename="message", message_read_throughput=5, message_write_throughput=5, statsd_host="localhost", statsd_port=8125, resolve_hostname=False, max_data=4096, # Reflected up from UDP Router wake_timeout=0, env='development', enable_cors=False, s3_bucket=DEFAULT_BUCKET, senderid_expry=SENDERID_EXPRY, senderid_list={}, hello_timeout=0, ): """Initialize the Settings object Upon creation, the HTTP agent will initialize, all configured routers will be setup and started, logging will be started, and the database will have a preflight check done. """ # Use a persistent connection pool for HTTP requests. pool = HTTPConnectionPool(reactor) self.agent = Agent(reactor, connectTimeout=5, pool=pool) # Metrics setup if datadog_api_key: self.metrics = DatadogMetrics( api_key=datadog_api_key, app_key=datadog_app_key, flush_interval=datadog_flush_interval) elif statsd_host: self.metrics = TwistedMetrics(statsd_host, statsd_port) else: self.metrics = SinkMetrics() if not crypto_key: crypto_key = [Fernet.generate_key()] if not isinstance(crypto_key, list): crypto_key = [crypto_key] self.update(crypto_key=crypto_key) self.crypto_key = crypto_key self.max_data = max_data self.clients = {} # Setup hosts/ports/urls default_hostname = socket.gethostname() self.hostname = hostname or default_hostname if resolve_hostname: self.hostname = resolve_ip(self.hostname) self.port = port self.endpoint_hostname = endpoint_hostname or self.hostname self.router_hostname = router_hostname or self.hostname self.router_conf = router_conf self.router_url = canonical_url(router_scheme or 'http', self.router_hostname, router_port) self.endpoint_url = canonical_url(endpoint_scheme or 'http', self.endpoint_hostname, endpoint_port) # Database objects self.router_table = get_router_table(router_tablename, router_read_throughput, router_write_throughput) self.storage_table = get_storage_table(storage_tablename, storage_read_throughput, storage_write_throughput) self.message_table = get_message_table(message_tablename, message_read_throughput, message_write_throughput) self.storage = Storage(self.storage_table, self.metrics) self.router = Router(self.router_table, self.metrics) self.message = Message(self.message_table, self.metrics) # Run preflight check preflight_check(self.storage, self.router) # CORS self.cors = enable_cors # Force timeout in idle seconds self.wake_timeout = wake_timeout # Setup the routers self.routers = {} self.routers["simplepush"] = SimpleRouter( self, router_conf.get("simplepush")) self.routers["webpush"] = WebPushRouter(self, None) if 'apns' in router_conf: self.routers["apns"] = APNSRouter(self, router_conf["apns"]) if 'gcm' in router_conf: self.routers["gcm"] = GCMRouter(self, router_conf["gcm"]) # Env self.env = env self.hello_timeout = hello_timeout