Exemplo n.º 1
0
class TestServerProtocol(unittest.TestCase):
    def setUp(self) -> None:
        self.factory = ClientProtocolFactory()
        self.addr = IPv4Address('TCP', '127.0.0.1', 50000)
        self.tr = StringTransport()

        self.input = ''
        self.output = ''

        def fake_input(s):
            return self.input

        def fake_print(s):
            self.output = s

        self.fake_input = fake_input
        self.fake_print = fake_print

    def test_send_receive(self):
        """
        Test sending and receiving data
        """
        with patch('builtins.input', self.fake_input):
            with patch('builtins.print', self.fake_print):
                self.proto = self.factory.buildProtocol(self.addr)

                self.input = 'set 1 one'
                self.proto.makeConnection(self.tr)
                command = RedisEncoder.encodeArray(['set', '1', 'one'])
                self.assertEqual(command, self.tr.value())
                self.tr.clear()

                self.input = 'get 1'
                self.proto.dataReceived(b'+OK\r\n')
                self.assertEqual('OK', self.output)
                command = RedisEncoder.encodeArray(['get', '1'])
                self.assertEqual(command, self.tr.value())

    def test_exit(self):
        """
        Test disconnecting on 'exit' prompt
        """
        with patch('builtins.input', self.fake_input):
            with patch('builtins.print', self.fake_print):
                self.proto = self.factory.buildProtocol(self.addr)

                self.input = 'exit'
                self.proto.makeConnection(self.tr)
                self.assertEqual(True, self.tr.disconnecting)

    def test_disconnect_on_garbage(self):
        """
        Test disconnecting when garbage received (no first bit in data)
        """
        with patch('builtins.input', self.fake_input):
            with patch('builtins.print', self.fake_print):
                self.proto = self.factory.buildProtocol(self.addr)

                self.input = 'set 1 one'
                self.proto.makeConnection(self.tr)
                command = RedisEncoder.encodeArray(['set', '1', 'one'])
                self.assertEqual(command, self.tr.value())
                self.tr.clear()

                self.proto.dataReceived(b'OK\r\n')
                self.assertEqual(True, self.tr.disconnecting)
Exemplo n.º 2
0
class TestServerProtocol(unittest.TestCase):
    def setUp(self) -> None:
        self.factory = ServerProtocolFactory()
        self.proto = self.factory.buildProtocol(('127.0.0.1', 6379))
        self.tr = StringTransport()
        self.proto.makeConnection(self.tr)
        self.output = ''

        def fake_print(s):
            self.output = s

        self.fake_print = fake_print

    def test_connection_lost(self):
        factory = ServerProtocolFactory()
        proto = factory.buildProtocol(('127.0.0.1', 6379))
        tr = StringTransportWithDisconnection()
        tr.protocol = proto
        proto.makeConnection(self.tr)
        tr.loseConnection()
        self.assertEqual(0, factory.proto_count)

    def test_set_get(self):
        """
        Set and then get key
        :return:
        """
        data = RedisEncoder.encodeArray(['set', '1', 'one'])
        self.proto.dataReceived(data)
        self.assertEqual(b'+OK\r\n', self.tr.value())
        self.tr.clear()
        data = RedisEncoder.encodeArray(['get', '1'])
        self.proto.dataReceived(data)
        self.assertEqual(b'$3\r\none\r\n', self.tr.value())
        self.tr.clear()

    def test_get_None(self):
        """
        Get key that is not set
        :return:
        """
        data = RedisEncoder.encodeArray(['get', 'nonset'])
        self.proto.dataReceived(data)
        self.assertEqual(b'$-1\r\n', self.tr.value())
        self.tr.clear()

    def test_lrange_None(self):
        """
        Get array that is not set
        :return:
        """
        data = RedisEncoder.encodeArray(['lrange', 'nonset', '0', '1'])
        self.proto.dataReceived(data)
        self.assertEqual(b'*-1\r\n', self.tr.value())
        self.tr.clear()

    def test_wrong_first_byte(self):
        """
        Receive data with wrong first byte,
        then receive normal data.
        :return:
        """
        with patch('builtins.print', self.fake_print):
            data = b'abc'
            self.proto.dataReceived(data)
            self.assertEqual(b'', self.tr.value())
            self.assertEqual(b'', self.proto._data_buffer)
            data = RedisEncoder.encodeArray(['set', '1', 'one'])
            self.proto.dataReceived(data)
            self.assertEqual(b'+OK\r\n', self.tr.value())
            self.tr.clear()