示例#1
0
 def setUp(self):
     self.chunk_size = 512
     self.mock_socket = unittest.mock.Mock()
     self.client_sock, self.server_sock = socket.socketpair()
     self.buf_sock = RecvBufferedSocket(self.client_sock,
                                        chunk_size=self.chunk_size)
     self.mocked_buf_sock = RecvBufferedSocket(self.mock_socket,
                                               chunk_size=self.chunk_size)
示例#2
0
    def __init__(self,
                 server_address,
                 runner_factory,
                 context_lock,
                 handler_class=None,
                 bind_and_activate=True):
        """Override of TCPServer.__init__().

    N.B. the majority of this function is copied verbatim from TCPServer.__init__().

    :param tuple server_address: An address tuple of (hostname, port) for socket.bind().
    :param class runner_factory: A factory function for creating a DaemonPantsRunner for each run.
    :param func context_lock: A contextmgr that will be used as a lock during request handling/forking.
    :param class handler_class: The request handler class to use for each request. (Optional)
    :param bool bind_and_activate: If True, binds and activates networking at __init__ time.
                                   (Optional)
    """
        # Old-style class, so we must invoke __init__() this way.
        BaseServer.__init__(self, server_address, handler_class
                            or PailgunHandler)
        self.socket = RecvBufferedSocket(
            socket.socket(self.address_family, self.socket_type))
        self.runner_factory = runner_factory
        self.allow_reuse_address = True  # Allow quick reuse of TCP_WAIT sockets.
        self.server_port = None  # Set during server_bind() once the port is bound.
        self._context_lock = context_lock

        if bind_and_activate:
            try:
                self.server_bind()
                self.server_activate()
            except Exception:
                self.server_close()
                raise
示例#3
0
  def __init__(self, server_address, runner_factory, lifecycle_lock, request_complete_callback,
               handler_class=None, bind_and_activate=True):
    """Override of TCPServer.__init__().

    N.B. the majority of this function is copied verbatim from TCPServer.__init__().

    :param tuple server_address: An address tuple of (hostname, port) for socket.bind().
    :param class runner_factory: A factory function for creating a DaemonPantsRunner for each run.
    :param threading.RLock lifecycle_lock: A lock used to guard against abrupt teardown of the servers
                                           execution thread during handling. All pailgun request handling
                                           will take place under care of this lock, which would be shared with
                                           a `PailgunServer`-external lifecycle manager to guard teardown.
    :param function request_complete_callback: A callback that will be called whenever a pailgun request is completed.
    :param class handler_class: The request handler class to use for each request. (Optional)
    :param bool bind_and_activate: If True, binds and activates networking at __init__ time.
                                   (Optional)
    """
    # Old-style class, so we must invoke __init__() this way.
    BaseServer.__init__(self, server_address, handler_class or PailgunHandler)
    self.socket = RecvBufferedSocket(socket.socket(self.address_family, self.socket_type))
    self.runner_factory = runner_factory
    self.lifecycle_lock = lifecycle_lock
    self.allow_reuse_address = True           # Allow quick reuse of TCP_WAIT sockets.
    self.server_port = None                   # Set during server_bind() once the port is bound.
    self.request_complete_callback = request_complete_callback

    if bind_and_activate:
      try:
        self.server_bind()
        self.server_activate()
      except Exception:
        self.server_close()
        raise
示例#4
0
  def try_connect(self):
    """Creates a socket, connects it to the nailgun and returns the connected socket.

    :returns: a connected `socket.socket`.
    :raises: `NailgunClient.NailgunConnectionError` on failure to connect.
    """
    sock = RecvBufferedSocket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    try:
      sock.connect((self._host, self._port))
    except (socket.error, socket.gaierror) as e:
      logger.debug('Encountered socket exception {!r} when attempting connect to nailgun'.format(e))
      sock.close()
      raise self.NailgunConnectionError(
        'Problem connecting to nailgun server at {}:{}: {!r}'.format(self._host, self._port, e))
    else:
      return sock
示例#5
0
  def try_connect(self):
    """Creates a socket, connects it to the nailgun and returns the connected socket.

    :returns: a connected `socket.socket`.
    :raises: `NailgunClient.NailgunConnectionError` on failure to connect.
    """
    sock = RecvBufferedSocket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    try:
      sock.connect(self._address)
    except (socket.error, socket.gaierror) as e:
      logger.debug('Encountered socket exception {!r} when attempting connect to nailgun'.format(e))
      sock.close()
      raise self.NailgunConnectionError(
        address=self._address_string,
        pid=self.pid,
        wrapped_exc=e,
        traceback=sys.exc_info()[2]
      )
    else:
      return sock
示例#6
0
    def try_connect(self):
        """Creates a socket, connects it to the nailgun and returns the connected socket.

        :returns: a connected `socket.socket`.
        :raises: `NailgunClient.NailgunConnectionError` on failure to connect.
        """
        sock = RecvBufferedSocket(
            sock=socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM))
        try:
            sock.connect(self._address)
        except (socket.error, socket.gaierror) as e:
            logger.debug(
                "Encountered socket exception {!r} when attempting connect to nailgun"
                .format(e))
            sock.close()
            raise self.NailgunConnectionError(
                address=self._address_string,
                pid=self._maybe_last_pid(),
                pgrp=self._maybe_last_pgrp(),
                wrapped_exc=e,
            )
        else:
            return sock