Example #1
0
 def test_empty_pipeline(self):
     c = Client()
     yield c.connect()
     p = Pipeline()
     res = yield c.call(p)
     self.assertTrue(isinstance(res, ClientError))
     c.disconnect()
Example #2
0
 def test_autoconnect_callback(self):
     condition = tornado.locks.Condition()
     c = Client(autoconnect=True)
     cb = functools.partial(self._test_autoconnect_callback_cb, condition)
     c.async_call('PING', callback=cb)
     yield condition.wait()
     c.disconnect()
Example #3
0
 def test_read_timeout1(self):
     orig_constructor = socket.socket
     socket.socket = functools.partial(fake_socket_constructor,
                                       FakeSocketObject5)
     c = Client(read_timeout=1)
     res = yield c.connect()
     self.assertTrue(res)
     res2 = yield c.call('PING')
     self.assertTrue(isinstance(res2, ConnectionError))
     c.disconnect()
     socket.socket = orig_constructor
Example #4
0
 def test_basic_pipeline(self):
     c = Client()
     yield c.connect()
     p = Pipeline()
     p.stack_call('PING')
     p.stack_call('PING')
     res = yield c.call(p)
     self.assertEqual(len(res), 2)
     self.assertEqual(res[0], b'PONG')
     self.assertEqual(res[1], b'PONG')
     c.disconnect()
Example #5
0
 def test_pubsub(self):
     c = PubSubClient()
     c2 = Client()
     yield c.connect()
     yield c2.connect()
     try:
         yield c.pubsub_pop_message()
         raise Exception("exception not raised")
     except Exception:
         pass
     res = yield c.pubsub_subscribe("foo1", "foo2")
     self.assertTrue(res)
     self.assertTrue(c.subscribed)
     self.assertFalse(c2.subscribed)
     try:
         yield c.call("PING")
         raise Exception("exception not raised")
     except Exception:
         pass
     res = yield c.pubsub_psubscribe("bar1*", "bar2*")
     self.assertTrue(res)
     tornado.ioloop.IOLoop.instance().add_future(self.publish(c2), None)
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[2], b"value1")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[2], b"value2")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[3], b"value3")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[3], b"value4")
     msg = yield c.pubsub_pop_message(deadline=1)
     self.assertEqual(msg, None)
     yield c.pubsub_unsubscribe("foo1")
     yield c2.call("PUBLISH", "foo1", "value1")
     c2.disconnect()
     msg = yield c.pubsub_pop_message(deadline=1)
     self.assertEqual(msg, None)
     yield c.pubsub_unsubscribe("foo2")
     yield c.pubsub_unsubscribe("foobar")
     yield c.pubsub_punsubscribe("foobar*")
     yield c.pubsub_punsubscribe("bar1*")
     yield c.pubsub_punsubscribe("bar2*")
     self.assertFalse(c.subscribed)
     c.disconnect()
Example #6
0
 def _pubsub_subscribe(self, command, *args):
     if len(args) == 0:
         raise ClientError("you must provide at least one argument")
     results = yield Client.call(self, command, *args,
                                 __multiple_replies=len(args))
     for reply in results:
         if isinstance(reply, ConnectionError) or len(reply) != 3 or \
                 reply[0].lower() != command.lower() or reply[2] == 0:
             raise tornado.gen.Return(False)
     self.subscribed = True
     raise tornado.gen.Return(True)
Example #7
0
 def test_client_close_connection(self):
     c = Client()
     yield c.connect()
     connection = c._Client__connection
     socket = connection._Connection__socket
     socket.close()
     res = yield c.call("PING")
     self.assertTrue(isinstance(res, ConnectionError))
     self.assertFalse(c.is_connected())
     c.disconnect()
Example #8
0
 def test_read_timeout2(self):
     c = Client(read_timeout=1, autoconnect=False)
     res = yield c.connect()
     self.assertTrue(res)
     res2 = yield c.call('PING')
     self.assertEqual(res2, b"PONG")
     yield tornado.gen.sleep(2)
     res3 = yield c.call('PING')
     self.assertTrue(isinstance(res3, ConnectionError))
     c.disconnect()
Example #9
0
 def test_autoconnect_callback(self):
     condition = tornado.locks.Condition()
     c = Client(autoconnect=True)
     cb = functools.partial(self._test_autoconnect_callback_cb, condition)
     c.async_call('PING', callback=cb)
     yield condition.wait()
     c.disconnect()
Example #10
0
 def test_empty_pipeline(self):
     c = Client()
     yield c.connect()
     p = Pipeline()
     res = yield c.call(p)
     self.assertTrue(isinstance(res, ClientError))
     c.disconnect()
Example #11
0
 def _pubsub_subscribe(self, command, *args):
     if len(args) == 0:
         raise ClientError("you must provide at least one argument")
     results = yield Client.call(self,
                                 command,
                                 *args,
                                 __multiple_replies=len(args))
     for reply in results:
         if isinstance(reply, ConnectionError) or len(reply) != 3 or \
                 reply[0].lower() != command.lower() or reply[2] == 0:
             raise tornado.gen.Return(False)
     self.subscribed = True
     raise tornado.gen.Return(True)
Example #12
0
 def _pubsub_unsubscribe(self, command, *args):
     if len(args) == 0:
         # see https://github.com/thefab/tornadis/issues/17
         args_len = 1
     else:
         args_len = len(args)
     results = yield Client.call(self, command, *args,
                                 __multiple_replies=args_len)
     for reply in results:
         if isinstance(reply, ConnectionError) or len(reply) != 3 or \
                 reply[0].lower() != command.lower():
             raise tornado.gen.Return(False)
         if reply[2] == 0:
             self.subscribed = False
     raise tornado.gen.Return(True)
Example #13
0
 def test_empty_pipeline(self):
     c = Client()
     yield c.connect()
     p = Pipeline()
     try:
         yield c.call(p)
         raise Exception("not raised ClientError")
     except ClientError:
         pass
     c.disconnect()
Example #14
0
 def test_client_close_connection(self):
     c = Client()
     yield c.connect()
     connection = c._Client__connection
     socket = connection._Connection__socket
     socket.close()
     res = yield c.call("PING")
     self.assertTrue(isinstance(res, ConnectionError))
     self.assertFalse(c.is_connected())
     c.disconnect()
Example #15
0
 def test_read_timeout2(self):
     c = Client(read_timeout=1, autoconnect=False)
     res = yield c.connect()
     self.assertTrue(res)
     res2 = yield c.call('PING')
     self.assertEqual(res2, b"PONG")
     yield tornado.gen.sleep(2)
     res3 = yield c.call('PING')
     self.assertTrue(isinstance(res3, ConnectionError))
     c.disconnect()
Example #16
0
 def test_basic_pipeline(self):
     c = Client()
     yield c.connect()
     p = Pipeline()
     p.stack_call('PING')
     p.stack_call('PING')
     res = yield c.call(p)
     self.assertEqual(len(res), 2)
     self.assertEqual(res[0], b'PONG')
     self.assertEqual(res[1], b'PONG')
     c.disconnect()
Example #17
0
 def test_read_timeout1(self):
     orig_constructor = socket.socket
     socket.socket = functools.partial(fake_socket_constructor,
                                       FakeSocketObject5)
     c = Client(read_timeout=1)
     res = yield c.connect()
     self.assertTrue(res)
     res2 = yield c.call('PING')
     self.assertTrue(isinstance(res2, ConnectionError))
     c.disconnect()
     socket.socket = orig_constructor
Example #18
0
 def _pubsub_unsubscribe(self, command, *args):
     if len(args) == 0:
         # see https://github.com/thefab/tornadis/issues/17
         args_len = 1
     else:
         args_len = len(args)
     results = yield Client.call(self,
                                 command,
                                 *args,
                                 __multiple_replies=args_len)
     for reply in results:
         if isinstance(reply, ConnectionError) or len(reply) != 3 or \
                 reply[0].lower() != command.lower():
             raise tornado.gen.Return(False)
         if reply[2] == 0:
             self.subscribed = False
     raise tornado.gen.Return(True)
Example #19
0
 def test_pubsub(self):
     c = PubSubClient()
     c2 = Client()
     yield c.connect()
     yield c2.connect()
     try:
         yield c.pubsub_pop_message()
         raise Exception("exception not raised")
     except:
         pass
     res = yield c.pubsub_subscribe("foo1", "foo2")
     self.assertTrue(res)
     self.assertTrue(c.subscribed)
     self.assertFalse(c2.subscribed)
     try:
         yield c.call("PING")
         raise Exception("exception not raised")
     except:
         pass
     res = yield c.pubsub_psubscribe("bar1*", "bar2*")
     self.assertTrue(res)
     tornado.ioloop.IOLoop.instance().add_future(self.publish(c2), None)
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[2], b"value1")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[2], b"value2")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[3], b"value3")
     msg = yield c.pubsub_pop_message()
     self.assertEqual(msg[3], b"value4")
     msg = yield c.pubsub_pop_message(deadline=1)
     self.assertEqual(msg, None)
     yield c.pubsub_unsubscribe("foo1")
     yield c2.call("PUBLISH", "foo1", "value1")
     c2.disconnect()
     msg = yield c.pubsub_pop_message(deadline=1)
     self.assertEqual(msg, None)
     yield c.pubsub_unsubscribe("foo2")
     yield c.pubsub_unsubscribe("foobar")
     yield c.pubsub_punsubscribe("foobar*")
     yield c.pubsub_punsubscribe("bar1*")
     yield c.pubsub_punsubscribe("bar2*")
     self.assertFalse(c.subscribed)
     c.disconnect()
Example #20
0
 def _make_client(self):
     """Makes and returns a Client object."""
     kwargs = self.client_kwargs
     client = Client(**kwargs)
     return client
Example #21
0
 def test_reply_error(self):
     c = Client()
     yield c.connect()
     res = yield c.call('BADCOMMAND')
     self.assertTrue(isinstance(res, ClientError))
     c.disconnect()
Example #22
0
 def test_server_close_connection(self):
     c = Client()
     c2 = Client()
     yield c.connect()
     yield c2.connect()
     future1 = c.call('BLPOP', 'test_server_close_connection', 0)
     future2 = self._close_connection(c2)
     res = yield [future1, future2]
     self.assertTrue(isinstance(res[0], ConnectionError))
     self.assertFalse(c.is_connected())
     yield c.connect()
     res2 = yield c.call("PING")
     self.assertEqual(res2, b"PONG")
     c.disconnect()
     c2.disconnect()
Example #23
0
 def test_discard(self):
     c = Client()
     yield c.connect()
     c.async_call('PING')
     c.disconnect()
Example #24
0
 def test_reply_error(self):
     c = Client()
     yield c.connect()
     res = yield c.call('BADCOMMAND')
     self.assertTrue(isinstance(res, ClientError))
     c.disconnect()
Example #25
0
 def test_init(self):
     c = Client()
     yield c.connect()
     c.disconnect()
Example #26
0
 def test_ping(self):
     c = Client()
     yield c.connect()
     res = yield c.call('PING')
     self.assertEqual(res, b"PONG")
     c.disconnect()
Example #27
0
 def test_init_with_db(self):
     c = Client(db=2)
     yield c.connect()
     self.assertEqual(c.db, 2)
     c.disconnect()
Example #28
0
 def test_autoconnect_future(self):
     c = Client(autoconnect=True)
     res = yield c.call('PING')
     self.assertEqual(res, b"PONG")
     c.disconnect()
Example #29
0
 def test_discard(self):
     c = Client()
     yield c.connect()
     c.async_call('PING')
     c.disconnect()
Example #30
0
 def test_ping(self):
     c = Client()
     yield c.connect()
     res = yield c.call('PING')
     self.assertEqual(res, b"PONG")
     c.disconnect()
Example #31
0
 def test_init_with_db(self):
     c = Client(db=2)
     yield c.connect()
     self.assertEqual(c.db, 2)
     c.disconnect()
Example #32
0
 def test_init(self):
     c = Client()
     yield c.connect()
     c.disconnect()
Example #33
0
 def test_server_close_connection(self):
     c = Client()
     c2 = Client()
     yield c.connect()
     yield c2.connect()
     future1 = c.call('BLPOP', 'test_server_close_connection', 0)
     future2 = self._close_connection(c2)
     res = yield [future1, future2]
     self.assertTrue(isinstance(res[0], ConnectionError))
     self.assertFalse(c.is_connected())
     yield c.connect()
     res2 = yield c.call("PING")
     self.assertEqual(res2, b"PONG")
     c.disconnect()
     c2.disconnect()
Example #34
0
 def test_autoconnect_future(self):
     c = Client(autoconnect=True)
     res = yield c.call('PING')
     self.assertEqual(res, b"PONG")
     c.disconnect()