Beispiel #1
0
 def setUp(self):
     super(TestPipeline, self).setUp()
     self.client = Client(io_loop=self.io_loop)
     self.client.connect(callback=self.stop)
     self.wait()
     self.client.flushdb(callback=self.stop)
     self.wait()
Beispiel #2
0
 def test_connect(self):
     client = Client(ioloop=self.io_loop)
     result = {}
     def callback():
         result["connected"] = True
         self.stop()
     client.connect(callback=callback)
     self.wait()
     # blocks
     self.assertTrue("connected" in result)
Beispiel #3
0
    def test_connect(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def callback():
            result["connected"] = True
            self.stop()
        client.connect(callback=callback)
        self.wait()
        # blocks
        self.assertTrue("connected" in result)
Beispiel #4
0
 def test_pub_command(self):
     client = Client(ioloop=self.io_loop)
     result = {}
     def pub_callback(response):
         result["pub"] = response
         self.stop()
     client.connect()
     client.publish("foobar", "message", callback=pub_callback)
     self.wait()
     # blocks
     self.assertTrue("pub" in result)
     self.assertEqual(result["pub"], 0) # no subscribers yet
Beispiel #5
0
 def test_set_command(self):
     client = Client(ioloop=self.io_loop)
     result = {}
     def set_callback(response):
         result["set"] = response
         self.stop()
     client.connect()
     client.set("foo", "bar", callback=set_callback)
     self.wait()
     #blocks
     self.assertTrue("set" in result)
     self.assertEqual(result["set"], "OK")
     conn = redis.Redis()
     self.assertEqual(conn["foo"], "bar")
Beispiel #6
0
 def test_pubsub_disconnect(self):
     client = Client(io_loop=self.io_loop)
     client.connect()
     client.subscribe("foo", lambda: None)
     client.close()
     with self.assertRaises(IOError):
         client._stream.read_bytes(1024, lambda x: x)
Beispiel #7
0
 def test_get_command(self):
     client = Client(ioloop=self.io_loop)
     result = {}
     def get_callback(response):
         result["get"] = response
         self.stop()
     time_string = "%s" % time.time()
     conn = redis.Redis()
     conn["foo"] = time_string
     client.connect()
     client.get("foo", callback=get_callback)
     self.wait()
     #blocks
     self.assertTrue("get" in result)
     self.assertEqual(result["get"], time_string)
Beispiel #8
0
 def setUp(self):
     super(TestPipeline, self).setUp()
     self.client = Client(io_loop=self.io_loop)
     self.client.connect(callback=self.stop)
     self.wait()
     self.client.flushdb(callback=self.stop)
     self.wait()
Beispiel #9
0
    def _new_client(self):
        if self._pool_size and len(self._active_cache) >= self._pool_size:
            if self._reuse_actives:
                return self._active_cache[0]

            raise TooManyClients("TOO MANY CLIENTS: %d, %d" %
                    (len(self._active_cache), self._pool_size))

        logger.info('MAKE NEW CLIENT')

        kwargs = self._kwargs
        kwargs['pool'] = self

        _client = Client(*self._args, **kwargs)
        _client.connect(host=self._host, port=self._port)

        return _client
Beispiel #10
0
 def test_pubsub_disconnect(self):
     client = Client(io_loop=self.io_loop)
     client.connect()
     client.subscribe("foo", lambda: None)
     client.close()
     with self.assertRaises(IOError):
         client._stream.read_bytes(1024, lambda x: x)
Beispiel #11
0
def main():
    """ Start the application and Twitter stream monitor """
    options, args = OPTIONS.parse_args()
    if options.port:
        SETTINGS["port"] = options.port
    ioloop = IOLoop.instance()
    prefetch_tweets(ioloop)
    app = Application([
        (r"/", PageHandler),
        (r"/stream", StreamHandler),
        (r"/poll", PollHandler)
    ], static_path="static", debug=True)
    app.listen(SETTINGS["port"])
    client = Client() # redis connection for all connected clients
    client.connect()
    client.subscribe(SETTINGS["redis_pub_channel"], callback=handle_message)
    # Monitor Twitter Stream once for all clients
    ioloop.start()
Beispiel #12
0
    def test_set_command(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def set_callback(response):
            result["set"] = response
            self.stop()

        client.connect()
        client.set("foo", "bar", callback=set_callback)
        self.wait()
        #blocks
        self.assertTrue("set" in result)
        self.assertEqual(result["set"], b"OK")

        conn = Client()
        conn.connect()
        value = yield gen.Task(conn.get, "foo")
        self.assertEqual("bar", value)
Beispiel #13
0
    def test_get_command(self):
        client = Client(io_loop=self.io_loop)
        result = None

        def get_callback(response):
            result = response
            self.stop()

        time_string = "%s" % time.time()
        conn = Client()
        conn.connect()
        yield gen.Task(conn.set, "foo", time_string)

        client.connect()
        client.get("foo", callback=get_callback)
        self.wait()
        #blocks

        self.assertTrue(result is not None, 'result is %s' % result)
        self.assertEqual(time_string, result)
Beispiel #14
0
    def test_sub_command(self):
        client = Client(ioloop=self.io_loop)
        result = {"message_count": 0}
        conn = redis.Redis()

        def sub_callback(response):
            if response[0] == "subscribe":
                result["sub"] = response
                conn.publish("foobar", "new message!")
            elif response[0] == "message":
                result["message_count"] += 1
                if result["message_count"] < 100:
                    count = result["message_count"]
                    return conn.publish("foobar", "new message %s!" % count)
                result["message"] = response[2]
                self.stop()

        client.connect()
        client.subscribe("foobar", callback=sub_callback)
        self.wait()
        # blocks
        self.assertTrue("sub" in result)
        self.assertTrue("message" in result)
        self.assertTrue(result["message"], "new message 99!")
Beispiel #15
0
    def test_set_command(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def set_callback(response):
            result["set"] = response
            self.stop()

        client.connect()
        client.set("foo", "bar", callback=set_callback)
        self.wait()
        #blocks
        self.assertTrue("set" in result)
        self.assertEqual(result["set"], b"OK")

        conn = Client()
        conn.connect()
        value = yield gen.Task(conn.get, "foo")
        self.assertEqual("bar", value)
Beispiel #16
0
    def test_get_command(self):
        client = Client(io_loop=self.io_loop)
        result = None

        def get_callback(response):
            result = response
            self.stop()
        time_string = "%s" % time.time()
        conn = Client()
        conn.connect()
        yield gen.Task(conn.set, "foo", time_string)

        client.connect()
        client.get("foo", callback=get_callback)
        self.wait()
        #blocks

        self.assertTrue(result is not None, 'result is %s' % result)
        self.assertEqual(time_string, result)
Beispiel #17
0
    def test_pub_command(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def pub_callback(response):
            result["pub"] = response
            self.stop()
        client.connect()
        client.publish("foobar", "message", callback=pub_callback)
        self.wait()
        # blocks
        self.assertTrue("pub" in result)
        self.assertEqual(result["pub"], 0)  # no subscribers yet
Beispiel #18
0
    def test_sub_command(self):
        client = Client(io_loop=self.io_loop)
        result = {"message_count": 0}
        conn = Client()
        conn.connect()

        client.connect()
        response = yield gen.Task(client.subscribe, "foobar")
        if response[0] == "subscribe":
            result["sub"] = response
            yield gen.Task(conn.publish, "foobar", "new message!")
        elif response[0] == "message":
            result["message_count"] += 1
            if result["message_count"] < 100:
                count = result["message_count"]
                value = yield gen.Task(conn.publish, "foobar",
                                       "new message %s!" % count)
            result["message"] = response[2]

        self.assertTrue("sub" in result)
        self.assertTrue("message" in result)
        self.assertTrue(result["message"], "new message 99!")
Beispiel #19
0
    def test_get_command(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def get_callback(response):
            result["get"] = response
            self.stop()
        time_string = "%s" % time.time()
        conn = redis.Redis()
        conn["foo"] = time_string
        client.connect()
        client.get("foo", callback=get_callback)
        self.wait()
        #blocks
        self.assertTrue("get" in result)
        self.assertEqual(result["get"], time_string)
Beispiel #20
0
    def test_set_command(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def set_callback(response):
            result["set"] = response
            self.stop()

        client.connect()
        client.set("foo", "bar", callback=set_callback)
        self.wait()
        #blocks
        self.assertTrue("set" in result)
        self.assertEqual(result["set"], "OK")
        conn = redis.Redis()
        self.assertEqual(conn["foo"].decode('utf-8'), "bar")
Beispiel #21
0
def main():
    """ Start the application and Twitter stream monitor """
    options, args = OPTIONS.parse_args()
    if options.port:
        SETTINGS["port"] = options.port
    ioloop = IOLoop.instance()
    prefetch_tweets(ioloop)
    app = Application([(r"/", PageHandler), (r"/stream", StreamHandler),
                       (r"/poll", PollHandler)],
                      static_path="static",
                      debug=True)
    app.listen(SETTINGS["port"])
    client = Client()  # redis connection for all connected clients
    client.connect()
    client.subscribe(SETTINGS["redis_pub_channel"], callback=handle_message)
    # Monitor Twitter Stream once for all clients
    ioloop.start()
Beispiel #22
0
    def test_blpop(self):
        client = Client(io_loop=self.io_loop)
        result = {}

        def rpush_callback(response):
            result["push"] = response

            def blpop_callback(response):
                result["pop"] = response
                self.stop()

            client.blpop("test", 0, blpop_callback)

        client.connect()
        client.rpush("test", "dummy", rpush_callback)
        self.wait()
        self.assertEqual(result["pop"], ["test", "dummy"])
Beispiel #23
0
    def test_sub_command(self):
        client = Client(io_loop=self.io_loop)
        result = {"message_count": 0}
        conn = Client()
        conn.connect()

        client.connect()
        response = yield gen.Task(client.subscribe, "foobar")
        if response[0] == "subscribe":
            result["sub"] = response
            yield gen.Task(conn.publish, "foobar", "new message!")
        elif response[0] == "message":
            result["message_count"] += 1
            if result["message_count"] < 100:
                count = result["message_count"]
                value = yield gen.Task(conn.publish,
                                       "foobar", "new message %s!" % count)
            result["message"] = response[2]

        self.assertTrue("sub" in result)
        self.assertTrue("message" in result)
        self.assertTrue(result["message"], "new message 99!")
Beispiel #24
0
    def test_sub_command(self):
        client = Client(io_loop=self.io_loop)
        result = {"message_count": 0}
        conn = redis.Redis()

        def sub_callback(response):
            if response[0] == "subscribe":
                result["sub"] = response
                conn.publish("foobar", "new message!")
            elif response[0] == "message":
                result["message_count"] += 1
                if result["message_count"] < 100:
                    count = result["message_count"]
                    return conn.publish("foobar", "new message %s!" % count)
                result["message"] = response[2]
                self.stop()

        client.connect()
        client.subscribe("foobar", callback=sub_callback)
        self.wait()
        # blocks
        self.assertTrue("sub" in result)
        self.assertTrue("message" in result)
        self.assertTrue(result["message"], "new message 99!")
Beispiel #25
0
class TestPipeline(AsyncTestCase):

    def setUp(self):
        super(TestPipeline, self).setUp()
        self.client = Client(io_loop=self.io_loop)
        self.client.connect(callback=self.stop)
        self.wait()
        self.client.flushdb(callback=self.stop)
        self.wait()

    def tearDown(self):
        self.client.close()
        super(TestPipeline, self).tearDown()

    def test_pipeline_send(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.set('foo2', 'bar2')
        pipeline.set('foo3', 'bar3')
        pipeline.get('foo1')
        pipeline.mget(['foo2', 'foo3'])
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(
            response, [b'OK', b'OK', b'OK', b'bar1', [b'bar2', b'bar3']]
        )

    def test_pipeline_reset(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.reset()
        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar2'])

    def test_pipeline_with_error(self):
        pipeline = self.client.pipeline()
        pipeline.eval('invalid', [], [])
        pipeline.eval(
            'return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
            ['key1', 'key2'], ['arg1', 'arg2']
        )
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertIsInstance(response[0], ReplyError)
        self.assertEqual(response[1], [b'key1', b'key2', b'arg1', b'arg2'])

    def test_pipeline_small(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo', 'bar')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK'])

    def test_pipeline_big(self):
        pipeline = self.client.pipeline()
        expected = []
        for i in range(10000):
            pipeline.set('foo%d' % i, 'bar%d' % i)
            pipeline.get('foo%d' % i)
            expected.extend([b'OK', b'bar' + str(i).encode()])
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, expected)

    def test_pipeline_and_commands(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar1'])

        self.client.eval(
            'return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
            ['key1', 'key2'], ['arg1', 'arg2'],
            callback=self.stop
        )
        response = self.wait()
        self.assertEqual(response, [b'key1', b'key2', b'arg1', b'arg2'])

        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar2'])

    def test_parallel_pipeline_and_commands(self):
        result = {}

        def callback_pipeline1(response):
            result['pipeline1'] = response
            self.stop()

        def callback_pipeline2(response):
            result['pipeline2'] = response
            self.stop()

        def callback_eval(response):
            result['eval'] = response
            self.stop()

        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.send(callback=callback_pipeline1)

        self.client.eval(
            'return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
            ['key1', 'key2'], ['arg1', 'arg2'],
            callback=callback_eval
        )

        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=callback_pipeline2)

        self.wait(lambda: len(result) == 3)
        self.assertEqual(result, {
            'pipeline1': [b'OK', b'bar1'],
            'eval': [b'key1', b'key2', b'arg1', b'arg2'],
            'pipeline2': [b'OK', b'bar2']
        })

    def test_parallel_pipelines(self):
        result = {}

        def callback_pipeline1(response):
            result['pipeline1'] = response
            self.stop()

        def callback_pipeline2(response):
            result['pipeline2'] = response
            self.stop()

        pipeline1 = Pipeline(self.client)
        pipeline1.set('foo1', 'bar1')
        pipeline1.get('foo1')
        pipeline1.send(callback=callback_pipeline1)

        pipeline2 = Pipeline(self.client)
        pipeline2.set('foo2', 'bar2')
        pipeline2.get('foo2')
        pipeline2.send(callback=callback_pipeline2)

        self.wait(lambda: len(result) == 2)
        self.assertEqual(result, {
            'pipeline1': [b'OK', b'bar1'],
            'pipeline2': [b'OK', b'bar2']
        })
Beispiel #26
0
class TestPipeline(AsyncTestCase):
    def setUp(self):
        super(TestPipeline, self).setUp()
        self.client = Client(io_loop=self.io_loop)
        self.client.connect(callback=self.stop)
        self.wait()
        self.client.flushdb(callback=self.stop)
        self.wait()

    def tearDown(self):
        self.client.close()
        super(TestPipeline, self).tearDown()

    def test_pipeline_send(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.set('foo2', 'bar2')
        pipeline.set('foo3', 'bar3')
        pipeline.get('foo1')
        pipeline.mget(['foo2', 'foo3'])
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response,
                         [b'OK', b'OK', b'OK', b'bar1', [b'bar2', b'bar3']])

    def test_pipeline_reset(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.reset()
        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar2'])

    def test_pipeline_with_error(self):
        pipeline = self.client.pipeline()
        pipeline.eval('invalid', [], [])
        pipeline.eval('return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
                      ['key1', 'key2'], ['arg1', 'arg2'])
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertIsInstance(response[0], ReplyError)
        self.assertEqual(response[1], [b'key1', b'key2', b'arg1', b'arg2'])

    def test_pipeline_small(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo', 'bar')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK'])

    def test_pipeline_big(self):
        pipeline = self.client.pipeline()
        expected = []
        for i in range(10000):
            pipeline.set('foo%d' % i, 'bar%d' % i)
            pipeline.get('foo%d' % i)
            expected.extend([b'OK', b'bar' + str(i).encode()])
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, expected)

    def test_pipeline_and_commands(self):
        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar1'])

        self.client.eval('return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
                         ['key1', 'key2'], ['arg1', 'arg2'],
                         callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'key1', b'key2', b'arg1', b'arg2'])

        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=self.stop)
        response = self.wait()
        self.assertEqual(response, [b'OK', b'bar2'])

    def test_parallel_pipeline_and_commands(self):
        result = {}

        def callback_pipeline1(response):
            result['pipeline1'] = response
            self.stop()

        def callback_pipeline2(response):
            result['pipeline2'] = response
            self.stop()

        def callback_eval(response):
            result['eval'] = response
            self.stop()

        pipeline = self.client.pipeline()
        pipeline.set('foo1', 'bar1')
        pipeline.get('foo1')
        pipeline.send(callback=callback_pipeline1)

        self.client.eval('return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}',
                         ['key1', 'key2'], ['arg1', 'arg2'],
                         callback=callback_eval)

        pipeline.set('foo2', 'bar2')
        pipeline.get('foo2')
        pipeline.send(callback=callback_pipeline2)

        self.wait(lambda: len(result) == 3)
        self.assertEqual(
            result, {
                'pipeline1': [b'OK', b'bar1'],
                'eval': [b'key1', b'key2', b'arg1', b'arg2'],
                'pipeline2': [b'OK', b'bar2']
            })

    def test_parallel_pipelines(self):
        result = {}

        def callback_pipeline1(response):
            result['pipeline1'] = response
            self.stop()

        def callback_pipeline2(response):
            result['pipeline2'] = response
            self.stop()

        pipeline1 = Pipeline(self.client)
        pipeline1.set('foo1', 'bar1')
        pipeline1.get('foo1')
        pipeline1.send(callback=callback_pipeline1)

        pipeline2 = Pipeline(self.client)
        pipeline2.set('foo2', 'bar2')
        pipeline2.get('foo2')
        pipeline2.send(callback=callback_pipeline2)

        self.wait(lambda: len(result) == 2)
        self.assertEqual(result, {
            'pipeline1': [b'OK', b'bar1'],
            'pipeline2': [b'OK', b'bar2']
        })
Beispiel #27
0
 def test_disconnect(self):
     client = Client(ioloop=self.io_loop)
     client.connect()
     client.disconnect()
     with self.assertRaises(IOError):
         client._stream.read_bytes(1024, lambda x: x)
Beispiel #28
0
 def test_disconnect(self):
     client = Client(io_loop=self.io_loop)
     client.connect()
     client.close()
     with self.assertRaises(IOError):
         client._stream.read_bytes(1024, lambda x: x)
Beispiel #29
0
 def test_set_no_callback(self):
     client = Client(ioloop=self.io_loop)
     client.connect()
     with self.assertRaises(Exception):
         client.set("foo", "bar1")
Beispiel #30
0
 def test_set_no_callback(self):
     client = Client(ioloop=self.io_loop)
     client.connect()
     with self.assertRaises(Exception):
         client.set("foo", "bar1")