def test_works_under_load(self): herald = DeliveryHerald() notified_ok = True def fn(msg: MessageId): try: sleep(1.5) herald.notify_delivered(msg) except: logging.exception('*** ERROR ***') notified_ok = False threads = [ Thread(target=fn, args=(MessageId(100, i), )) for i in range(1, 32) ] for t in threads: t.start() m = lambda x: MessageId(halink_ctx=100, tag=x) herald.wait_for_any(HaLinkMessagePromise([m(99), m(25), m(28), m(31)]), timeout_sec=5) for t in threads: t.join() self.assertTrue(notified_ok, 'Unexpected exception appeared in notifier thread')
def fn(): try: sleep(1.5) herald.notify_delivered(MessageId(halink_ctx=42, tag=3)) herald.notify_delivered(MessageId(halink_ctx=42, tag=1)) except: logging.exception('*** ERROR ***') notified_ok = False
def _msg_delivered_cb(self, proc_fid, proc_endpoint: str, tag: int, halink_ctx: int): LOG.info( 'Delivered to endpoint' "'{}', process fid = {}".format(proc_endpoint, str(proc_fid)) + 'tag= %d', tag) self.herald.notify_delivered(MessageId(halink_ctx=halink_ctx, tag=tag))
def test_if_delivered_earlier_than_awaited_notified_immediately(self): herald = DeliveryHerald() notified_ok = True thread_count = 1 latch = CountDownLatch(thread_count) def fn(msg: MessageId): try: LOG.debug('Thread started') herald.notify_delivered(msg) LOG.debug('Notified delivery %s', msg) latch.count_down() LOG.debug('Main thread unblocked') except: logging.exception('*** ERROR ***') notified_ok = False threads = [ Thread(target=fn, args=(MessageId(100, i + 1), )) for i in range(thread_count) ] for t in threads: t.start() # Block until all the threads come to latch.count_down() and thus # the message is notified for sure latch.await() def m(x): return MessageId(halink_ctx=100, tag=x) try: started = time() herald.wait_for_all(HaLinkMessagePromise([m(1)]), timeout_sec=2) finished = time() finally: for t in threads: t.join() self.assertTrue(notified_ok, 'Unexpected exception appeared in notifier thread') self.assertLess( finished - started, 5, 'Awaiting thread was unblocked only by a timeout. It means ' 'that unsorted_deliveries was analyzed too late.' )
def test_if_delivered_earlier_than_awaited_wait_many(self): herald = DeliveryHerald() notified_ok = True thread_count = 6 latch = CountDownLatch(thread_count) def fn(msg: MessageId): try: LOG.debug('Thread started') herald.notify_delivered(msg) LOG.debug('Notified delivery %s', msg) latch.count_down() LOG.debug('Main thread unblocked') except: logging.exception('*** ERROR ***') notified_ok = False threads = [ Thread(target=fn, args=(MessageId(100, i + 1), )) for i in range(thread_count) ] for t in threads: t.start() # Block until all the threads come to latch.count_down() and thus # the message is notified for sure latch.await() def m(x): return MessageId(halink_ctx=100, tag=x) try: herald.wait_for_all(HaLinkMessagePromise([m(1), m(5)]), timeout_sec=2) finally: for t in threads: t.join() self.assertTrue(notified_ok, 'Unexpected exception appeared in notifier thread') self.assertEqual(4, len(herald.unsorted_deliveries.keys()))
def m(x): return MessageId(halink_ctx=100, tag=x)
def fake_add(cmd): if hasattr(cmd, 'reply_to') and cmd.reply_to: cmd.reply_to.put([MessageId(1, 42)])
def hax_stop(self, *args): return [MessageId(111, 1)]
def ha_broadcast(self, _ha_ctx, ha_notes, notes_len): return [MessageId(101, 1), MessageId(101, 2)]
def ha_nvec_reply(self, _ha_ctx, ha_notes, notes_len): return [MessageId(101, 1), MessageId(101, 2)]
def ha_broadcast_hax_only(self, _ha_ctx, ha_notes, notes_len, hax_endpoint): return [MessageId(101, 1), MessageId(101, 2)]