def test_clear_alarms_by_topic(): s = Scheduler(real_time=False) a = TestAgent() s.add(a) msg1 = TestMessage(sender=a, receiver=a, topic='1') msg2 = TestMessage(sender=a, receiver=a, topic='2') msg3 = TestMessage(sender=a, receiver=a, topic='3') a.set_alarm(alarm_time=1, alarm_message=msg1, relative=True, ignore_alarm_if_idle=False) a.set_alarm(alarm_time=3, alarm_message=msg2, relative=True, ignore_alarm_if_idle=False) a.set_alarm(alarm_time=1, alarm_message=msg3, relative=True, ignore_alarm_if_idle=False) assert s.clock.alarm_time == [1, 3] assert s.clock.clients_to_wake_up == {1: {a.uuid}, 3: {a.uuid}} a.clear_alarms(receiver=a.uuid, topic='1') assert s.clock.alarm_time == [1, 3], s.clock.alarm_time a.clear_alarms(receiver=None, topic='3') assert s.clock.list_alarms(a.uuid) == [(3, [msg2])] assert s.clock.clients_to_wake_up == {3: {a.uuid}}
def test_clear_alarms(): s = Scheduler(real_time=False) a = TestAgent() b = TestAgent() s.add(a) s.add(b) # set alarms for a and b, then clear them alarm_msg = TestMessage(sender=a, receiver=a, topic="Alarm_b") alarm_msg_b = TestMessage(sender=b, receiver=b, topic="Alarm_b") a.set_alarm(alarm_time=1, alarm_message=alarm_msg, relative=True, ignore_alarm_if_idle=False) # set for a by a a.set_alarm(alarm_time=1, alarm_message=alarm_msg_b, relative=True, ignore_alarm_if_idle=False) # set for b by a b.set_alarm(alarm_time=2, alarm_message=alarm_msg_b, relative=True, ignore_alarm_if_idle=False) # set for b by b assert s.clock.alarm_time == [1, 2] # alarms set at 1 and 2 assert a.list_alarms() == [(1, [alarm_msg])] assert b.list_alarms() == [(1, [alarm_msg_b]), (2, [alarm_msg_b])] b.clear_alarms() assert s.clock.alarm_time == [1] # only the alarm for a at 1 remains a.clear_alarms(receiver=b.uuid) assert s.clock.alarm_time == [1] # the alarm for a at 1 still remains b.clear_alarms(receiver=a.uuid) assert s.clock.alarm_time == []
def test_basic_message_abuse(): s = Scheduler() a = Agent(uuid=1) b = Agent(uuid=2) c = Agent(uuid=3) for i in [a, b, c]: s.add(i) i.subscribe('test')
def demo(sellers=None, buyers=None, time_limit=True, seller_can_initialise=True, buyer_can_initialise=True): """ This demo() function was the first piece of code written in this python script. The simplicity of the demo forced the dev process to target a linear execution model. """ if sellers is None: sellers = [] if buyers is None: buyers = [] s = Scheduler() for uuid in sellers: agent = Seller(uuid, prices=seller_data[uuid], send_advert_at_setup=seller_can_initialise) s.add(agent) for uuid in buyers: agent = Buyer(uuid, max_price=buyer_data[uuid], send_rfq_at_setup=buyer_can_initialise) s.add(agent) if time_limit: s.run(seconds=1) else: s.run(pause_if_idle=True) contracts = {} for uuid, agent in s.agents.items(): if not isinstance(agent, (Buyer, Seller)): continue if agent.in_contract_with() is False: contracts[agent.uuid] = None else: contracts[agent.uuid] = agent.in_contract_with() print("", flush=True) print("Contracts at end:") for k, v in contracts.items(): print("{}: {}".format(k, v), flush=True) assert len(contracts) > 0, "no contracts? That can't be true" return contracts
def test_subscribe_and_unsubscribe(): a, b, c, d = Agent(), Agent(), Agent(), Agent() s = Scheduler() for i in (a, b, c, d): s.add(i) a.subscribe(b.uuid, 'fish') a.subscribe(c.uuid, 'fish') # We do not: a.subscribe(d, 'fish') a.subscribe(topic='quantum physics') c.subscribe(target=a.uuid) assert a.get_subscriptions() == { # topic, target_set None: {a.uuid}, 'Agent': {None}, 'fish': {b.uuid, c.uuid}, 'quantum physics': {None} } assert b.get_subscriptions() == {None: {b.uuid}, 'Agent': {None}} assert c.get_subscriptions() == {None: {a.uuid, c.uuid}, 'Agent': {None}} c.unsubscribe(everything=True) d3 = c.get_subscriptions() assert not d3 # d3 is empty. c.subscribe(target=c.uuid) assert c.get_subscriptions() == {None: {c.uuid}} assert a.get_subscriptions() == { # check that a's subscriptions are # unaffect by c choosing to unsubscribe. None: {a.uuid}, 'Agent': {None}, 'fish': {b.uuid, c.uuid}, 'quantum physics': {None} }
def test_ping_pong_tests(): s = Scheduler() limit = 5000 player_a = PingPongPlayer(limit) player_b = PingPongPlayer(limit) s.add(player_a) s.add(player_b) player_a.serve(opponent=player_b) start = time.process_time() s.run() end = time.process_time() mps = (player_a.update_count + player_b.update_count) / (end - start) assert player_a.update_count == limit assert player_a.outcome == "won!" assert player_b.update_count == limit assert player_b.outcome != "won!" print(mps, "messages per second")
def tests_add_to_scheduler(): s = Scheduler() a = Agent() assert callable(a.setup) s.add(a) assert callable(a.teardown) s.remove(a) a = TestAgent() s.add(a) assert a.count_setups == 1 assert a.get_subscription_topics() == s.get_subscription_topics( ), "these should be the same" assert a.uuid in a.get_subscription_topics() assert a.__class__.__name__ in a.get_subscription_topics() a.unsubscribe(topic=a.__class__.__name__) assert a.uuid in a.get_subscription_topics() assert a.__class__.__name__ not in a.get_subscription_topics() assert a.uuid in a.get_subscriber_list(a.uuid) a.subscribe(topic=a.__class__.__name__) assert a.__class__.__name__ in a.get_subscription_topics() assert a.messages is False m = TestMessage(sender=a, receiver=a) assert m.sender == a.uuid assert m.receiver == a.uuid a.send(m) assert m in s.mail_queue s.process_mail_queue() assert m in a.inbox assert a.messages is True m2 = a.receive() assert m is m2 m3 = a.receive() assert m3 is None assert a.messages is False s.run() start = time.time() alarm_mesage = TestMessage(a, a) a.set_alarm(alarm_time=1000000000, alarm_message=alarm_mesage) s.run() end = time.time() assert end - start < 1, "scheduler didn't ignore the setting drop alarm if idle." assert a.count_updates == 1, a.count_updates assert a.count_setups == 1 assert a.count_teardowns == 0 # test the pause methods. s.run() assert a.count_updates == 1 # previous scheduler ended with setting alarm to update. s.run() assert a.count_updates == 1 # nothing has happened. # when the scheduler runs update with keep_awake == True, the agent will press "pause". a.keep_awake = True s.run() assert a.count_updates == 2 s.remove(a) assert a.count_teardowns == 1 s.add(a) assert a.count_setups == 2 b = TestAgent() a.add(b) assert b.uuid in s.agents assert b.uuid in a.get_subscription_topics() try: a.add(b) raise Exception("!") except SchedulerException: assert True a.remove(b.uuid) assert b.uuid not in s.agents assert b.uuid not in a.get_subscription_topics() a.add(b) s.run() assert len(s.needs_update) == 0 m4 = TestMessage(sender=a, receiver=b) a.send(m4) s.run(iterations=1) assert m4 in b.inbox _ = b.receive() start = time.time() s.run(seconds=0.30, pause_if_idle=False) end = time.time() assert 0.295 < end - start < 0.315, end - start alarm_msg = TestMessage(sender=a, receiver=a, topic="Alarm!!!") a.set_alarm(alarm_time=1, alarm_message=alarm_msg, relative=True, ignore_alarm_if_idle=False) assert len(s.clock.alarm_time) == 1 start = time.time() s.run(clear_alarms_at_end=True, pause_if_idle=True) end = time.time() assert 0.95 < end - start < 1.05 assert len(s.clock.alarm_time) == 0 alarm = a.receive() assert alarm_msg.topic == alarm.topic random_id = 2134565432 m6 = TestMessage(sender=a, receiver=random_id) a.send(m6) assert random_id not in s.agents a.log(msg="test done")
def test_run_scheduler_until(): s = Scheduler(real_time=False) a = TestAgent() s.add(a) msg1 = TestMessage(sender=a, receiver=a, topic='1') msg2 = TestMessage(sender=a, receiver=a, topic='2') msg3 = TestMessage(sender=a, receiver=a, topic='3') a.set_alarm(alarm_time=1, alarm_message=msg1, relative=True, ignore_alarm_if_idle=False) a.set_alarm(alarm_time=2, alarm_message=msg2, relative=True, ignore_alarm_if_idle=False) a.set_alarm(alarm_time=3, alarm_message=msg3, relative=True, ignore_alarm_if_idle=False) s.run(seconds=2) assert s.clock.time == 2 assert s.clock.list_alarms(a.uuid) == [(3, [msg3])] s = Scheduler(real_time=True) a = TestAgent() s.add(a) msg1 = TestMessage(sender=a, receiver=a, topic='1') start_time = time.time() a.set_alarm(alarm_time=start_time + 10, alarm_message=msg1, relative=True, ignore_alarm_if_idle=False) s.run(seconds=2) end_time = time.time() assert round(end_time - start_time, 0) == 2
def test01(): s = Scheduler() # create M2 m2_runtimes = {'A': 14, 'B': 7, 'C': 3, 'D': 10, 'E': 5, 'F': 6, 'G': 6} m2_transformations = { 'A': 'M1A', 'B': 'M1B', 'C': 'M1C', 'D': 'M1D', 'E': 'M1E', 'F': 'M1F', 'G': 'M1G' } m2 = Machine(name='M2', run_times=m2_runtimes, transformations=m2_transformations) order = Order(sender=m2, receiver=m2, order_items={ "A": 1, "B": 1, "C": 1, "D": 1, "E": 1, "F": 1, "G": 1 }) # {SKU:qty} m2.inbox.append(order) # create M1 m1_runtimes = { 'M1A': 2, 'M1B': 5, 'M1C': 10, 'M1D': 8, 'M1E': 4, 'M1F': 12, 'M1G': 9 } m1_transformations = { 'M1A': 'rawA', 'M1B': 'rawB', 'M1C': 'rawC', 'M1D': 'rawD', 'M1E': 'rawE', 'M1F': 'rawF', 'M1G': 'rawG' } m1 = Machine(name="M1", run_times=m1_runtimes, transformations=m1_transformations) # create Stock Agent. stock_agent = StockAgent() stock_agent.set_customer(m1) # Below we are setting the uuid that the agent (m1) needs to talk to (m2) and vice versa. m2.set_supplier(m1) m1.set_customer(m2) m1.set_supplier(stock_agent) # Add m1 and m2 to the scheduler. for agent in [m1, m2, stock_agent]: s.add(agent) s.run(pause_if_idle=True) check_sequence = ["A", "E", "B", "D", "G", "F", "C"] try: for idx, job in enumerate(m2.jobs): assert isinstance(job, Job) assert job.order_sku == check_sequence[idx] except AssertionError: raise AssertionError( "Expected the final sequence as: {}\n but got: {}".format( check_sequence, [job.order_sku for job in m2.jobs]))