示例#1
0
    def test_should_not_block_on_many_writes_if_no_input_being_received_by_the_process(
            self):
        process = Process("./tests/models/output-once.sh")
        process.start()

        for _ in range(100000):
            process.write({'aaa': 'bbb'})
示例#2
0
    def test_should_read_json_document(self):
        process = Process("./tests/models/output-once.sh")
        process.start()

        while process.read() != {'key1': 'value1', 'key2': 2}:
            time.sleep(0.1)

        process.stop()
示例#3
0
    def test_should_set_running_to_true_immediately_after_start(self):
        process = Process("./tests/models/output-echo.sh")
        process.start()

        assert process.running
        process.stop()
        while process.running:
            time.sleep(0.1)
示例#4
0
    def test_should_read_long_json_documents(self):
        process = Process("./tests/models/output-long.sh")
        process.start()

        time.sleep(1)

        doc = process.read()

        assert doc is not None
        assert len(doc) == 65
示例#5
0
    def test_should_kill_process_on_stop(self):
        process = Process("./tests/models/output-echo.sh")

        process.start()
        while not process.running:
            time.sleep(0.1)

        process.stop()
        while process.running:
            time.sleep(0.1)
示例#6
0
    def test_should_not_let_start_the_process_twice(self):
        process = Process("./tests/models/output-echo.sh")
        process.start()

        with pytest.raises(Exception):
            process.start()

        process.stop()
        while process.running:
            time.sleep(0.1)
示例#7
0
    def test_should_let_start_the_process_again_after_it_terminated_voluntarily(
            self):
        process = Process("./tests/models/terminate-voluntarily.sh")

        process.start()
        while process.running:
            time.sleep(0.1)

        process.start()
        while not process.running:
            time.sleep(0.1)
示例#8
0
    def test_should_read_and_write(self):
        process = Process("./tests/models/output-echo.sh")
        process.start()

        time.sleep(1)
        assert process.read() is None

        for value in ['value1', 'value2', 'value3']:
            process.write({'key': value})
            while process.read() != {'key': value}:
                time.sleep(0.1)

        process.stop()
示例#9
0
    def test_should_terminate_process_on_broken_input_pipe(self):
        process = Process("./tests/models/break-pipe.sh")
        process.start()

        # so the process has some time to break the pipe
        time.sleep(1)

        for _ in range(10):
            process.write({'aaa': 'bbb'})

        time.sleep(5)

        assert process.running is False
示例#10
0
    def test_should_skip_invalid_json_documents(self):
        process = Process("./tests/models/output-invalid.sh")
        process.start()

        while process.read() != {'key': 'value1'}:
            time.sleep(0.1)

        while process.read() != {'key': 'value2'}:
            time.sleep(0.1)

        while process.read() != {'key': 'value3'}:
            time.sleep(0.1)

        process.stop()
示例#11
0
    def test_should_let_start_the_process_again_after_it_got_stopped(self):
        process = Process("./tests/models/output-echo.sh")

        process.start()
        while not process.running:
            time.sleep(0.1)

        process.stop()
        while process.running:
            time.sleep(0.1)

        process.start()
        while not process.running:
            time.sleep(0.1)
示例#12
0
    def test_should_read_multiple_json_documents(self):
        process = Process("./tests/models/output-multiple.sh")
        process.start()

        while process.read() != {'key': 'value1'}:
            time.sleep(0.1)

        while process.read() != {'key': 'value2'}:
            time.sleep(0.1)

        while process.read() != {'key': 'value3'}:
            time.sleep(0.1)

        process.stop()
        while process.running:
            time.sleep(0.1)
示例#13
0
class Model:
    logger = logging.getLogger()

    def __init__(self, command: str, parameters: Parameters):
        assert isinstance(command, str)
        assert isinstance(parameters, Parameters)

        self._command = command
        self._arguments = f"--id {parameters.id}"
        self._arguments += f" --flipper {parameters.flipper}" if parameters.flipper is not None else ""
        self._arguments += f" --flapper {parameters.flapper}" if parameters.flapper is not None else ""
        self._arguments += f" --flopper {parameters.flopper}" if parameters.flopper is not None else ""
        self._last_output = None

        self.logger.info(
            f"Instantiated model using process '{self._command} {self._arguments}'"
        )

        self._process = Process(f"{self._command} {self._arguments}")
        self._process.start()

    def _ensure_process_running(self):
        if not self._process.running:
            self.logger.warning(
                f"Process '{self._command} {self._arguments}' is down, restarting it"
            )

            self._process.start()

    def send_status(self, input: Status):
        assert isinstance(input, Status)

        self._ensure_process_running()

        record = {
            "id": str(input.id),
            "bid": str(input.bid),
            "lot": str(input.lot),
            "beg": str(input.beg),
            "guy": str(input.guy),
            "era": int(input.era),
            "tic": int(input.tic),
            "end": int(input.end),
            "price": str(input.price) if input.price is not None else None,
        }

        if input.tab:
            record['tab'] = str(input.tab)

        if input.flipper:
            record['flipper'] = str(input.flipper)

        if input.flapper:
            record['flapper'] = str(input.flapper)

        if input.flopper:
            record['flopper'] = str(input.flopper)

        self._process.write(record)

    def get_stance(self) -> Optional[Stance]:
        self._ensure_process_running()

        while True:
            data = self._process.read()

            if data is not None:
                self._last_output = Stance(price=Wad.from_number(
                    data['price']),
                                           gas_price=int(data['gasPrice'])
                                           if 'gasPrice' in data else None)

            else:
                break

        return self._last_output

    def terminate(self):
        self.logger.info(
            f"Terminating model using process '{self._command} {self._arguments}'"
        )

        self._process.stop()
示例#14
0
    def test_should_return_none_from_read_if_nothing_to_read(self):
        process = Process("./tests/models/terminate-voluntarily.sh")

        process.start()
        while process.running:
            assert process.read() is None
示例#15
0
    def test_should_not_let_start_the_process_twice(self):
        process = Process("./tests/models/output-echo.sh")
        process.start()

        with pytest.raises(Exception):
            process.start()
示例#16
0
class Model:
    logger = logging.getLogger()

    def __init__(self, command: str, parameters: Parameters):
        assert isinstance(command, str)
        assert isinstance(parameters, Parameters)

        self._command = command
        self._arguments = f"--id {parameters.id}"
        self._arguments += f" --collateral_auction_house {parameters.collateral_auction_house}" if parameters.collateral_auction_house is not None else ""
        self._arguments += f" --surplus_auction_house {parameters.surplus_auction_house}" if parameters.surplus_auction_house is not None else ""
        self._arguments += f" --debt_auction_house {parameters.debt_auction_house}" if parameters.debt_auction_house is not None else ""
        self._arguments += f" --staked_token_auction_house {parameters.staked_token_auction_house}" if parameters.staked_token_auction_house is not None else ""
        self._last_output = None

        self.logger.info(
            f"Instantiated model using process '{self._command} {self._arguments}'"
        )

        self._process = Process(f"{self._command} {self._arguments}")
        self._process.start()

    def _ensure_process_running(self):
        if not self._process.running:
            self.logger.warning(
                f"Process '{self._command} {self._arguments}' is down, restarting it"
            )

            self._process.start()

    def send_status(self, input: Status):
        assert isinstance(input, Status)

        self._ensure_process_running()

        record = input.to_dict()

        self._process.write(record)

    def get_stance(self) -> Optional[Stance]:
        self._ensure_process_running()

        while True:
            data = self._process.read()

            if data is not None:
                self._last_output = Stance(price=Wad.from_number(data['price'])
                                           if 'price' in data else None,
                                           gas_price=int(data['gasPrice'])
                                           if 'gasPrice' in data else None)

            else:
                break

        return self._last_output

    def terminate(self):
        self.logger.info(
            f"Terminating model using process '{self._command} {self._arguments}'"
        )

        self._process.stop()