def test_no_prefix_or_suffix(self): pattern_no_prefix_no_suffix = re.compile("^-[A-Za-z0-9]+-$") ut = UniqueWorkRef().id self.assertTrue(pattern_no_prefix_no_suffix.match(ut)) ut = UniqueWorkRef(prefix=None, suffix=None).id self.assertTrue(pattern_no_prefix_no_suffix.match(ut)) return
def test_with_suffix(self): random_sufx = random.choice(TestUniqueRef.sufx) pattern_with_suffix = re.compile( "^-[A-Za-z0-9]+-{}$".format(random_sufx)) ut = UniqueWorkRef(suffix=random_sufx).id self.assertTrue(pattern_with_suffix.match(ut)) ut = UniqueWorkRef(prefix=None, suffix=random_sufx).id self.assertTrue(pattern_with_suffix.match(ut)) return
def test_with_prefix(self): random_pref = random.choice(TestUniqueRef.pref) pattern_with_prefix = re.compile( "^{}-[A-Za-z0-9]+-$".format(random_pref)) ut = UniqueWorkRef(prefix=random_pref).id self.assertTrue(pattern_with_prefix.match(ut)) ut = UniqueWorkRef(prefix=random_pref, suffix=None).id self.assertTrue(pattern_with_prefix.match(ut)) return
def _do_work_initiate( self, work_notification_initiate: WorkNotificationInitiate) -> None: """ Handle the initiation the given work item from this agent """ self._trace_log_update("_do_work_initiate", work_notification_initiate) pool = self._get_recent_pool_address() if pool is None: task = work_notification_initiate.task logging.info( "{} Do Work Initiate waiting for Pool address task {} queued". format(self._agent_name, task.id)) self._add_item_to_initiate_queue( SimpleWorkNotificationInitiate(task=task, originator=self)) else: pool_topic = pool.topic to_do = list() to_do.append(work_notification_initiate) with self._work_lock: while not self._work_initiate.empty(): to_do.append(self._work_initiate.get_nowait()) for wni in to_do: task_to_do = wni.task wnd = SimpleWorkNotificationDo(unique_work_ref=UniqueWorkRef( prefix=str(task_to_do.id), suffix=self.name), task=task_to_do, source=self, originator=self) logging.info("{} Task {} Initiate sent to Pool {}".format( self.name, self._agent_name, pool_topic)) pub.sendMessage(topicName=pool_topic, notification=wnd) return
def __init__(self, responder_srcsink: SrcSink, address_book: List[SrcSink], sender_workref: UniqueWorkRef): self._work_ref = UniqueWorkRef(responder_srcsink.name, UniqueRef().ref) self._sender_srcsink = responder_srcsink self._address_book = address_book self._sender_workref = sender_workref return
def _factory() -> UniqueWorkRef: """ Generate a random instance of a UniqueWorkRef :return: A new UniqueWorkRef """ return UniqueWorkRef(prefix=random.choice(TestUniqueRef.pref), suffix=random.choice(TestUniqueRef.sufx))
def test_unique(self): """ Generate 10K ref's with random prefix/suffix and ensure ref follows expected pattern as well as there being no duplicate references generated. """ have_seen = dict() dups = 0 for _ in range(10000): random_pref = random.choice(TestUniqueRef.pref) random_sufx = random.choice(TestUniqueRef.sufx) if random_pref is None: test_pref = '' else: test_pref = random_pref if random_sufx is None: test_sufx = '' else: test_sufx = random_sufx test_pattern = re.compile("^{}-[A-Za-z0-9]+-{}$".format( test_pref, test_sufx)) ut = UniqueWorkRef(prefix=random_pref, suffix=random_sufx).id self.assertTrue(test_pattern.match(ut)) if ut in have_seen: dups += 1 else: have_seen[ut] = True self.assertTrue(dups == 0) return
def test_simple_task_injection(self): effort = 3 capacity = 1 source_name = "Dummy-Test-Source-Inject" SimpleTask.process_start_state(State.S0) SimpleTask.process_end_state(State.S1) for i in range(3): logging.info( "\n\n- - - - - T E S T C A S E {} - - - -\n\n".format(i)) test_task = SimpleTask(effort=effort) test_srcsink = DummySrcSink(source_name) test_agent = SimpleAgent( 'agent {}'.format(i), start_state=State.S0, end_state=State.S1, capacity=capacity, task_consumption_policy=GreedyTaskConsumptionPolicy(), trace=True) test_notification = SimpleWorkNotificationDo(UniqueWorkRef( suffix=source_name, prefix=str(test_task.id)), originator=test_agent, source=test_srcsink, task=test_task) time.sleep(1) if i == 0: # Publish to agent via it's private topic. # test_agent(test_notification) pub.sendMessage(topicName=test_agent.topic, notification=test_notification) elif i == 1: # Direct Injection. test_agent._do_work(test_notification) else: # With Agent as callable test_agent(test_notification) time.sleep(1) tlid = SimpleAgent.trace_log_id("_do_work", type(test_notification), test_notification.work_ref) self.assertEqual(test_agent.trace_log[tlid], effort) tlid = SimpleAgent.trace_log_id("_do_notification", type(test_notification), test_notification.work_ref) self.assertTrue(tlid not in test_agent.trace_log) self.assertEqual(test_task.lead_time, int(ceil(effort / capacity))) work_done = test_agent.work_done self.assertEqual(1, len(work_done)) self.assertEqual(test_task.id, work_done[0].task.id) return
def __init__(self, task: Task, originator: SrcSink, source: SrcSink = None): self._task = task if source is None: self._source = originator else: self._source = source self._originator = originator self._work_ref = UniqueWorkRef(suffix=str(task.id), prefix=originator.name) return
def test_handlers(self): task_pool = SimpleTaskPool('Task Pool 1') SimpleTask.process_start_state(State.S0) SimpleTask.process_end_state(State.S1) test_task = SimpleTask(effort=1, start_state=State.S0) uwr = UniqueWorkRef("Dummy1", "Dummy2") src = DummySrcSink("Source") orig = DummySrcSink("originator") scenarios = [ SimpleWorkNotificationDo(uwr, test_task, orig, src), SimpleWorkNotificationFinalise(uwr, test_task, orig, src) ] try: for scenario in scenarios: task_pool(scenario) except Exception as e: self.fail("Un expected exception {}".format(str(e))) return
def __init__(self, sender_srcsink: SrcSink, required_capabilities: List[Capability]): self._work_ref = UniqueWorkRef(sender_srcsink.name, UniqueRef().ref) self._sender_srcsink = sender_srcsink self._required_capabilities = required_capabilities return
def __init__(self): self._unique_sig = UniqueWorkRef( prefix=str(TestWorkNotificationDo._task_id), suffix=str(TestWorkNotificationDo._src)) TestWorkNotificationDo._task_id += 1 TestWorkNotificationDo._src += 1