class TxLinkCreditTest(MessagingHandler): def __init__(self, host): super(TxLinkCreditTest, self).__init__() self.host = host self.sender_conn = None self.query_conn = None self.addr = "rx/link/credit/test" self.credit_issued = 0 self.error = None self.stages = ['Setup', 'LinkBlocked', 'LinkUnblocked', '250Credits'] self.stage = 0 def timeout(self): self.error = "Timeout Expired - stage: %s" % self.stages[self.stage] self.sender_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() def fail(self, error): self.error = error self.sender_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() self.timer.cancel() def on_start(self, event): self.timer = event.reactor.schedule(TIMEOUT, TestTimeout(self)) self.poll_timer = None self.sender_conn = event.container.connect(self.host) self.query_conn = event.container.connect(self.host) self.reply_receiver = event.container.create_receiver(self.query_conn, None, dynamic=True) self.query_sender = event.container.create_sender(self.query_conn, "$management") self.sender = None self.receiver = None def on_link_opened(self, event): if event.receiver == self.reply_receiver: self.reply_addr = event.receiver.remote_source.address self.proxy = MgmtMsgProxy(self.reply_addr) self.sender = event.container.create_sender(self.sender_conn, self.addr) elif event.sender == self.sender: self.stage = 1 self.process() def process(self): if self.stage == 1: # # LinkBlocked # msg = self.proxy.query_router() self.query_sender.send(msg) elif self.stage == 2: # # LinkUnblocked # msg = self.proxy.query_router() self.query_sender.send(msg) elif self.stage == 3: # # 250Credits # msg = self.proxy.query_links() self.query_sender.send(msg) def on_message(self, event): if event.receiver == self.reply_receiver: response = self.proxy.response(event.message) if self.stage == 1: # # LinkBlocked # if response.results[0].linksBlocked == 1: self.receiver = event.container.create_receiver(self.sender_conn, self.addr) self.stage = 2 self.process() return elif self.stage == 2: # # LinkUnblocked # if response.results[0].linksBlocked == 0: self.stage = 3 self.process() return elif self.stage == 3: # # 250Credits # for link in response.results: if 'M0' + self.addr == link.owningAddr: if link.creditAvailable == 250: self.fail(None) return self.poll_timer = event.reactor.schedule(0.5, PollTimeout(self)) def poll_timeout(self): self.process() def run(self): Container(self).run()
class DelayedSettlementTest(MessagingHandler): def __init__(self, sender_host, receiver_host, query_host, addr, dlv_count, stuck_list, close_link): super(DelayedSettlementTest, self).__init__(auto_accept=False) self.sender_host = sender_host self.receiver_host = receiver_host self.query_host = query_host self.addr = addr self.dlv_count = dlv_count self.stuck_list = stuck_list self.close_link = close_link self.stuck_dlvs = [] self.sender_conn = None self.receiver_conn = None self.query_conn = None self.error = None self.n_tx = 0 self.n_rx = 0 self.expected_stuck = 0 self.last_stuck = 0 def timeout(self): self.error = "Timeout Expired - n_tx=%d, n_rx=%d, expected_stuck=%d last_stuck=%d" %\ (self.n_tx, self.n_rx, self.expected_stuck, self.last_stuck) self.sender_conn.close() self.receiver_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() def fail(self, error): self.error = error self.sender_conn.close() self.receiver_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() self.timer.cancel() def on_start(self, event): self.timer = event.reactor.schedule(30.0, Timeout(self)) self.poll_timer = None self.sender_conn = event.container.connect(self.sender_host) self.receiver_conn = event.container.connect(self.receiver_host) self.query_conn = event.container.connect(self.query_host) self.sender = event.container.create_sender(self.sender_conn, self.addr) self.receiver = event.container.create_receiver( self.receiver_conn, self.addr) self.reply_receiver = event.container.create_receiver(self.query_conn, None, dynamic=True) self.query_sender = event.container.create_sender( self.query_conn, "$management") def on_link_opened(self, event): if event.receiver == self.reply_receiver: self.reply_addr = event.receiver.remote_source.address self.proxy = MgmtMsgProxy(self.reply_addr) def on_sendable(self, event): if event.sender == self.sender: while self.sender.credit > 0 and self.n_tx < self.dlv_count: self.sender.send(Message("Message %d" % self.n_tx)) self.n_tx += 1 def on_message(self, event): if event.receiver == self.receiver: if self.n_rx not in self.stuck_list: self.accept(event.delivery) else: self.stuck_dlvs.append(event.delivery) self.n_rx += 1 if self.n_rx == self.dlv_count: self.query_stats(len(self.stuck_list) * 2) elif event.receiver == self.reply_receiver: response = self.proxy.response(event.message) self.accept(event.delivery) self.last_stuck = response.results[0].deliveriesStuck if response.results[0].deliveriesStuck == self.expected_stuck: if self.close_link: self.receiver.close() else: for dlv in self.stuck_dlvs: self.accept(dlv) self.stuck_dlvs = [] if self.expected_stuck > 0: self.query_stats(0) else: self.fail(None) else: self.poll_timer = event.reactor.schedule( 0.5, PollTimeout(self)) def query_stats(self, expected_stuck): self.expected_stuck = expected_stuck msg = self.proxy.query_router() self.query_sender.send(msg) def poll_timeout(self): self.query_stats(self.expected_stuck) def run(self): Container(self).run()
class DelayedSettlementTest(MessagingHandler): def __init__(self, sender_host, receiver_host, query_host, addr, dlv_count, stuck_list, close_link): super(DelayedSettlementTest, self).__init__(auto_accept=False) self.sender_host = sender_host self.receiver_host = receiver_host self.query_host = query_host self.addr = addr self.dlv_count = dlv_count self.stuck_list = stuck_list self.close_link = close_link self.stuck_dlvs = [] self.sender_conn = None self.receiver_conn = None self.query_conn = None self.error = None self.n_tx = 0 self.n_rx = 0 self.expected_stuck = 0 self.last_stuck = 0 def timeout(self): self.error = "Timeout Expired - n_tx=%d, n_rx=%d, expected_stuck=%d last_stuck=%d" %\ (self.n_tx, self.n_rx, self.expected_stuck, self.last_stuck) self.sender_conn.close() self.receiver_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() def fail(self, error): self.error = error self.sender_conn.close() self.receiver_conn.close() self.query_conn.close() if self.poll_timer: self.poll_timer.cancel() self.timer.cancel() def on_start(self, event): self.timer = event.reactor.schedule(TIMEOUT, TestTimeout(self)) self.poll_timer = None self.receiver_conn = event.container.connect(self.receiver_host) self.query_conn = event.container.connect(self.query_host) self.receiver = event.container.create_receiver(self.receiver_conn, self.addr) self.reply_receiver = event.container.create_receiver(self.query_conn, None, dynamic=True) self.query_sender = event.container.create_sender(self.query_conn, "$management") self.proxy = None def on_link_opened(self, event): if event.receiver == self.reply_receiver: self.reply_addr = event.receiver.remote_source.address self.proxy = MgmtMsgProxy(self.reply_addr) # Create the sender only after the self.proxy is populated. # If the sender was created in the on_start, in some cases, the on_sendable # is called before the on_link_opened and the self.proxy remains empty. # see DISPATCH-1675. # The second error in test_04_delayed_settlement_different_edges_check_interior # is caused due to the first test failure, so this fix will # fix the second failure self.sender_conn = event.container.connect(self.sender_host) self.sender = event.container.create_sender(self.sender_conn, self.addr) def on_sendable(self, event): if event.sender == self.sender: while self.sender.credit > 0 and self.n_tx < self.dlv_count: self.sender.send(Message("Message %d" % self.n_tx)) self.n_tx += 1 def on_message(self, event): if event.receiver == self.receiver: if self.n_rx not in self.stuck_list: self.accept(event.delivery) else: self.stuck_dlvs.append(event.delivery) self.n_rx += 1 if self.n_rx == self.dlv_count: self.query_stats(len(self.stuck_list) * 2) elif event.receiver == self.reply_receiver: response = self.proxy.response(event.message) self.accept(event.delivery) self.last_stuck = response.results[0].deliveriesStuck if self.last_stuck == self.expected_stuck: if self.close_link: self.receiver.close() else: for dlv in self.stuck_dlvs: self.accept(dlv) self.stuck_dlvs = [] if self.expected_stuck > 0: self.query_stats(0) else: self.fail(None) else: self.poll_timer = event.reactor.schedule(0.5, PollTimeout(self)) def query_stats(self, expected_stuck): self.expected_stuck = expected_stuck msg = self.proxy.query_router() self.query_sender.send(msg) def poll_timeout(self): self.query_stats(self.expected_stuck) def run(self): Container(self).run()