Skip to content

Commit

Permalink
Merge pull request #31 from marrrvin/fix-pool
Browse files Browse the repository at this point in the history
AttributeError and lock if ClientError was pushed pool
  • Loading branch information
thefab committed Aug 28, 2016
2 parents 495cc8c + f380e92 commit 1af6d76
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 58 deletions.
4 changes: 4 additions & 0 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def test_redis_or_raise_skiptest(host="localhost", port=6379):
except socket.error:
raise unittest.SkipTest("redis must be launched on %s:%i" % (host,
port))
finally:
s.close()


def test_redis_uds_or_raise_skiptest(uds="/tmp/redis.sock"):
Expand All @@ -20,3 +22,5 @@ def test_redis_uds_or_raise_skiptest(uds="/tmp/redis.sock"):
s.connect(uds)
except socket.error:
raise unittest.SkipTest("redis must listen on %s" % uds)
finally:
s.close()
8 changes: 4 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_ping(self):
c = Client()
yield c.connect()
res = yield c.call('PING')
self.assertEquals(res, b"PONG")
self.assertEqual(res, b"PONG")
c.disconnect()

@tornado.testing.gen_test
Expand All @@ -46,11 +46,11 @@ def test_reply_error(self):
def test_autoconnect_future(self):
c = Client(autoconnect=True)
res = yield c.call('PING')
self.assertEquals(res, b"PONG")
self.assertEqual(res, b"PONG")
c.disconnect()

def _test_autoconnect_callback_cb(self, condition, result):
self.assertEquals(result, b"PONG")
self.assertEqual(result, b"PONG")
condition.notify()

@tornado.testing.gen_test
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_server_close_connection(self):
self.assertFalse(c.is_connected())
yield c.connect()
res2 = yield c.call("PING")
self.assertEquals(res2, b"PONG")
self.assertEqual(res2, b"PONG")
c.disconnect()
c2.disconnect()

Expand Down
12 changes: 6 additions & 6 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def test_bigwrite(self):
c.write(data2)
reply1 = yield self.reply_queue.get()
reply2 = yield self.reply_queue.get()
self.assertEquals(reply1, b"OK")
self.assertEquals(reply2, BIG_VALUE)
self.assertEqual(reply1, b"OK")
self.assertEqual(reply2, BIG_VALUE)
c.disconnect()

@tornado.testing.gen_test
Expand Down Expand Up @@ -202,9 +202,9 @@ def _test_basic_write(self):
c.write(data1)
c.write(data2)
reply1 = yield self.reply_queue.get()
self.assertEquals(reply1, b"PONG")
self.assertEqual(reply1, b"PONG")
reply2 = yield self.reply_queue.get()
self.assertEquals(reply2, b"OK")
self.assertEqual(reply2, b"OK")
c.disconnect()

@tornado.testing.gen_test
Expand Down Expand Up @@ -242,9 +242,9 @@ def test_write_on_closed_socket(self):
c.write(data1)
c.write(data2)
reply1 = yield self.reply_queue.get()
self.assertEquals(reply1, b"PONG")
self.assertEqual(reply1, b"PONG")
reply2 = yield self.reply_queue.get()
self.assertEquals(reply2, b"OK")
self.assertEqual(reply2, b"OK")
c.write(data1)
# Wait a short moment while the server closes the socket
yield tornado.gen.sleep(.0001)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def test_basic_pipeline(self):
p.stack_call('PING')
p.stack_call('PING')
res = yield c.call(p)
self.assertEquals(len(res), 2)
self.assertEquals(res[0], b'PONG')
self.assertEquals(res[1], b'PONG')
self.assertEqual(len(res), 2)
self.assertEqual(res[0], b'PONG')
self.assertEqual(res[1], b'PONG')
c.disconnect()

@tornado.testing.gen_test
Expand Down
18 changes: 18 additions & 0 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ def test_get_client2(self):
c.release_client(client3)
c.destroy()

@tornado.testing.gen_test
def test_get_client_client_error(self):
wrong_port = 11111

c = ClientPool(max_size=1, port=wrong_port)

client = yield c.get_connected_client()

self.assertTrue(isinstance(client, ClientError))

c.release_client(client)

new_client = yield c.get_connected_client()

self.assertTrue(isinstance(new_client, ClientError))

c.destroy()

@tornado.testing.gen_test
def test_get_client_nowait1(self):
c = ClientPool()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ def test_pubsub(self):
self.assertTrue(res)
tornado.ioloop.IOLoop.instance().add_future(self.publish(c2), None)
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[2], b"value1")
self.assertEqual(msg[2], b"value1")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[2], b"value2")
self.assertEqual(msg[2], b"value2")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[3], b"value3")
self.assertEqual(msg[3], b"value3")
msg = yield c.pubsub_pop_message()
self.assertEquals(msg[3], b"value4")
self.assertEqual(msg[3], b"value4")
msg = yield c.pubsub_pop_message(deadline=1)
self.assertEquals(msg, None)
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.assertEquals(msg, None)
self.assertEqual(msg, None)
yield c.pubsub_unsubscribe("foo2")
yield c.pubsub_unsubscribe("foobar")
yield c.pubsub_punsubscribe("foobar*")
Expand Down
28 changes: 14 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@ class UtilsTestCase(tornado.testing.AsyncTestCase):

def test_protocol1(self):
res = bytes(format_args_in_redis_protocol("PING"))
self.assertEquals(res, b"*1\r\n$4\r\nPING\r\n")
self.assertEqual(res, b"*1\r\n$4\r\nPING\r\n")

def test_protocol2(self):
res = bytes(format_args_in_redis_protocol("SET", "key", "foobar"))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")
res = bytes(format_args_in_redis_protocol("SET", "key",
six.u("foobar")))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")

def test_protocol3(self):
res = bytes(format_args_in_redis_protocol("SET", "key", six.u("\xe9")))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$2\r\n\xc3\xa9\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$2\r\n\xc3\xa9\r\n")

def test_protocol4(self):
res = bytes(format_args_in_redis_protocol("SET", "key", b"\000"))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$1\r\n\000\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$1\r\n\000\r\n")

def test_protocol5(self):
res = bytes(format_args_in_redis_protocol("SET", "key", 1))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$1\r\n1\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$1\r\n1\r\n")

def test_protocol6(self):
wb = WriteBuffer()
wb.append(b"foobar")
res = bytes(format_args_in_redis_protocol("SET", "key", wb))
self.assertEquals(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")
self.assertEqual(res, b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n"
b"$6\r\nfoobar\r\n")

def test_protocol_exception(self):
self.assertRaises(Exception, format_args_in_redis_protocol, ["foo"])
Expand All @@ -65,7 +65,7 @@ def test_context_manager_future(self):
cb = self._test_context_manager_future_cb
cmf = ContextManagerFuture(future, cb)
with (yield cmf) as value:
self.assertEquals(value, "foobar")
self.assertEqual(value, "foobar")
boolean = self._test_context_manager_future_cb_called
self.assertTrue(boolean)

Expand Down
48 changes: 24 additions & 24 deletions tests/test_writebuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,53 @@ def _pop_chunk_as_str(self, buf, max_size):
def test_empty_write_buffer(self):
x = WriteBuffer()
s = bytes(x)
self.assertEquals(s, b"")
self.assertEqual(s, b"")
c = x.pop_chunk(4096)
self.assertEquals(c, b"")
self.assertEquals(len(x), 0)
self.assertEqual(c, b"")
self.assertEqual(len(x), 0)

def test_write_buffer1(self):
b = self._make_test_buffer()
s = bytes(b)
self.assertEquals(len(b), 9)
self.assertEquals(s, b"123456789")
self.assertEqual(len(b), 9)
self.assertEqual(s, b"123456789")
self.assertFalse(b.is_empty())
self.assertEquals(b._total_length, 9)
self.assertEqual(b._total_length, 9)
b2 = self._make_test_buffer()
b.append(b2)
s = bytes(b)
self.assertEquals(s, b"123456789123456789")
self.assertEqual(s, b"123456789123456789")
self.assertFalse(b.is_empty())
self.assertEquals(b._total_length, 18)
self.assertEqual(b._total_length, 18)
chunk = self._pop_chunk_as_str(b, 1000)
self.assertEquals(chunk, b"123456789123456789")
self.assertEqual(chunk, b"123456789123456789")
self.assertTrue(b.is_empty())

def test_write_buffer2(self):
b = self._make_test_buffer()
chunk = self._pop_chunk_as_str(b, 1)
self.assertEquals(chunk, b"1")
self.assertEquals(bytes(b), b"23456789")
self.assertEquals(b._total_length, 8)
self.assertEqual(chunk, b"1")
self.assertEqual(bytes(b), b"23456789")
self.assertEqual(b._total_length, 8)
chunk = self._pop_chunk_as_str(b, 1)
self.assertEquals(chunk, b"2")
self.assertEquals(bytes(b), b"3456789")
self.assertEquals(b._total_length, 7)
self.assertEqual(chunk, b"2")
self.assertEqual(bytes(b), b"3456789")
self.assertEqual(b._total_length, 7)
chunk = self._pop_chunk_as_str(b, 4)
self.assertEquals(chunk, b"3456")
self.assertEquals(bytes(b), b"789")
self.assertEquals(b._total_length, 3)
self.assertEqual(chunk, b"3456")
self.assertEqual(bytes(b), b"789")
self.assertEqual(b._total_length, 3)
chunk = self._pop_chunk_as_str(b, 10)
self.assertEquals(chunk, b"789")
self.assertEquals(bytes(b), b"")
self.assertEquals(b._total_length, 0)
self.assertEqual(chunk, b"789")
self.assertEqual(bytes(b), b"")
self.assertEqual(b._total_length, 0)

def test_write_buffer3(self):
b = WriteBuffer()
b.append(b"x" * 10000)
chunk = self._pop_chunk_as_str(b, 4000)
self.assertEquals(len(chunk), 4000)
self.assertEqual(len(chunk), 4000)
chunk = self._pop_chunk_as_str(b, 4000)
self.assertEquals(len(chunk), 4000)
self.assertEqual(len(chunk), 4000)
chunk = self._pop_chunk_as_str(b, 4000)
self.assertEquals(len(chunk), 2000)
self.assertEqual(len(chunk), 2000)
2 changes: 1 addition & 1 deletion tornadis/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def release_client(self, client):
Args:
client: Client object.
"""
if not self._is_expired_client(client):
if isinstance(client, Client) and not self._is_expired_client(client):
self.__pool.append(client)
if self.__sem is not None:
self.__sem.release()
Expand Down

0 comments on commit 1af6d76

Please sign in to comment.