Exemplo n.º 1
0
    def start(self):
        """
        Start the service.  The caller is responsible for calling `stop()`.

        It chooses a random local port to start the service on,
        and will retry several times if it gets an address already in use
        error.  If it cannot find an open local port after a certain
        number of trieds, it raises a `SrcInstrumenterError`.
        """

        if self._jscover is None:

            try:
                self._port_num, self._jscover = retry(
                    self._start_jscover,
                    self.MAX_START_ATTEMPTS,
                    self.WAIT_BETWEEN_ATTEMPTS,
                    fail_fast_errors=[OSError],
                    name="Start JSCover"
                )
            except OSError:
                msg = "Could not find JSCover JAR file at '{}'".format(self._tool_path)
                raise SrcInstrumenterError(msg)

            except SrcInstrumenterError:
                msg = "Could not start JSCover, most likely due to port conflicts."
                raise SrcInstrumenterError(msg)
Exemplo n.º 2
0
    def instrumented_src(self, rel_path):
        """
        Return an instrumented version of the JavaScript source
        file at `rel_path`, interpreted relative to the
        root URL (configured in the constructor).

        Raises a `SrcInstrumenterError` is the service hasn't been
        started or the source could not be retrieved.
        """

        # If have not started the service yet, do so now.
        if self._jscover is None:
            raise SrcInstrumenterError("You need to start the JSCover server first.")

        # Get the instrumented version of the source from JSCover
        try:
            return retry(
                lambda: self._get_src_from_jscover(rel_path),
                self.MAX_CONNECT_ATTEMPTS,
                self.WAIT_BETWEEN_ATTEMPTS,
                recover_func=self.start,
                num_attempts_before_recover=2,
                name="Get source from JSCover"
            )

        except requests.exceptions.ConnectionError:
            raise SrcInstrumenterError("Could not connect to JSCover server.")