def stock_reply_for_stock_request(stock_request): control.control() if active(): return stock_pb2.StockReply(symbol=stock_request.symbol, price=_price(stock_request.symbol)) else: raise abandonment.Abandoned()
def _get_last_trade_price(stock_request, stock_reply_callback, control, active): """A unary-request, unary-response test method.""" control.control() if active(): stock_reply_callback( stock_pb2.StockReply( symbol=stock_request.symbol, price=_price(stock_request.symbol))) else: raise abandonment.Abandoned()
def terminate(self): control.control() if active(): if self._symbol is None: raise ValueError() else: stock_reply_callback( stock_pb2.StockReply(symbol=self._symbol, price=self._price)) self._symbol = None self._price = None
def _watch_future_trades(stock_request, stock_reply_consumer, control, active): """A unary-request, stream-response test method.""" base_price = _price(stock_request.symbol) for index in range(stock_request.num_trades_to_watch): control.control() if active(): stock_reply_consumer.consume( stock_pb2.StockReply(symbol=stock_request.symbol, price=base_price + index)) else: raise abandonment.Abandoned() stock_reply_consumer.terminate()
def consume_and_terminate(self, stock_request): control.control() if active(): if self._price is None: stock_reply_callback( stock_pb2.StockReply(symbol=stock_request.symbol, price=_price( stock_request.symbol))) else: candidate_price = _price(stock_request.symbol) if self._price < candidate_price: stock_reply_callback( stock_pb2.StockReply(symbol=stock_request.symbol, price=candidate_price)) else: stock_reply_callback( stock_pb2.StockReply(symbol=self._symbol, price=self._price)) self._symbol = None self._price = None