Ejemplo n.º 1
0
    def _get_response(self, future, expectation=None, format_callback=None):
        """Read and parse command execution responses from Redis

        :param future: The future for the possible response
        :type future: :class:`~tornado.concurrent.Future`
        :param mixed expectation: An optional response expectation

        """

        def on_data(data):
            LOGGER.debug('Read %r', data)
            self._reader.feed(data)
            self._get_response(future, expectation, format_callback)

        response = self._reader.gets()
        if response is not False:
            if isinstance(response, hiredis.ReplyError):
                future.set_exception(exceptions.RedisError(response))
            elif format_callback is not None:
                future.set_result(format_callback(response))
            elif expectation is not None:
                if isinstance(expectation, int) and expectation > 1:
                    future.set_result(response == expectation or response)
                else:
                    future.set_result(response == expectation)
            else:
                future.set_result(response)
        else:
            self._read(on_data)
Ejemplo n.º 2
0
    def _read(self, command, future):
        """Invoked when a command is executed to read and parse its results.
        It will loop on the IOLoop until the response is complete and then
        set the value of the response in the execution future.

        :param command: The command that was being executed
        :type command: tredis.client.Command
        :param future: The execution future
        :type future: tornado.concurrent.Future

        """
        response = self._reader.gets()
        if response is not False:
            if isinstance(response, hiredis.ReplyError):
                if response.args[0].startswith('MOVED '):
                    self._on_cluster_data_moved(response.args[0], command,
                                                future)
                elif response.args[0].startswith('READONLY '):
                    self._on_read_only_error(command, future)
                else:
                    future.set_exception(exceptions.RedisError(response))
            elif command.callback is not None:
                future.set_result(command.callback(response))
            elif command.expectation is not None:
                self._eval_expectation(command, response, future)
            else:
                future.set_result(response)
        else:

            def on_data(data):
                # LOGGER.debug('Read %r', data)
                self._reader.feed(data)
                self._read(command, future)

            command.connection.read(on_data)
Ejemplo n.º 3
0
        def on_selected(response):
            """Invoked when the default database is selected when connecting

            :param response: The connection response future
            :type response: :class:`~tornado.concurrent.Future`

            """
            exc = response.exception()
            if exc:
                future.set_exception(exceptions.RedisError(exc))
            else:
                future.set_result(response.result == b'OK')
Ejemplo n.º 4
0
 def test_zadd_with_error(self):
     key, score, value = self.uuid4(3)
     self._execute_result = exceptions.RedisError('Test Exception')
     with mock.patch.object(self.client, '_execute', self._execute):
         with self.assertRaises(exceptions.RedisError):
             yield self.client.zadd(key, score, value)
Ejemplo n.º 5
0
 def test_scan_with_error(self):
     key = self.uuid4()
     self._execute_result = exceptions.RedisError('Test Exception')
     with mock.patch.object(self.client, '_execute', self._execute):
         with self.assertRaises(exceptions.RedisError):
             yield self.client.scan(key, 0)
Ejemplo n.º 6
0
 def test_auth_raises_auth_error(self):
     self._execute_result = exceptions.RedisError(b'invalid password')
     with mock.patch.object(self.client, '_execute', self._execute):
         with self.assertRaises(exceptions.AuthError):
             yield self.client.auth('boom-goes-the-silver-nitrate')