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()
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()
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)
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()
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
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()
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
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()