def test_market_participants_market_bad_seller_argument(): market_list = MarketList() market_name = 'test_market' with pytest.raises(ValueError) as error_info: bad_participant = MarketParticipant('bob is cool', 'agent_id') market_list.make_reservation(market_name, bad_participant) assert 'bob is cool' in error_info.value.message
def test_market_participants_market_bad_seller_argument(): market_list = MarketList() market_name = 'test_market' with pytest.raises(ValueError) as error_info: bad_participant = MarketParticipant('bob is cool', 'agent_id') market_list.make_reservation(market_name, bad_participant) assert 'bob is cool' in error_info.value.args[0]
def test_market_unformed_market_list(): market_list = MarketList() market_name1 = 'test_market1' market_name2 = 'test_market2' buyer_participant = MarketParticipant(BUYER, 'agent_id1') market_list.make_reservation(market_name1, buyer_participant) seller_participant = MarketParticipant(SELLER, 'agent_id2') market_list.make_reservation(market_name2, seller_participant) assert market_list.market_count() == 2 unformed_markets = market_list.unformed_market_list() assert len(unformed_markets) > 0
def test_market_participants_market_formed_one_buyer_one_seller(): market_list = MarketList() market_name = 'test_market' buyer_participant = MarketParticipant(BUYER, 'agent_id1') market_list.make_reservation(market_name, buyer_participant) seller_participant = MarketParticipant(SELLER, 'agent_id2') market_list.make_reservation(market_name, seller_participant) assert market_list.market_count() == 1 assert market_list.has_market_formed(market_name) == True unformed_markets = market_list.unformed_market_list() assert len(unformed_markets) == 0
def test_market_participants_market_not_formed_one_buyer(): market_list = MarketList() market_name = 'test_market' buyer_participant = MarketParticipant(BUYER, 'agent_id') market_list.make_reservation(market_name, buyer_participant) assert market_list.has_market_formed(market_name) == False
def test_market_participants_has_market(): market_list = MarketList() market_name = 'test_market' seller_participant = MarketParticipant(SELLER, 'agent_id') market_list.make_reservation(market_name, seller_participant) assert market_list.has_market(market_name) == True
class MarketServiceAgent(Agent): states = [INITIAL_WAIT, COLLECT_RESERVATIONS, COLLECT_OFFERS, NO_MARKETS] transitions = [ {'trigger': 'start_reservations', 'source': INITIAL_WAIT, 'dest': COLLECT_RESERVATIONS}, {'trigger': 'start_offers_no_markets', 'source': COLLECT_RESERVATIONS, 'dest': NO_MARKETS}, {'trigger': 'start_offers_has_markets', 'source': COLLECT_RESERVATIONS, 'dest': COLLECT_OFFERS}, {'trigger': 'start_reservations', 'source': COLLECT_OFFERS, 'dest': COLLECT_RESERVATIONS}, {'trigger': 'start_reservations', 'source': NO_MARKETS, 'dest': COLLECT_RESERVATIONS}, ] def __init__(self, market_period=300, reservation_delay=0, offer_delay=120, verbose_logging = True, **kwargs): super(MarketServiceAgent, self).__init__(**kwargs) _log.debug("vip_identity: {}".format(self.core.identity)) _log.debug("market_period: {}".format(market_period)) _log.debug("reservation_delay: {}".format(reservation_delay)) _log.debug("offer_delay: {}".format(offer_delay)) _log.debug("verbose_logging: {}".format(verbose_logging)) self.state_machine = Machine(model=self, states=MarketServiceAgent.states, transitions= MarketServiceAgent.transitions, initial=INITIAL_WAIT) self.market_list = None self.verbose_logging = verbose_logging self.director = Director(market_period, reservation_delay, offer_delay) @Core.receiver("onstart") def onstart(self, sender, **kwargs): self.market_list = MarketList(self.vip.pubsub.publish, self.verbose_logging) self.director.start(self) def send_collect_reservations_request(self, timestamp): _log.debug("send_collect_reservations_request at {}".format(timestamp)) self.start_reservations() self.market_list.send_market_failure_errors() self.market_list.clear_reservations() self.vip.pubsub.publish(peer='pubsub', topic=MARKET_RESERVE, message=utils.format_timestamp(timestamp)) def send_collect_offers_request(self, timestamp): if (self.has_any_markets()): self.begin_collect_offers(timestamp) else: self.start_offers_no_markets() def begin_collect_offers(self, timestamp): _log.debug("send_collect_offers_request at {}".format(timestamp)) self.start_offers_has_markets() self.market_list.collect_offers() unformed_markets = self.market_list.unformed_market_list() self.vip.pubsub.publish(peer='pubsub', topic=MARKET_BID, message=[utils.format_timestamp(timestamp), unformed_markets]) @RPC.export def make_reservation(self, market_name, buyer_seller): identity = bytes(self.vip.rpc.context.vip_message.peer) log_message = "Received {} reservation for market {} from agent {}".format(buyer_seller, market_name, identity) _log.debug(log_message) if (self.state == COLLECT_RESERVATIONS): self.accept_reservation(buyer_seller, identity, market_name) else: self.reject_reservation(buyer_seller, identity, market_name) def accept_reservation(self, buyer_seller, identity, market_name): _log.info("Reservation on Market: {} {} made by {} was accepted.".format(market_name, buyer_seller, identity)) participant = MarketParticipant(buyer_seller, identity) self.market_list.make_reservation(market_name, participant) def reject_reservation(self, buyer_seller, identity, market_name): _log.info("Reservation on Market: {} {} made by {} was rejected.".format(market_name, buyer_seller, identity)) raise RuntimeError("Error: Market service not accepting reservations at this time.") @RPC.export def make_offer(self, market_name, buyer_seller, offer): identity = bytes(self.vip.rpc.context.vip_message.peer) log_message = "Received {} offer for market {} from agent {}".format(buyer_seller, market_name, identity) _log.debug(log_message) if (self.state == COLLECT_OFFERS): self.accept_offer(buyer_seller, identity, market_name, offer) else: self.reject_offer(buyer_seller, identity, market_name, offer) def accept_offer(self, buyer_seller, identity, market_name, offer): _log.info("Offer on Market: {} {} made by {} was accepted.".format(market_name, buyer_seller, identity)) participant = MarketParticipant(buyer_seller, identity) curve = PolyLineFactory.fromTupples(offer) self.market_list.make_offer(market_name, participant, curve) def reject_offer(self, buyer_seller, identity, market_name, offer): _log.info("Offer on Market: {} {} made by {} was rejected.".format(market_name, buyer_seller, identity)) raise RuntimeError("Error: Market service not accepting offers at this time.") def has_any_markets(self): unformed_markets = self.market_list.unformed_market_list() return len(unformed_markets) < self.market_list.market_count()
def test_market_participants_market_not_formed_one_seller(): market_list = MarketList() market_name = 'test_market' seller_participant = MarketParticipant(SELLER, 'agent_id') market_list.make_reservation(market_name, seller_participant) assert market_list.has_market_formed(market_name) == False
def test_market_participants_market_not_formed_one_seller(): market_list = MarketList() market_name = 'test_market' seller_participant = MarketParticipant(SELLER, 'agent_id') market_list.make_reservation(market_name, seller_participant) assert market_list.has_market_formed(market_name) is False