<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.testing
import tornado.ioloop
import tornado

from tornadis.client import Client
from tornadis.exceptions import ClientError, ConnectionError
from support import test_redis_or_raise_skiptest


class ClientTestCase(tornado.testing.AsyncTestCase):

    def setUp(self):
        test_redis_or_raise_skiptest()
        super(ClientTestCase, self).setUp()

    def get_new_ioloop(self):
        return tornado.ioloop.IOLoop.instance()

    @tornado.testing.gen_test
    def test_init(self):
        c = Client()
        yield c.connect()
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_ping(self):
        c = Client()
        yield c.connect()
        res = yield c.call('PING')
        self.assertEquals(res, b"PONG")
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_discard(self):
        c = Client()
        yield c.connect()
        c.call('PING', discard_reply=True)
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_discard_and_callback(self):
        c = Client()
        yield c.connect()
        try:
            c.call('PING', discard_reply=True, callback=lambda x: x)
            raise Exception("ClientError not raised")
        except ClientError:
            pass
        yield c.disconnect()

    @tornado.gen.coroutine
    def _close_connection(self, client):
        yield tornado.gen.sleep(1)
        yield client.call("CLIENT", "KILL", "SKIPME", "YES")

    @tornado.testing.gen_test
    def test_server_close_connection(self):
        c = Client(return_connection_error=True)
        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.assertEquals(res2, "PONG")
        yield c.disconnect()
        yield c2.disconnect()

    @tornado.testing.gen_test
    def test_client_close_connection(self):
        c = Client(return_connection_error=True)
        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())
        yield c.disconnect()
<commit_msg>fix tests on py3<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.testing
import tornado.ioloop
import tornado

from tornadis.client import Client
from tornadis.exceptions import ClientError, ConnectionError
from support import test_redis_or_raise_skiptest


class ClientTestCase(tornado.testing.AsyncTestCase):

    def setUp(self):
        test_redis_or_raise_skiptest()
        super(ClientTestCase, self).setUp()

    def get_new_ioloop(self):
        return tornado.ioloop.IOLoop.instance()

    @tornado.testing.gen_test
    def test_init(self):
        c = Client()
        yield c.connect()
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_ping(self):
        c = Client()
        yield c.connect()
        res = yield c.call('PING')
        self.assertEquals(res, b"PONG")
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_discard(self):
        c = Client()
        yield c.connect()
        c.call('PING', discard_reply=True)
        yield c.disconnect()

    @tornado.testing.gen_test
    def test_discard_and_callback(self):
        c = Client()
        yield c.connect()
        try:
            c.call('PING', discard_reply=True, callback=lambda x: x)
            raise Exception("ClientError not raised")
        except ClientError:
            pass
        yield c.disconnect()

    @tornado.gen.coroutine
    def _close_connection(self, client):
        yield tornado.gen.sleep(1)
        yield client.call("CLIENT", "KILL", "SKIPME", "YES")

    @tornado.testing.gen_test
    def test_server_close_connection(self):
        c = Client(return_connection_error=True)
        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.assertEquals(res2, b"PONG")
        yield c.disconnect()
        yield c2.disconnect()

    @tornado.testing.gen_test
    def test_client_close_connection(self):
        c = Client(return_connection_error=True)
        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())
        yield c.disconnect()

{'fraction_non_alphanumeric': 0.10822342901474011, 'fraction_numerical': 0.005624515128006206, 'mean_word_length': 2.596234309623431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265283', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1559, 'n_words': 315}