예제 #1
0
 def __init__(self, host, port, db, password=None, encoding=None):
     self.host = host
     self.port = port
     self.db = db
     if encoding:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             encoding=encoding,
             decode_responses=True,
             encoding_errors="replace",
         )
     else:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             decode_responses=False,
         )
     # all command upper case
     self.answer_callbacks = command2callback
     self.callbacks = self.reder_funcname_mapping()
예제 #2
0
 def test_retry_on_timeout_retry(self, retries):
     retry_on_timeout = retries > 0
     c = Connection(retry_on_timeout=retry_on_timeout,
                    retry=Retry(NoBackoff(), retries))
     assert c.retry_on_timeout == retry_on_timeout
     assert isinstance(c.retry, Retry)
     assert c.retry._retries == retries
예제 #3
0
    def test_close_connection_in_child(self):
        """
        A connection owned by a parent and closed by a child doesn't
        destroy the file descriptors so a parent can still use it.
        """
        conn = Connection()
        conn.send_command('ping')
        assert conn.read_response() == b'PONG'

        def target(conn):
            conn.send_command('ping')
            assert conn.read_response() == b'PONG'
            conn.disconnect()

        proc = multiprocessing.Process(target=target, args=(conn, ))
        proc.start()
        proc.join(3)
        assert proc.exitcode is 0

        # The connection was created in the parent but disconnected in the
        # child. The child called socket.close() but did not call
        # socket.shutdown() because it wasn't the "owning" process.
        # Therefore the connection still works in the parent.
        conn.send_command('ping')
        assert conn.read_response() == b'PONG'
예제 #4
0
    def test_close_connection_in_parent(self):
        """
        A connection owned by a parent is unusable by a child if the parent
        (the owning process) closes the connection.
        """
        conn = Connection()
        conn.send_command('ping')
        assert conn.read_response() == b'PONG'

        def target(conn, ev):
            ev.wait()
            # the parent closed the connection. because it also created the
            # connection, the connection is shutdown and the child
            # cannot use it.
            with pytest.raises(ConnectionError):
                conn.send_command('ping')

        ev = multiprocessing.Event()
        proc = multiprocessing.Process(target=target, args=(conn, ev))
        proc.start()

        conn.disconnect()
        ev.set()

        proc.join(3)
        assert proc.exitcode is 0
 def test_disconnect(self):
     conn = Connection()
     mock_sock = mock.Mock()
     conn._sock = mock_sock
     conn.disconnect()
     mock_sock.shutdown.assert_called_once()
     mock_sock.close.assert_called_once()
     assert conn._sock is None
예제 #6
0
 def test_memoryviews_are_not_packed(self):
     c = Connection()
     arg = memoryview(b"some_arg")
     arg_list = ["SOME_COMMAND", arg]
     cmd = c.pack_command(*arg_list)
     assert cmd[1] is arg
     cmds = c.pack_commands([arg_list, arg_list])
     assert cmds[1] is arg
     assert cmds[3] is arg
예제 #7
0
 def test_connect_without_retry_on_os_error(self):
     """Test that the _connect function is not being retried in case of a OSError"""
     with patch.object(Connection, "_connect") as _connect:
         _connect.side_effect = OSError("")
         conn = Connection(retry_on_timeout=True,
                           retry=Retry(NoBackoff(), 2))
         with pytest.raises(ConnectionError):
             conn.connect()
         assert _connect.call_count == 1
         self.clear(conn)
 def test_disconnect__close_OSError(self):
     """An OSError on socket close will still clear out the socket."""
     conn = Connection()
     mock_sock = mock.Mock()
     conn._sock = mock_sock
     conn._sock.close.side_effect = OSError
     conn.disconnect()
     mock_sock.shutdown.assert_called_once()
     mock_sock.close.assert_called_once()
     assert conn._sock is None
예제 #9
0
    def to_blocking_connection(self, socket_read_size=65536):
        """ Convert asynchronous connection to blocking socket connection
        """
        conn = Connection(
            self.host, self.port, self.db, self.password, self.socket_timeout,
            self.socket_connect_timeout, self.socket_keepalive,
            self.socket_keepalive_options, self.retry_on_timeout,
            parser_class=DefaultParser, socket_read_size=socket_read_size)

        conn._sock = self._stream.socket
        return conn
예제 #10
0
    def test_connect_timeout_error_without_retry(self):
        """Test that the _connect function is not being retried if retry_on_timeout is
        set to False"""
        conn = Connection(retry_on_timeout=False)
        conn._connect = mock.Mock()
        conn._connect.side_effect = socket.timeout

        with pytest.raises(TimeoutError) as e:
            conn.connect()
        assert conn._connect.call_count == 1
        assert str(e.value) == "Timeout connecting to server"
        self.clear(conn)
예제 #11
0
 def __init__(self,
              host,
              port,
              db,
              password=None,
              encoding=None,
              get_info=True):
     self.host = host
     self.port = port
     self.db = db
     if encoding:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             encoding=encoding,
             decode_responses=True,
             encoding_errors="replace",
         )
     else:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             decode_responses=False,
         )
     # all command upper case
     self.answer_callbacks = command2callback
     self.callbacks = self.reder_funcname_mapping()
     self.connection.connect()
     if get_info:
         try:
             self.get_server_info()
         except Exception as e:
             logger.warn(f"[After Connection] {str(e)}")
             config.no_version_reason = str(e)
     else:
         config.no_version_reason = "--no-info flag activated"
예제 #12
0
파일: client.py 프로젝트: crhan/iredis
 def __init__(self, host, port, db, password=None):
     self.host = host
     self.port = port
     self.db = db
     if config.decode:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             encoding=config.decode,
             decode_responses=True,
             encoding_errors="replace",
             socket_keepalive=config.socket_keepalive,
         )
     else:
         self.connection = Connection(
             host=self.host,
             port=self.port,
             db=self.db,
             password=password,
             decode_responses=False,
             socket_keepalive=config.socket_keepalive,
         )
     # all command upper case
     self.answer_callbacks = command2callback
     self.callbacks = self.reder_funcname_mapping()
     try:
         self.connection.connect()
     except Exception as e:
         print(str(e), file=sys.stderr)
     if not config.no_info:
         try:
             self.get_server_info()
         except Exception as e:
             logger.warning(f"[After Connection] {str(e)}")
             config.no_version_reason = str(e)
     else:
         config.no_version_reason = "--no-info flag activated"
예제 #13
0
    def test_retry_connect_on_timeout_error(self):
        """Test that the _connect function is retried in case of a timeout"""
        conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 3))
        origin_connect = conn._connect
        conn._connect = mock.Mock()

        def mock_connect():
            # connect only on the last retry
            if conn._connect.call_count <= 2:
                raise socket.timeout
            else:
                return origin_connect()

        conn._connect.side_effect = mock_connect
        conn.connect()
        assert conn._connect.call_count == 3
        self.clear(conn)
예제 #14
0
    def handle(self, *args, **options):
        owner_username = args[0]
        audience_name = args[1]
        filename = os.path.abspath(args[2])
        redis_key = Profiles.ip_audiences_key
        # the connection is not necessary, but the object is used to
        # format redis commands to redis protocol using pack_command
        redis_connection = Connection(host=settings.profiles_redis_host,
                                      port=settings.profiles_redis_port)
        user = User.objects.get(username=owner_username)
        audience, _ = Audience.objects.get_or_create(name=audience_name,
                                                     owner=user.account,
                                                     is_ip=True)
        audience_id = audience.public_id

        with open(filename) as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                ip = IPAddress(row[0]).packed
                sys.stdout.write(
                    redis_connection.pack_command("SADD", redis_key.format(ip),
                                                  audience_id))
예제 #15
0
 def test_retry_on_timeout_boolean(self, retry_on_timeout):
     c = Connection(retry_on_timeout=retry_on_timeout)
     assert c.retry_on_timeout == retry_on_timeout
     assert isinstance(c.retry, Retry)
     assert c.retry._retries == (1 if retry_on_timeout else 0)
 def tearDown(self):
     self._redis.redis.flushdb()
     Connection(self._redis.redis).disconnect()
     super(TestRedisListenStore, self).tearDown()
예제 #17
0
 def __init__(self):
     self.conn = Connection()