Exemplo n.º 1
0
 def test_incr(self):
     self.r_server = FakeRedis()
     self.r_server.set("inc", 1)
     self.assertEqual('1', self.r_server.get("inc"))
     self.assertEqual(2, self.r_server.incr("inc"))
     self.assertEqual(3, self.r_server.incr("inc"))
     self.assertEqual('3', self.r_server.get("inc"))
Exemplo n.º 2
0
 def test_sunion(self):
     self.r_server = FakeRedis()
     self.r_server.sadd('set1', 1)
     self.r_server.sadd('set2', 2)
     self.assertEqual(self.r_server.sunion('set1'), set(['1']))
     self.assertEqual(self.r_server.sunion('set1', 'set2'), set(['1', '2']))
     self.assertEqual(self.r_server.sunion('other'), set())
Exemplo n.º 3
0
 def setUp(self):
     yield super(TestContentKeywordRouter, self).setUp()
     self.config = {
         'dispatcher_name':
         'keyword_dispatcher',
         'router_class':
         'vumi.dispatchers.base.ContentKeywordRouter',
         'transport_names': ['transport1', 'transport2'],
         'transport_mappings': {
             'shortcode1': 'transport1',
             'shortcode2': 'transport2',
         },
         'exposed_names': ['app1', 'app2', 'app3', 'fallback_app'],
         'rules': [{
             'app': 'app1',
             'keyword': 'KEYWORD1',
             'to_addr': '8181',
             'prefix': '+256',
         }, {
             'app': 'app2',
             'keyword': 'KEYWORD2',
         }],
         'keyword_mappings': {
             'app2': 'KEYWORD3',
             'app3': 'KEYWORD1',
         },
         'fallback_application':
         'fallback_app',
         'expire_routing_memory':
         '3',
     }
     self.fake_redis = FakeRedis()
     self.dispatcher = yield self.get_dispatcher(self.config)
     self.router = self.dispatcher._router
     self.router.r_server = self.fake_redis
Exemplo n.º 4
0
 def setUp(self):
     self.today = datetime.utcnow().date()
     self.port = 9999
     self.path = '/api/v1/sms/vas2nets/receive/'
     self.config = {
         'transport_name': 'vas2nets',
         'url': 'http://localhost:%s%s' % (self.port, self.path),
         'username': '******',
         'password': '******',
         'owner': 'owner',
         'service': 'service',
         'subservice': 'subservice',
         'web_receive_path': '/receive',
         'web_receipt_path': '/receipt',
         'web_port': 9998,
     }
     self.fail_config = {
         'transport_name': 'vas2nets',
         'retry_routing_key': '%(transport_name)s.outbound',
         'failures_routing_key': '%(transport_name)s.failures',
         }
     self.workers = []
     self.broker = FakeAMQPBroker()
     self.redis = FakeRedis()
     self.worker = yield self.mk_transport_worker(self.config, self.broker)
     self.fail_worker = yield self.mk_failure_worker(
         self.fail_config, self.broker, self.redis)
Exemplo n.º 5
0
 def test_hgetall_returns_copy(self):
     self.r_server = FakeRedis()
     self.r_server.hset("hash", "foo", "1")
     data = self.r_server.hgetall("hash")
     data["foo"] = "2"
     self.assertEqual(self.r_server.hgetall("hash"), {
         "foo": "1",
     })
Exemplo n.º 6
0
 def test_incr_with_by_param(self):
     self.r_server = FakeRedis()
     self.r_server.set("inc", 1)
     self.assertEqual('1', self.r_server.get("inc"))
     self.assertEqual(2, self.r_server.incr("inc", 1))
     self.assertEqual(4, self.r_server.incr("inc", 2))
     self.assertEqual(7, self.r_server.incr("inc", 3))
     self.assertEqual(11, self.r_server.incr("inc", 4))
     self.assertEqual(111, self.r_server.incr("inc", 100))
     self.assertEqual('111', self.r_server.get("inc"))
Exemplo n.º 7
0
 def test_hincrby(self):
     self.r_server = FakeRedis()
     hincrby = self.r_server.hincrby
     self.assertEqual(hincrby("inc", "field1"), 1)
     self.assertEqual(hincrby("inc", "field1"), 2)
     self.assertEqual(hincrby("inc", "field1", 3), 5)
     self.assertEqual(hincrby("inc", "field1", "2"), 7)
     self.assertRaises(Exception, hincrby, "inc", "field1", "1.5")
     self.r_server.hset("inc", "field2", "a")
     self.assertRaises(Exception, hincrby, "inc", "field2")
     self.r_server.set("key", "string")
     self.assertRaises(Exception, self.r_server.hincrby, "key", "field1")
Exemplo n.º 8
0
    def test_smove(self):
        self.r_server = FakeRedis()
        self.r_server.sadd('set1', 1)
        self.r_server.sadd('set2', 2)
        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), True)
        self.assertEqual(self.r_server.smembers('set1'), set())
        self.assertEqual(self.r_server.smembers('set2'), set(['1', '2']))

        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), False)

        self.assertEqual(self.r_server.smove('set2', 'set3', '1'), True)
        self.assertEqual(self.r_server.smembers('set2'), set(['2']))
        self.assertEqual(self.r_server.smembers('set3'), set(['1']))
Exemplo n.º 9
0
class SessionManagerTestCase(TestCase):
    def setUp(self):
        self.fake_redis = FakeRedis()
        self.sm = SessionManager(self.fake_redis, prefix="test")

    def tearDown(self):
        self.sm.stop()
        self.fake_redis.teardown()

    def test_active_sessions(self):
        def get_sessions():
            return sorted(self.sm.active_sessions())

        def ids():
            return [x[0] for x in get_sessions()]

        self.assertEqual(ids(), [])
        self.sm.create_session("u1")
        self.assertEqual(ids(), ["u1"])
         # 10 seconds later
        self.sm.create_session("u2", created_at=time.time() + 10)
        self.assertEqual(ids(), ["u1", "u2"])

        s1, s2 = get_sessions()
        self.assertTrue(s1[1]['created_at'] < s2[1]['created_at'])

    def test_schedule_session_expiry(self):
        self.sm.max_session_length = 60.0
        self.sm.create_session("u1")

    def test_create_and_retrieve_session(self):
        session = self.sm.create_session("u1")
        self.assertEqual(sorted(session.keys()), ['created_at'])
        self.assertTrue(time.time() - float(session['created_at']) < 10.0)
        loaded = self.sm.load_session("u1")
        self.assertEqual(loaded, session)

    def test_save_session(self):
        test_session = {"foo": 5, "bar": "baz"}
        self.sm.create_session("u1")
        self.sm.save_session("u1", test_session)
        session = self.sm.load_session("u1")
        self.assertTrue(session.pop('created_at') is not None)
        # Redis saves & returns all session values as strings
        self.assertEqual(session, dict([map(str, kvs) for kvs
                                        in test_session.items()]))

    def test_lazy_clearing(self):
        self.sm.save_session('user_id', {})
        self.assertEqual(list(self.sm.active_sessions()), [])
Exemplo n.º 10
0
    def setUp(self):
        yield super(UserGroupingRouterTestCase, self).setUp()
        self.config = {
            'dispatcher_name': 'user_group_dispatcher',
            'router_class': 'vumi.dispatchers.base.UserGroupingRouter',
            'transport_names': [
                self.transport_name,
            ],
            'exposed_names': [
                'app1',
                'app2',
            ],
            'group_mappings': {
                'group1': 'app1',
                'group2': 'app2',
            },
            'transport_mappings': {
                'upstream1': self.transport_name,
            }
        }

        self.fake_redis = FakeRedis()
        self.dispatcher = yield self.get_dispatcher(self.config)
        self.router = self.dispatcher._router
        self.router.r_server = self.fake_redis
        self.router.setup_routing()
Exemplo n.º 11
0
 def test_incr(self):
     self.r_server = FakeRedis()
     self.r_server.set("inc", 1)
     self.assertEqual('1', self.r_server.get("inc"))
     self.assertEqual(2, self.r_server.incr("inc"))
     self.assertEqual(3, self.r_server.incr("inc"))
     self.assertEqual('3', self.r_server.get("inc"))
Exemplo n.º 12
0
 def setUp(self):
     self.today = datetime.utcnow().date()
     self.port = 9999
     self.path = "/api/v1/sms/vas2nets/receive/"
     self.config = {
         "transport_name": "vas2nets",
         "url": "http://localhost:%s%s" % (self.port, self.path),
         "username": "******",
         "password": "******",
         "owner": "owner",
         "service": "service",
         "subservice": "subservice",
         "web_receive_path": "/receive",
         "web_receipt_path": "/receipt",
         "web_port": 9998,
     }
     self.fail_config = {
         "transport_name": "vas2nets",
         "retry_routing_key": "sms.outbound.%(transport_name)s",
         "failures_routing_key": "sms.outbound.%(transport_name)s.failures",
     }
     self.workers = []
     self.broker = FakeAMQPBroker()
     self.redis = FakeRedis()
     self.worker = yield self.mk_transport_worker(self.config, self.broker)
     self.fail_worker = yield self.mk_failure_worker(self.fail_config, self.broker, self.redis)
Exemplo n.º 13
0
 def test_sunion(self):
     self.r_server = FakeRedis()
     self.r_server.sadd('set1', 1)
     self.r_server.sadd('set2', 2)
     self.assertEqual(self.r_server.sunion('set1'), set(['1']))
     self.assertEqual(self.r_server.sunion('set1', 'set2'), set(['1', '2']))
     self.assertEqual(self.r_server.sunion('other'), set())
Exemplo n.º 14
0
 def setUp(self):
     self.today = datetime.utcnow().date()
     self.port = 9999
     self.path = '/api/v1/sms/vas2nets/receive/'
     self.config = {
         'transport_name': 'vas2nets',
         'url': 'http://localhost:%s%s' % (self.port, self.path),
         'username': '******',
         'password': '******',
         'owner': 'owner',
         'service': 'service',
         'subservice': 'subservice',
         'web_receive_path': '/receive',
         'web_receipt_path': '/receipt',
         'web_port': 9998,
     }
     self.fail_config = {
         'transport_name': 'vas2nets',
         'retry_routing_key': '%(transport_name)s.outbound',
         'failures_routing_key': '%(transport_name)s.failures',
         }
     self.workers = []
     self.broker = FakeAMQPBroker()
     self.redis = FakeRedis()
     self.worker = yield self.mk_transport_worker(self.config, self.broker)
     self.fail_worker = yield self.mk_failure_worker(
         self.fail_config, self.broker, self.redis)
Exemplo n.º 15
0
 def setUp(self):
     yield super(TestContentKeywordRouter, self).setUp()
     self.config = {
         'dispatcher_name': 'keyword_dispatcher',
         'router_class': 'vumi.dispatchers.base.ContentKeywordRouter',
         'transport_names': ['transport1', 'transport2'],
         'transport_mappings': {
             'shortcode1': 'transport1',
             'shortcode2': 'transport2',
             },
         'exposed_names': ['app1', 'app2', 'app3', 'fallback_app'],
         'rules': [{'app': 'app1',
                    'keyword': 'KEYWORD1',
                    'to_addr': '8181',
                    'prefix': '+256',
                    },
                   {'app': 'app2',
                    'keyword': 'KEYWORD2',
                    }],
         'keyword_mappings': {
             'app2': 'KEYWORD3',
             'app3': 'KEYWORD1',
             },
         'fallback_application': 'fallback_app',
         'expire_routing_memory': '3',
         }
     self.fake_redis = FakeRedis()
     self.dispatcher = yield self.get_dispatcher(self.config)
     self.router = self.dispatcher._router
     self.router.r_server = self.fake_redis
Exemplo n.º 16
0
    def setUp(self):
        yield super(TwitterTransportTestCase, self).setUp()
        self.config = {
            'app_name': 'testapp',
            'consumer_key': 'consumer1',
            'consumer_secret': 'consumersecret1',
            'access_token': 'token1',
            'access_token_secret': 'tokensecret1',
            'terms': ['some', 'trending', 'topic'],
        }
        self.transport = yield self.get_transport(self.config, start=False)
        self.transport.validate_config()
        self.transport.r_server = FakeRedis()
        self.transport.transport_name = self.transport_name
        self.transport.concurrent_sends = None
        self.transport._consumers = []
        self.transport.twitter = FakeTwitter()
        yield self.transport.start_tracking_terms()
        self.transport.start_checking_for_replies()

        yield self.transport._setup_failure_publisher()
        yield self.transport._setup_message_publisher()
        yield self.transport._setup_event_publisher()

        yield self.transport.setup_middleware()
Exemplo n.º 17
0
    def setUp(self):
        yield super(EsmeToSmscTestCase, self).setUp()
        delivery_report_regex = "id:(?P<id>\S{,65})" \
            " +sub:(?P<sub>.{1,3})" \
            " +dlvrd:(?P<dlvrd>.{1,3})" \
            " +submit date:(?P<submit_date>\d*)" \
            " +done date:(?P<done_date>\d*)" \
            " +stat:(?P<stat>[0-9,A-Z]{1,7})" \
            " +err:(?P<err>.{1,3})" \
            " +[Tt]ext:(?P<text>.{,20}).*" \

        self.config = {
            "system_id": "VumiTestSMSC",
            "password": "******",
            "host": "localhost",
            "port": 0,
            "redis": {},
            "transport_name": self.transport_name,
            "transport_type": "smpp",
            "delivery_report_regex": delivery_report_regex,
            "smsc_delivery_report_string": (
                'id:%s sub:1 dlvrd:1 submit date:%s done date:%s '
                'stat:0 err:0 text:If a general electio'),
        }
        self.service = SmppService(None, config=self.config)
        yield self.service.startWorker()
        self.service.factory.protocol = SmscTestServer
        self.config['port'] = self.service.listening.getHost().port
        self.transport = yield self.get_transport(self.config, start=False)
        self.transport.r_server = FakeRedis()
        self.expected_delivery_status = 'delivered'  # stat:0 means delivered
Exemplo n.º 18
0
 def setUp(self):
     dummy_worker = object()
     config = {}
     self.mw = StoringMiddleware("dummy_storer", config, dummy_worker)
     self.mw.setup_middleware()
     self.mw.store.r_server = FakeRedis()
     self.store = self.mw.store
Exemplo n.º 19
0
 def get_esme(self, **callbacks):
     config = ClientConfig(host="127.0.0.1",
                           port="0",
                           system_id="1234",
                           password="******")
     esme_callbacks = EsmeCallbacks(**callbacks)
     esme = FakeEsmeTransceiver(config, FakeRedis(), esme_callbacks)
     return esme
Exemplo n.º 20
0
 def test_hgetall_returns_copy(self):
     self.r_server = FakeRedis()
     self.r_server.hset("hash", "foo", "1")
     data = self.r_server.hgetall("hash")
     data["foo"] = "2"
     self.assertEqual(self.r_server.hgetall("hash"), {
         "foo": "1",
         })
Exemplo n.º 21
0
 def setUp(self):
     super(TestDecisionTreeWorker, self).setUp()
     self.worker = yield self.get_application({
         'worker_name':
         'test_decision_tree_worker',
     })
     self.worker.r_server = FakeRedis()
     self.worker.session_manager.r_server = self.worker.r_server
Exemplo n.º 22
0
 def test_incrby(self):
     self.r_server = FakeRedis()
     self.r_server.set("inc", 1)
     self.assertEqual('1', self.r_server.get("inc"))
     self.assertEqual('2', self.r_server.incrby("inc", 1))
     self.assertEqual('4', self.r_server.incrby("inc", 2))
     self.assertEqual('7', self.r_server.incrby("inc", 3))
     self.assertEqual('11', self.r_server.incrby("inc", 4))
     self.assertEqual('111', self.r_server.incrby("inc", 100))
Exemplo n.º 23
0
 def test_zrange(self):
     self.r_server = FakeRedis()
     self.r_server.zadd('set', one=0.1, two=0.2, three=0.3)
     self.assertEqual(self.r_server.zrange('set', 0, 0), ['one'])
     self.assertEqual(self.r_server.zrange('set', 0, 1), ['one', 'two'])
     self.assertEqual(self.r_server.zrange('set', 0, 2),
                      ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, 3),
                      ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, -1),
                      ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, -1, withscores=True),
                      [(0.1, 'one'), (0.2, 'two'), (0.3, 'three')])
     self.assertEqual(self.r_server.zrange('set', 0, -1, desc=True),
                      ['three', 'two', 'one'])
     self.assertEqual(
         self.r_server.zrange('set', 0, -1, desc=True, withscores=True),
         [(0.3, 'three'), (0.2, 'two'), (0.1, 'one')])
Exemplo n.º 24
0
 def test_incr_with_by_param(self):
     self.r_server = FakeRedis()
     self.r_server.set("inc", 1)
     self.assertEqual('1', self.r_server.get("inc"))
     self.assertEqual(2, self.r_server.incr("inc", 1))
     self.assertEqual(4, self.r_server.incr("inc", 2))
     self.assertEqual(7, self.r_server.incr("inc", 3))
     self.assertEqual(11, self.r_server.incr("inc", 4))
     self.assertEqual(111, self.r_server.incr("inc", 100))
     self.assertEqual('111', self.r_server.get("inc"))
Exemplo n.º 25
0
 def setUp(self):
     yield super(TestCellulantTransportTestCase, self).setUp()
     self.config = {
         'web_port': 0,
         'web_path': '/api/v1/ussd/cellulant/',
         'ussd_session_timeout': 60,
         'redis': {}
     }
     self.transport = yield self.get_transport(self.config)
     self.transport_url = self.transport.get_transport_url(
         self.config['web_path'])
     self.transport.r_server = FakeRedis()
Exemplo n.º 26
0
 def test_hincrby(self):
     self.r_server = FakeRedis()
     hincrby = self.r_server.hincrby
     self.assertEqual(hincrby("inc", "field1"), 1)
     self.assertEqual(hincrby("inc", "field1"), 2)
     self.assertEqual(hincrby("inc", "field1", 3), 5)
     self.assertEqual(hincrby("inc", "field1", "2"), 7)
     self.assertRaises(Exception, hincrby, "inc", "field1", "1.5")
     self.r_server.hset("inc", "field2", "a")
     self.assertRaises(Exception, hincrby, "inc", "field2")
     self.r_server.set("key", "string")
     self.assertRaises(Exception, self.r_server.hincrby, "key", "field1")
Exemplo n.º 27
0
 def setUp(self):
     super(TestTruteqTransport, self).setUp()
     self.dummy_connect = MockConnectTCPForSSMI()
     self.patch(reactor, 'connectTCP', self.dummy_connect)
     self.config = {
         'username': '******',
         'password': '******',
         'host': 'localhost',
         'port': 1234,
     }
     self.transport = yield self.get_transport(self.config, start=True)
     self.transport.r_server = FakeRedis()
Exemplo n.º 28
0
 def setUp(self):
     config = {
         'transport_name': 'test_infobip',
         'transport_type': 'ussd',
         'web_path': "/session/",
         'web_port': 0,
     }
     self.worker = get_stubbed_worker(InfobipTransport, config)
     self.broker = self.worker._amqp_client.broker
     yield self.worker.startWorker()
     self.worker_url = self.worker.get_transport_url()
     self.worker.r_server = FakeRedis()
Exemplo n.º 29
0
    def test_smove(self):
        self.r_server = FakeRedis()
        self.r_server.sadd('set1', 1)
        self.r_server.sadd('set2', 2)
        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), True)
        self.assertEqual(self.r_server.smembers('set1'), set())
        self.assertEqual(self.r_server.smembers('set2'), set(['1', '2']))

        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), False)

        self.assertEqual(self.r_server.smove('set2', 'set3', '1'), True)
        self.assertEqual(self.r_server.smembers('set2'), set(['2']))
        self.assertEqual(self.r_server.smembers('set3'), set(['1']))
Exemplo n.º 30
0
 def test_sequence_rollover(self):
     config = ClientConfig(host="127.0.0.1",
                           port="0",
                           system_id="1234",
                           password="******")
     esme = FakeEsmeTransceiver(config, FakeRedis(), None)
     self.assertEqual(1, esme.get_seq())
     esme.get_next_seq()
     self.assertEqual(2, esme.get_seq())
     esme.set_seq(4004004004)
     self.assertEqual(4004004004, esme.get_seq())
     esme.get_next_seq()
     self.assertEqual(1, esme.get_seq())
Exemplo n.º 31
0
 def make_worker(self, retry_delivery_period=0):
     self.config = {
         'transport_name': 'sphex',
         'retry_routing_key': 'sms.outbound.%(transport_name)s',
         'failures_routing_key': 'sms.failures.%(transport_name)s',
         'retry_delivery_period': retry_delivery_period,
     }
     self.worker = get_stubbed_worker(FailureWorker, self.config)
     self.worker.r_server = FakeRedis()
     self.worker.r_server.flushdb()
     self.redis = self.worker.r_server
     yield self.worker.startWorker()
     self.broker = self.worker._amqp_client.broker
     self.retry_timestamps_key = self.worker._retry_timestamps_key
Exemplo n.º 32
0
 def mk_transport(self, cls=OperaTransport, **config):
     default_config = {
         'url': 'http://testing.domain',
         'channel': 'channel',
         'service': 'service',
         'password': '******',
         'web_receipt_path': '/receipt.xml',
         'web_receive_path': '/receive.xml',
         'web_port': self.port
     }
     default_config.update(config)
     self.r_server = FakeRedis()
     worker = yield self.get_transport(default_config, cls)
     worker.r_server = self.r_server
     returnValue(worker)
Exemplo n.º 33
0
 def test_zrange(self):
     self.r_server = FakeRedis()
     self.r_server.zadd('set', one=0.1, two=0.2, three=0.3)
     self.assertEqual(self.r_server.zrange('set', 0, 0), ['one'])
     self.assertEqual(self.r_server.zrange('set', 0, 1), ['one', 'two'])
     self.assertEqual(self.r_server.zrange('set', 0, 2),
                                             ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, 3),
                                             ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, -1),
                                             ['one', 'two', 'three'])
     self.assertEqual(self.r_server.zrange('set', 0, -1, withscores=True),
                     [(0.1, 'one'), (0.2, 'two'), (0.3, 'three')])
     self.assertEqual(self.r_server.zrange('set', 0, -1, desc=True),
                     ['three', 'two', 'one'])
     self.assertEqual(self.r_server.zrange('set', 0, -1, desc=True,
         withscores=True), [(0.3, 'three'), (0.2, 'two'), (0.1, 'one')])
Exemplo n.º 34
0
 def setUp(self):
     yield super(EsmeToSmscTestCase, self).setUp()
     self.config = {
         "system_id": "VumiTestSMSC",
         "password": "******",
         "host": "localhost",
         "port": 0,
         "redis": {},
         "transport_name": self.transport_name,
         "transport_type": "smpp",
     }
     self.service = SmppService(None, config=self.config)
     yield self.service.startWorker()
     self.service.factory.protocol = SmscTestServer
     self.config['port'] = self.service.listening.getHost().port
     self.transport = yield self.get_transport(self.config, start=False)
     self.transport.r_server = FakeRedis()
     self.expected_delivery_status = 'delivered'
Exemplo n.º 35
0
    def setUp(self):
        super(TestHangmanWorker, self).setUp()
        root = Resource()
        # data is elephant with a UTF-8 encoded BOM
        # it is a sad elephant (as seen in the wild)
        root.putChild("word", Data('\xef\xbb\xbfelephant\r\n', 'text/html'))
        site_factory = Site(root)
        self.webserver = yield reactor.listenTCP(0, site_factory)
        addr = self.webserver.getHost()
        random_word_url = "http://%s:%s/word" % (addr.host, addr.port)

        self.worker = yield self.get_application({
            'worker_name':
            'test_hangman',
            'random_word_url':
            random_word_url,
        })
        self.worker.r_server = FakeRedis()
Exemplo n.º 36
0
    def setUp(self):
        super(FakeRedisRespTestCase, self).setUp()
        self.config = {
                "TRANSPORT_NAME": "redis_testing_transport",
                "system_id": "vumitest-vumitest-vumitest",
                "host": "host",
                "port": "port",
                "password": "******",
                }
        self.vumi_options = {
                "vhost": "develop",
                }
        self.clientConfig = ClientConfig(
                system_id=self.config['system_id'],
                host=self.config['host'],
                port=self.config['port'],
                password=self.config['password'],
                )

        # hack a lot of transport setup
        self.transport = yield self.get_transport(self.config, start=False)
        self.transport.r_server = FakeRedis()
        self.esme_callbacks = EsmeCallbacks(
            connect=lambda: None, disconnect=lambda: None,
            submit_sm_resp=self.transport.submit_sm_resp,
            delivery_report=lambda: None, deliver_sm=lambda: None)

        self.esme = RedisTestEsmeTransceiver(
                self.clientConfig,
                self.transport.r_server,
                self.esme_callbacks)
        self.esme.state = 'BOUND_TRX'
        self.transport.esme_client = self.esme

        yield self.transport.startWorker()
        self.transport.esme_connected(self.esme)
Exemplo n.º 37
0
class Vas2NetsFailureWorkerTestCase(unittest.TestCase):
    @inlineCallbacks
    def setUp(self):
        self.today = datetime.utcnow().date()
        self.port = 9999
        self.path = "/api/v1/sms/vas2nets/receive/"
        self.config = {
            "transport_name": "vas2nets",
            "url": "http://localhost:%s%s" % (self.port, self.path),
            "username": "******",
            "password": "******",
            "owner": "owner",
            "service": "service",
            "subservice": "subservice",
            "web_receive_path": "/receive",
            "web_receipt_path": "/receipt",
            "web_port": 9998,
        }
        self.fail_config = {
            "transport_name": "vas2nets",
            "retry_routing_key": "sms.outbound.%(transport_name)s",
            "failures_routing_key": "sms.outbound.%(transport_name)s.failures",
        }
        self.workers = []
        self.broker = FakeAMQPBroker()
        self.redis = FakeRedis()
        self.worker = yield self.mk_transport_worker(self.config, self.broker)
        self.fail_worker = yield self.mk_failure_worker(self.fail_config, self.broker, self.redis)

    def tearDown(self):
        for worker in self.workers:
            worker.stopWorker()

    @inlineCallbacks
    def mk_transport_worker(self, config, broker):
        worker = get_stubbed_worker(Vas2NetsTransport, config, broker)
        self.workers.append(worker)
        yield worker.startWorker()
        returnValue(worker)

    @inlineCallbacks
    def mk_failure_worker(self, config, broker, redis):
        w = get_stubbed_worker(Vas2NetsFailureWorker, config, broker)
        self.workers.append(w)
        yield w.startWorker()
        w.retry_publisher = yield self.worker.publish_to("foo")
        w.r_server = redis
        returnValue(w)

    @inlineCallbacks
    def mk_resource_worker(self, body, headers=None, code=http.OK):
        w = get_stubbed_worker(TestResourceWorker, {}, self.broker)
        self.workers.append(w)
        w.set_resources([(self.path, BadVas2NetsResource, (body, headers, code))])
        yield w.startWorker()
        returnValue(w)

    def get_dispatched(self, rkey):
        return self.broker.get_dispatched("vumi", rkey)

    def get_retry_keys(self):
        timestamps = self.redis.zrange(self.fail_worker._retry_timestamps_key, 0, 0)
        retry_keys = set()
        for timestamp in timestamps:
            bucket_key = self.fail_worker.r_key("retry_keys." + timestamp)
            retry_keys.update(self.redis.smembers(bucket_key))
        return retry_keys

    def mkmsg_out(self, in_reply_to=None):
        msg = TransportSMS(
            transport="vas2nets",
            to_addr="+27761234567",
            from_addr="9292",
            message_id="1",
            in_reply_to=in_reply_to,
            transport_metadata={"network_id": "network-id"},
            message="hello world",
        )
        return msg

    def assert_dispatched_count(self, count, routing_key):
        self.assertEqual(count, len(self.get_dispatched(routing_key)))

    @inlineCallbacks
    def test_send_sms_success(self):
        yield self.mk_resource_worker("Result_code: 00, Message OK")
        yield self.worker.handle_outbound_message(self.mkmsg_out())
        self.assert_dispatched_count(1, "sms.ack.vas2nets")
        self.assert_dispatched_count(0, "sms.failures.vas2nets")

    @inlineCallbacks
    def test_send_sms_fail(self):
        """
        A 'No SmsId Header' error should not be retried.
        """
        self.worker.failure_published = FailureCounter(1)
        self.worker.SUPPRESS_EXCEPTIONS = True
        yield self.mk_resource_worker("Result_code: 04, Internal system error " "occurred while processing message", {})
        yield self.worker.handle_outbound_message(self.mkmsg_out())
        yield self.worker.failure_published.deferred
        self.assert_dispatched_count(0, "sms.ack.vas2nets")
        self.assert_dispatched_count(1, "sms.outbound.vas2nets.failures")
        [fmsg] = self.get_dispatched("sms.outbound.vas2nets.failures")
        fmsg = from_json(fmsg.body)
        self.assertTrue("Vas2NetsTransportError: No SmsId Header" in fmsg["reason"])

        yield self.broker.kick_delivery()
        [key] = self.fail_worker.get_failure_keys()
        self.assertEqual(set(), self.get_retry_keys())

    @inlineCallbacks
    def test_send_sms_noconn(self):
        """
        A 'connection refused' error should be retried.
        """
        self.worker.failure_published = FailureCounter(1)
        self.worker.SUPPRESS_EXCEPTIONS = True
        msg = self.mkmsg_out()
        yield self.worker.handle_outbound_message(msg)
        yield self.worker.failure_published.deferred
        self.assert_dispatched_count(0, "sms.ack.vas2nets")
        self.assert_dispatched_count(1, "sms.outbound.vas2nets.failures")
        [fmsg] = self.get_dispatched("sms.outbound.vas2nets.failures")
        fmsg = from_json(fmsg.body)
        self.assertEqual(msg, fmsg["message"])
        self.assertEqual("connection refused", fmsg["reason"])

        yield self.broker.kick_delivery()
        [key] = self.fail_worker.get_failure_keys()
        self.assertEqual(set([key]), self.get_retry_keys())
Exemplo n.º 38
0
 def setUp(self):
     self.r_server = FakeRedis()
     self.store = MessageStore(self.r_server, 'teststore')
Exemplo n.º 39
0
class TestMessageStore(ApplicationTestCase):
    # inherits from ApplicationTestCase for .mkmsg_in and .mkmsg_out

    def setUp(self):
        self.r_server = FakeRedis()
        self.store = MessageStore(self.r_server, 'teststore')

    def tearDown(self):
        self.r_server.teardown()

    def test_batch_start(self):
        tag1 = ("poolA", "tag1")
        batch_id = self.store.batch_start([tag1])
        self.assertEqual(self.store.batch_messages(batch_id), [])
        self.assertEqual(self.store.batch_status(batch_id), {
            'ack': 0, 'delivery_report': 0, 'message': 0, 'sent': 0,
            })
        self.assertEqual(self.store.batch_common(batch_id),
                         {"tags": [tag1]})
        self.assertEqual(self.store.tag_common(tag1),
                         {"current_batch_id": batch_id})

    def test_batch_done(self):
        tag1 = ("poolA", "tag1")
        batch_id = self.store.batch_start([tag1])
        self.store.batch_done(batch_id)
        self.assertEqual(self.store.batch_common(batch_id),
                         {"tags": [tag1]})
        self.assertEqual(self.store.tag_common(tag1),
                         {"current_batch_id": None})

    def test_add_outbound_message(self):
        msg = self.mkmsg_out(content="outfoo")
        msg_id = msg['message_id']
        self.store.add_outbound_message(msg)

        self.assertEqual(self.store.get_outbound_message(msg_id), msg)
        self.assertEqual(self.store.message_events(msg_id), [])

    def test_add_outbound_message_with_batch_id(self):
        batch_id = self.store.batch_start([("pool", "tag")])
        msg = self.mkmsg_out(content="outfoo")
        msg_id = msg['message_id']
        self.store.add_outbound_message(msg, batch_id=batch_id)

        self.assertEqual(self.store.get_outbound_message(msg_id), msg)
        self.assertEqual(self.store.message_batches(msg_id), [batch_id])
        self.assertEqual(self.store.batch_messages(batch_id), [msg_id])
        self.assertEqual(self.store.message_events(msg_id), [])
        self.assertEqual(self.store.batch_status(batch_id), {
            'ack': 0, 'delivery_report': 0, 'message': 1, 'sent': 1,
            })

    def test_add_outbound_message_with_tag(self):
        batch_id = self.store.batch_start([("pool", "tag")])
        msg = self.mkmsg_out(content="outfoo")
        msg_id = msg['message_id']
        self.store.add_outbound_message(msg, tag=("pool", "tag"))

        self.assertEqual(self.store.get_outbound_message(msg_id), msg)
        self.assertEqual(self.store.message_batches(msg_id), [batch_id])
        self.assertEqual(self.store.batch_messages(batch_id), [msg_id])
        self.assertEqual(self.store.message_events(msg_id), [])
        self.assertEqual(self.store.batch_status(batch_id), {
            'ack': 0, 'delivery_report': 0, 'message': 1, 'sent': 1,
            })

    def test_add_ack_event(self):
        batch_id = self.store.batch_start([("pool", "tag")])
        msg = self.mkmsg_out(content="outfoo")
        msg_id = msg['message_id']
        ack = TransportEvent(user_message_id=msg_id, event_type='ack',
                             sent_message_id='xyz')
        ack_id = ack['event_id']
        self.store.add_outbound_message(msg, batch_id=batch_id)
        self.store.add_event(ack)

        self.assertEqual(self.store.get_event(ack_id), ack)
        self.assertEqual(self.store.message_events(msg_id), [ack_id])

    def test_add_inbound_message(self):
        msg = self.mkmsg_in(content="infoo")
        msg_id = msg['message_id']
        self.store.add_inbound_message(msg)

        self.assertEqual(self.store.get_inbound_message(msg_id), msg)

    def test_add_inbound_message_with_batch_id(self):
        batch_id = self.store.batch_start([("pool1", "default10001")])
        msg = self.mkmsg_in(content="infoo", to_addr="+1234567810001",
                            transport_type="sms")
        msg_id = msg['message_id']
        self.store.add_inbound_message(msg, batch_id=batch_id)

        self.assertEqual(self.store.get_inbound_message(msg_id), msg)
        self.assertEqual(self.store.batch_replies(batch_id), [msg_id])

    def test_add_inbound_message_with_tag(self):
        batch_id = self.store.batch_start([("pool1", "default10001")])
        msg = self.mkmsg_in(content="infoo", to_addr="+1234567810001",
                            transport_type="sms")
        msg_id = msg['message_id']
        self.store.add_inbound_message(msg, tag=("pool1", "default10001"))

        self.assertEqual(self.store.get_inbound_message(msg_id), msg)
        self.assertEqual(self.store.batch_replies(batch_id), [msg_id])
Exemplo n.º 40
0
 def setUp(self):
     self.fake_redis = FakeRedis()
     self.sm = SessionManager(self.fake_redis, prefix="test")
Exemplo n.º 41
0
 def test_delete(self):
     self.r_server = FakeRedis()
     self.r_server.set("delete_me", 1)
     self.assertEqual(True, self.r_server.delete("delete_me"))
     self.assertEqual(False, self.r_server.delete("delete_me"))
Exemplo n.º 42
0
 def setUp(self):
     super(TestMemoWorker, self).setUp()
     self.worker = yield self.get_application({
         'worker_name': 'testmemo',
         })
     self.worker.r_server = FakeRedis()
Exemplo n.º 43
0
class Vas2NetsFailureWorkerTestCase(unittest.TestCase):

    @inlineCallbacks
    def setUp(self):
        self.today = datetime.utcnow().date()
        self.port = 9999
        self.path = '/api/v1/sms/vas2nets/receive/'
        self.config = {
            'transport_name': 'vas2nets',
            'url': 'http://localhost:%s%s' % (self.port, self.path),
            'username': '******',
            'password': '******',
            'owner': 'owner',
            'service': 'service',
            'subservice': 'subservice',
            'web_receive_path': '/receive',
            'web_receipt_path': '/receipt',
            'web_port': 9998,
        }
        self.fail_config = {
            'transport_name': 'vas2nets',
            'retry_routing_key': '%(transport_name)s.outbound',
            'failures_routing_key': '%(transport_name)s.failures',
            }
        self.workers = []
        self.broker = FakeAMQPBroker()
        self.redis = FakeRedis()
        self.worker = yield self.mk_transport_worker(self.config, self.broker)
        self.fail_worker = yield self.mk_failure_worker(
            self.fail_config, self.broker, self.redis)

    def tearDown(self):
        for worker in self.workers:
            worker.stopWorker()

    @inlineCallbacks
    def mk_transport_worker(self, config, broker):
        worker = get_stubbed_worker(Vas2NetsTransport, config, broker)
        self.workers.append(worker)
        yield worker.startWorker()
        returnValue(worker)

    @inlineCallbacks
    def mk_failure_worker(self, config, broker, redis):
        w = get_stubbed_worker(FailureWorker, config, broker)
        self.workers.append(w)
        yield w.startWorker()
        w.retry_publisher = yield self.worker.publish_to("foo")
        w.r_server = redis
        returnValue(w)

    @inlineCallbacks
    def mk_resource_worker(self, body, headers=None, code=http.OK):
        w = get_stubbed_worker(TestResourceWorker, {}, self.broker)
        self.workers.append(w)
        w.set_resources([(self.path, BadVas2NetsResource,
                          (body, headers, code))])
        yield w.startWorker()
        returnValue(w)

    def get_dispatched(self, rkey):
        return self.broker.get_dispatched('vumi', rkey)

    def get_retry_keys(self):
        timestamps = self.redis.zrange(
            self.fail_worker._retry_timestamps_key, 0, 0)
        retry_keys = set()
        for timestamp in timestamps:
            bucket_key = self.fail_worker.r_key("retry_keys." + timestamp)
            retry_keys.update(self.redis.smembers(bucket_key))
        return retry_keys

    def mkmsg_out(self, in_reply_to=None):
        return TransportUserMessage(
            to_addr='+41791234567',
            from_addr='9292',
            message_id='1',
            transport_name='vas2nets',
            transport_type='sms',
            transport_metadata={
               'network_id': 'network-id',
               },
            content='hello world',
            in_reply_to=in_reply_to,
            )

    def assert_dispatched_count(self, count, routing_key):
        self.assertEqual(count, len(self.get_dispatched(routing_key)))

    @inlineCallbacks
    def test_send_sms_success(self):
        yield self.mk_resource_worker("Result_code: 00, Message OK")
        yield self.worker._process_message(self.mkmsg_out())
        self.assert_dispatched_count(1, 'vas2nets.event')
        self.assert_dispatched_count(0, 'vas2nets.failures')

    @inlineCallbacks
    def test_send_sms_fail(self):
        """
        A 'No SmsId Header' error should not be retried.
        """
        self.worker.failure_published = FailureCounter(1)
        yield self.mk_resource_worker("Result_code: 04, Internal system error "
                                      "occurred while processing message",
                                      {})
        yield self.worker._process_message(self.mkmsg_out())
        yield self.worker.failure_published.deferred
        yield self.broker.kick_delivery()
        self.assert_dispatched_count(0, 'vas2nets.event')
        self.assert_dispatched_count(1, 'vas2nets.failures')
        [fmsg] = self.get_dispatched('vas2nets.failures')
        fmsg = from_json(fmsg.body)
        self.assertTrue(
            "Vas2NetsTransportError: No SmsId Header" in fmsg['reason'])

        yield self.broker.kick_delivery()
        [key] = self.fail_worker.get_failure_keys()
        self.assertEqual(set(), self.get_retry_keys())

    @inlineCallbacks
    def test_send_sms_noconn(self):
        """
        A 'connection refused' error should be retried.
        """
        self.worker.failure_published = FailureCounter(1)
        msg = self.mkmsg_out()
        yield self.worker._process_message(msg)
        yield self.worker.failure_published.deferred
        self.assert_dispatched_count(0, 'vas2nets.event')
        self.assert_dispatched_count(1, 'vas2nets.failures')
        [fmsg] = self.get_dispatched('vas2nets.failures')
        fmsg = from_json(fmsg.body)
        self.assertEqual(msg.payload, fmsg['message'])
        self.assertEqual(FailureMessage.FC_TEMPORARY,
                         fmsg['failure_code'])
        self.assertTrue(fmsg['reason'].strip().endswith("connection refused"))

        yield self.broker.kick_delivery()
        [key] = self.fail_worker.get_failure_keys()
        self.assertEqual(set([key]), self.get_retry_keys())
Exemplo n.º 44
0
 def test_sadd(self):
     self.r_server = FakeRedis()
     self.r_server.sadd('set', 1)
     self.r_server.sadd('set', 2, 3, 4)
     self.assertEqual(self.r_server.smembers('set'), set([
         '1', '2', '3', '4']))
Exemplo n.º 45
0
 def test_sadd(self):
     self.r_server = FakeRedis()
     self.r_server.sadd('set', 1)
     self.r_server.sadd('set', 2, 3, 4)
     self.assertEqual(self.r_server.smembers('set'),
                      set(['1', '2', '3', '4']))
Exemplo n.º 46
0
class SchedulerTestCase(TestCase):

    def setUp(self):
        self.r_server = FakeRedis()
        self.scheduler = Scheduler(self.r_server, self._scheduler_callback)
        self._delivery_history = []

    def tearDown(self):
        if self.scheduler.is_running:
            self.scheduler.stop()

    def _scheduler_callback(self, scheduled_at, message):
        self._delivery_history.append((scheduled_at, message))
        return (scheduled_at, message)

    def assertDelivered(self, message):
        delivered_messages = [TransportUserMessage(**payload)
                                for _, payload in self._delivery_history]
        self.assertIn(message['message_id'],
            [message['message_id'] for message in delivered_messages])

    def assertNumDelivered(self, number):
        self.assertEqual(number, len(self._delivery_history))

    def get_pending_messages(self):
        scheduled_timestamps = self.scheduler.r_key('scheduled_timestamps')
        return self.r_server.zrange(scheduled_timestamps, 0, -1)

    def mkmsg_in(self, content='hello world', message_id='abc',
                 to_addr='9292', from_addr='+41791234567',
                 session_event=None, transport_type='sms',
                 transport_name='test_transport',
                 helper_metadata={}, transport_metadata={}):
        return TransportUserMessage(
            from_addr=from_addr,
            to_addr=to_addr,
            message_id=message_id,
            transport_name=transport_name,
            transport_type=transport_type,
            transport_metadata=transport_metadata,
            helper_metadata=helper_metadata,
            content=content,
            session_event=session_event,
            timestamp=datetime.now(),
            )

    def test_scheduling(self):
        msg = self.mkmsg_in()
        now = time.mktime(datetime(2012, 1, 1).timetuple())
        delta = 10  # seconds from now
        key, bucket_key = self.scheduler.schedule(delta, msg.payload, now)
        self.assertEqual(bucket_key, '%s#%s.%s' % (
            self.scheduler.r_prefix,
            'scheduled_keys',
            self.scheduler.get_next_write_timestamp(delta, now)
        ))
        scheduled_key = self.scheduler.get_scheduled_key(now)
        self.assertEqual(scheduled_key, None)
        scheduled_time = now + delta
        scheduled_key = self.scheduler.get_scheduled_key(scheduled_time)
        self.assertTrue(scheduled_key)
        self.assertEqual(set([scheduled_key]),
                        self.scheduler.get_all_scheduled_keys())

    @inlineCallbacks
    def test_delivery_loop(self):
        msg = self.mkmsg_in()
        now = time.mktime(datetime(2012, 1, 1).timetuple())
        delta = 16  # seconds from now
        self.scheduler.schedule(delta, msg.payload, now)
        scheduled_time = now + delta + self.scheduler.granularity
        yield self.scheduler.deliver_scheduled(scheduled_time)
        self.assertDelivered(msg)

    @inlineCallbacks
    def test_deliver_loop_future(self):
        now = time.mktime(datetime(2012, 1, 1).timetuple())
        for i in range(0, 3):
            msg = self.mkmsg_in(message_id='message_%s' % (i,))
            delta = i * 10
            key, _ = self.scheduler.schedule(delta, msg.payload, now)
            scheduled_time = now + delta + self.scheduler.granularity
            self.assertEqual(set([key]),
                self.scheduler.get_all_scheduled_keys())
            yield self.scheduler.deliver_scheduled(scheduled_time)
            self.assertNumDelivered(i + 1)
            self.assertEqual(set(), self.scheduler.get_all_scheduled_keys())

    @inlineCallbacks
    def test_deliver_ancient_messages(self):
        # something stuck in the queue since 1912 or scheduler hasn't
        # been running since 1912
        msg = self.mkmsg_in()
        way_back = time.mktime(datetime(1912, 1, 1).timetuple())
        scheduled_key, _ = self.scheduler.schedule(0, msg.payload, way_back)
        self.assertTrue(scheduled_key)
        now = time.mktime(datetime.now().timetuple())
        yield self.scheduler.deliver_scheduled(now)
        self.assertEqual(set([scheduled_key]),
            self.scheduler.get_all_scheduled_keys())
        self.assertEqual(len(self.get_pending_messages()), 1)
        yield self.scheduler.deliver_scheduled(
            way_back + self.scheduler.granularity)
        self.assertDelivered(msg)
        self.assertEqual(self.get_pending_messages(), [])
        self.assertEqual(set(), self.scheduler.get_all_scheduled_keys())

    @inlineCallbacks
    def test_clear_scheduled_messages(self):
        msg = self.mkmsg_in()
        now = time.mktime(datetime.now().timetuple())
        scheduled_time = now + self.scheduler.granularity
        key, bucket = self.scheduler.schedule(0, msg.payload, scheduled_time)
        self.assertEqual(len(self.get_pending_messages()), 1)
        self.assertEqual(set([key]),
            self.scheduler.get_all_scheduled_keys())
        self.scheduler.clear_scheduled(key)
        yield self.scheduler.deliver_scheduled()
        self.assertEqual(self.r_server.hgetall(key), {})
        self.assertEqual(self.r_server.smembers(bucket), set())
        self.assertNumDelivered(0)
Exemplo n.º 47
0
 def setUp(self):
     self.r_server = FakeRedis()
     self.scheduler = Scheduler(self.r_server, self._scheduler_callback)
     self._delivery_history = []
Exemplo n.º 48
0
class FakeRedisTestCase(TestCase):

    def test_delete(self):
        self.r_server = FakeRedis()
        self.r_server.set("delete_me", 1)
        self.assertEqual(True, self.r_server.delete("delete_me"))
        self.assertEqual(False, self.r_server.delete("delete_me"))

    def test_incr(self):
        self.r_server = FakeRedis()
        self.r_server.set("inc", 1)
        self.assertEqual('1', self.r_server.get("inc"))
        self.assertEqual(2, self.r_server.incr("inc"))
        self.assertEqual(3, self.r_server.incr("inc"))
        self.assertEqual('3', self.r_server.get("inc"))

    def test_incr_with_by_param(self):
        self.r_server = FakeRedis()
        self.r_server.set("inc", 1)
        self.assertEqual('1', self.r_server.get("inc"))
        self.assertEqual(2, self.r_server.incr("inc", 1))
        self.assertEqual(4, self.r_server.incr("inc", 2))
        self.assertEqual(7, self.r_server.incr("inc", 3))
        self.assertEqual(11, self.r_server.incr("inc", 4))
        self.assertEqual(111, self.r_server.incr("inc", 100))
        self.assertEqual('111', self.r_server.get("inc"))

    def test_zrange(self):
        self.r_server = FakeRedis()
        self.r_server.zadd('set', one=0.1, two=0.2, three=0.3)
        self.assertEqual(self.r_server.zrange('set', 0, 0), ['one'])
        self.assertEqual(self.r_server.zrange('set', 0, 1), ['one', 'two'])
        self.assertEqual(self.r_server.zrange('set', 0, 2),
                                                ['one', 'two', 'three'])
        self.assertEqual(self.r_server.zrange('set', 0, 3),
                                                ['one', 'two', 'three'])
        self.assertEqual(self.r_server.zrange('set', 0, -1),
                                                ['one', 'two', 'three'])
        self.assertEqual(self.r_server.zrange('set', 0, -1, withscores=True),
                        [(0.1, 'one'), (0.2, 'two'), (0.3, 'three')])
        self.assertEqual(self.r_server.zrange('set', 0, -1, desc=True),
                        ['three', 'two', 'one'])
        self.assertEqual(self.r_server.zrange('set', 0, -1, desc=True,
            withscores=True), [(0.3, 'three'), (0.2, 'two'), (0.1, 'one')])

    def test_hgetall_returns_copy(self):
        self.r_server = FakeRedis()
        self.r_server.hset("hash", "foo", "1")
        data = self.r_server.hgetall("hash")
        data["foo"] = "2"
        self.assertEqual(self.r_server.hgetall("hash"), {
            "foo": "1",
            })

    def test_hincrby(self):
        self.r_server = FakeRedis()
        hincrby = self.r_server.hincrby
        self.assertEqual(hincrby("inc", "field1"), 1)
        self.assertEqual(hincrby("inc", "field1"), 2)
        self.assertEqual(hincrby("inc", "field1", 3), 5)
        self.assertEqual(hincrby("inc", "field1", "2"), 7)
        self.assertRaises(Exception, hincrby, "inc", "field1", "1.5")
        self.r_server.hset("inc", "field2", "a")
        self.assertRaises(Exception, hincrby, "inc", "field2")
        self.r_server.set("key", "string")
        self.assertRaises(Exception, self.r_server.hincrby, "key", "field1")

    def test_sadd(self):
        self.r_server = FakeRedis()
        self.r_server.sadd('set', 1)
        self.r_server.sadd('set', 2, 3, 4)
        self.assertEqual(self.r_server.smembers('set'), set([
            '1', '2', '3', '4']))

    def test_smove(self):
        self.r_server = FakeRedis()
        self.r_server.sadd('set1', 1)
        self.r_server.sadd('set2', 2)
        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), True)
        self.assertEqual(self.r_server.smembers('set1'), set())
        self.assertEqual(self.r_server.smembers('set2'), set(['1', '2']))

        self.assertEqual(self.r_server.smove('set1', 'set2', '1'), False)

        self.assertEqual(self.r_server.smove('set2', 'set3', '1'), True)
        self.assertEqual(self.r_server.smembers('set2'), set(['2']))
        self.assertEqual(self.r_server.smembers('set3'), set(['1']))

    def test_sunion(self):
        self.r_server = FakeRedis()
        self.r_server.sadd('set1', 1)
        self.r_server.sadd('set2', 2)
        self.assertEqual(self.r_server.sunion('set1'), set(['1']))
        self.assertEqual(self.r_server.sunion('set1', 'set2'), set(['1', '2']))
        self.assertEqual(self.r_server.sunion('other'), set())
Exemplo n.º 49
0
class TestContentKeywordRouter(DispatcherTestCase):

    dispatcher_class = BaseDispatchWorker
    transport_name = 'test_transport'

    @inlineCallbacks
    def setUp(self):
        yield super(TestContentKeywordRouter, self).setUp()
        self.config = {
            'dispatcher_name': 'keyword_dispatcher',
            'router_class': 'vumi.dispatchers.base.ContentKeywordRouter',
            'transport_names': ['transport1', 'transport2'],
            'transport_mappings': {
                'shortcode1': 'transport1',
                'shortcode2': 'transport2',
                },
            'exposed_names': ['app1', 'app2', 'app3', 'fallback_app'],
            'rules': [{'app': 'app1',
                       'keyword': 'KEYWORD1',
                       'to_addr': '8181',
                       'prefix': '+256',
                       },
                      {'app': 'app2',
                       'keyword': 'KEYWORD2',
                       }],
            'keyword_mappings': {
                'app2': 'KEYWORD3',
                'app3': 'KEYWORD1',
                },
            'fallback_application': 'fallback_app',
            'expire_routing_memory': '3',
            }
        self.fake_redis = FakeRedis()
        self.dispatcher = yield self.get_dispatcher(self.config)
        self.router = self.dispatcher._router
        self.router.r_server = self.fake_redis

    def tearDown(self):
        self.fake_redis.teardown()
        super(TestContentKeywordRouter, self).tearDown()

    @inlineCallbacks
    def test_inbound_message_routing(self):
        msg = self.mkmsg_in(content='KEYWORD1 rest of a msg',
                            to_addr='8181',
                            from_addr='+256788601462')

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='inbound')

        msg2 = self.mkmsg_in(content='KEYWORD2 rest of a msg',
                            to_addr='8181',
                            from_addr='+256788601462')

        yield self.dispatch(msg2,
                            transport_name='transport1',
                            direction='inbound')

        msg3 = self.mkmsg_in(content='KEYWORD3 rest of a msg',
                            to_addr='8181',
                            from_addr='+256788601462')

        yield self.dispatch(msg3,
                            transport_name='transport1',
                            direction='inbound')

        app1_inbound_msg = self.get_dispatched_messages('app1',
                                                        direction='inbound')
        self.assertEqual(app1_inbound_msg, [msg])
        app2_inbound_msg = self.get_dispatched_messages('app2',
                                                        direction='inbound')
        self.assertEqual(app2_inbound_msg, [msg2, msg3])
        app3_inbound_msg = self.get_dispatched_messages('app3',
                                                        direction='inbound')
        self.assertEqual(app3_inbound_msg, [msg])

    @inlineCallbacks
    def test_inbound_message_routing_empty_message_content(self):
        msg = self.mkmsg_in(content=None)

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='inbound')

        app1_inbound_msg = self.get_dispatched_messages('app1',
                                                        direction='inbound')
        self.assertEqual(app1_inbound_msg, [])
        app2_inbound_msg = self.get_dispatched_messages('app2',
                                                        direction='inbound')
        self.assertEqual(app2_inbound_msg, [])
        fallback_msgs = self.get_dispatched_messages('fallback_app',
                                                     direction='inbound')
        self.assertEqual(fallback_msgs, [msg])

    @inlineCallbacks
    def test_inbound_message_routing_not_casesensitive(self):
        msg = self.mkmsg_in(content='keyword1 rest of a msg',
                            to_addr='8181',
                            from_addr='+256788601462')

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='inbound')

        app1_inbound_msg = self.get_dispatched_messages('app1',
                                                        direction='inbound')
        self.assertEqual(app1_inbound_msg, [msg])

    @inlineCallbacks
    def test_inbound_event_routing_ok(self):
        msg = self.mkmsg_ack(user_message_id='1',
                             transport_name='transport1')
        self.router.r_server.set('keyword_dispatcher:message:1',
                                 'app2')

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='event')

        app2_event_msg = self.get_dispatched_messages('app2',
                                                      direction='event')
        self.assertEqual(app2_event_msg, [msg])
        app1_event_msg = self.get_dispatched_messages('app1',
                                                      direction='event')
        self.assertEqual(app1_event_msg, [])

    @inlineCallbacks
    def test_inbound_event_routing_failing_publisher_not_defined(self):
        msg = self.mkmsg_ack(transport_name='transport1')

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='event')

        app1_routed_msg = self.get_dispatched_messages('app1',
                                                       direction='event')
        self.assertEqual(app1_routed_msg, [])
        app2_routed_msg = self.get_dispatched_messages('app2',
                                                       direction='event')
        self.assertEqual(app2_routed_msg, [])

    @inlineCallbacks
    def test_inbound_event_routing_failing_no_routing_back_in_redis(self):
        msg = self.mkmsg_ack(transport_name='transport1')

        yield self.dispatch(msg,
                            transport_name='transport1',
                            direction='event')

        app1_routed_msg = self.get_dispatched_messages('app1',
                                                       direction='event')
        self.assertEqual(app1_routed_msg, [])
        app2_routed_msg = self.get_dispatched_messages('app2',
                                                       direction='event')
        self.assertEqual(app2_routed_msg, [])

    @inlineCallbacks
    def test_outbound_message_routing(self):
        msg = self.mkmsg_out(content="KEYWORD1 rest of msg",
                             from_addr='shortcode1',
                             transport_name='app2')

        yield self.dispatch(msg,
                            transport_name='app2',
                            direction='outbound')

        transport1_msgs = self.get_dispatched_messages('transport1',
                                                       direction='outbound')
        self.assertEqual(transport1_msgs, [msg])
        transport2_msgs = self.get_dispatched_messages('transport2',
                                                       direction='outbound')
        self.assertEqual(transport2_msgs, [])

        app2_route = self.fake_redis.get('keyword_dispatcher:message:1')
        self.assertEqual(app2_route, 'app2')
Exemplo n.º 50
0
 def test_delete(self):
     self.r_server = FakeRedis()
     self.r_server.set("delete_me", 1)
     self.assertEqual(True, self.r_server.delete("delete_me"))
     self.assertEqual(False, self.r_server.delete("delete_me"))
Exemplo n.º 51
0
 def setUp(self):
     self.prefix = "testpool"
     self.tagpool = TagpoolManager(FakeRedis(), self.prefix)
Exemplo n.º 52
0
class FakeRedisIncrTestCase(TestCase):

    def test_incr(self):
        self.r_server = FakeRedis()
        self.r_server.set("inc", 1)
        self.assertEqual('1', self.r_server.get("inc"))
        self.assertEqual('2', self.r_server.incr("inc"))
        self.assertEqual('3', self.r_server.incr("inc"))


    def test_incrby(self):
        self.r_server = FakeRedis()
        self.r_server.set("inc", 1)
        self.assertEqual('1', self.r_server.get("inc"))
        self.assertEqual('2', self.r_server.incrby("inc", 1))
        self.assertEqual('4', self.r_server.incrby("inc", 2))
        self.assertEqual('7', self.r_server.incrby("inc", 3))
        self.assertEqual('11', self.r_server.incrby("inc", 4))
        self.assertEqual('111', self.r_server.incrby("inc", 100))