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 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)
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)
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)
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)
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
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!")
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)
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")
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"])
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()
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!")
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)
def test_set_no_callback(self): client = Client(ioloop=self.io_loop) client.connect() with self.assertRaises(Exception): client.set("foo", "bar1")