class InteriorSyncUpTest(MessagingHandler): def __init__(self, host_a, host_b, inter_router_port): """ This test verifies that a router can join an existing network and be synced up using an absolute MAU (as opposed to an incremental MAU). This requires that the exiting network have sequenced through mobile address changes at least 10 times to avoid the cached incremental update optimization that the routers use. """ super(InteriorSyncUpTest, self).__init__() self.host_a = host_a self.host_b = host_b self.timer = None self.poll_timer = None self.delay_timer = None self.count = 200 self.delay_count = 12 # This should be larger than MAX_KEPT_DELTAS in mobile.py self.inter_router_port = inter_router_port self.receivers = [] self.n_receivers = 0 self.n_setup_delays = 0 self.error = None self.last_action = "test initialization" self.expect = "" def fail(self, text): self.error = text self.conn_a.close() self.conn_b.close() self.timer.cancel() if self.poll_timer: self.poll_timer.cancel() if self.delay_timer: self.delay_timer.cancel() def timeout(self): self.error = "Timeout Expired - last_action: %s, n_receivers: %d, n_setup_delays: %d" % \ (self.last_action, self.n_receivers, self.n_setup_delays) self.conn_a.close() self.conn_b.close() if self.poll_timer: self.poll_timer.cancel() if self.delay_timer: self.delay_timer.cancel() def poll_timeout(self): self.probe() def delay_timeout(self): self.n_setup_delays += 1 self.add_receivers() def add_receivers(self): if len(self.receivers) < self.count: self.receivers.append(self.container.create_receiver(self.conn_b, "address.%d" % len(self.receivers))) if self.n_setup_delays < self.delay_count: self.delay_timer = self.reactor.schedule(2.0, DelayTimeout(self)) else: while len(self.receivers) < self.count: self.receivers.append(self.container.create_receiver(self.conn_b, "address.%d" % len(self.receivers))) def on_start(self, event): self.container = event.container self.reactor = event.reactor self.timer = self.reactor.schedule(40.0, Timeout(self)) self.conn_a = self.container.connect(self.host_a) self.conn_b = self.container.connect(self.host_b) self.probe_receiver = self.container.create_receiver(self.conn_a, dynamic=True) self.last_action = "on_start - opened connections" def probe(self): self.probe_sender.send(self.proxy.query_addresses()) def on_link_opened(self, event): if event.receiver == self.probe_receiver: self.probe_reply = self.probe_receiver.remote_source.address self.proxy = MgmtMsgProxy(self.probe_reply) self.probe_sender = self.container.create_sender(self.conn_a, '$management') elif event.sender == self.probe_sender: ## ## Create listeners for an address per count ## self.add_receivers() self.last_action = "started slow creation of receivers" elif event.receiver in self.receivers: self.n_receivers += 1 if self.n_receivers == self.count: self.expect = "not-found" self.probe() self.last_action = "started probe expecting addresses not found" def on_message(self, event): if event.receiver == self.probe_receiver: response = self.proxy.response(event.message); if response.status_code < 200 or response.status_code > 299: self.fail("Unexpected operation failure: (%d) %s" % (response.status_code, response.status_description)) if self.expect == "not-found": response = self.proxy.response(event.message) for addr in response.results: if "address." in addr.name: self.fail("Found address on host-a when we didn't expect it - %s" % addr.name) ## ## Hook up the two routers to start the sync-up ## self.probe_sender.send(self.proxy.create_connector("IR", port=self.inter_router_port, role="inter-router")) self.expect = "create-success" self.last_action = "created inter-router connector" elif self.expect == "create-success": ## ## Start polling for the addresses on host_a ## response = self.proxy.response(event.message) self.probe_sender.send(self.proxy.query_addresses()) self.expect = "query-success" self.last_action = "started probing host_a for addresses" elif self.expect == "query-success": response = self.proxy.response(event.message) got_count = 0 for addr in response.results: if "address." in addr.name: got_count += 1 self.last_action = "Got a query response with %d of the expected addresses" % (got_count) if got_count == self.count: self.fail(None) else: self.poll_timer = self.reactor.schedule(0.5, PollTimeout(self)) def run(self): container = Container(self) container.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 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 InteriorSyncUpTest(MessagingHandler): def __init__(self, host_a, host_b, inter_router_port): """ This test verifies that a router can join an existing network and be synced up using an absolute MAU (as opposed to an incremental MAU). This requires that the exiting network have sequenced through mobile address changes at least 10 times to avoid the cached incremental update optimization that the routers use. """ super(InteriorSyncUpTest, self).__init__() self.host_a = host_a self.host_b = host_b self.timer = None self.poll_timer = None self.delay_timer = None self.count = 2000 self.delay_count = 12 # This should be larger than MAX_KEPT_DELTAS in mobile.py self.inter_router_port = inter_router_port self.receivers = [] self.n_receivers = 0 self.n_setup_delays = 0 self.error = None self.last_action = "test initialization" self.expect = "" def fail(self, text): self.error = text self.conn_a.close() self.conn_b.close() self.timer.cancel() if self.poll_timer: self.poll_timer.cancel() if self.delay_timer: self.delay_timer.cancel() def timeout(self): self.error = "Timeout Expired - last_action: %s, n_receivers: %d, n_setup_delays: %d" % \ (self.last_action, self.n_receivers, self.n_setup_delays) self.conn_a.close() self.conn_b.close() if self.poll_timer: self.poll_timer.cancel() if self.delay_timer: self.delay_timer.cancel() def poll_timeout(self): self.probe() def delay_timeout(self): self.n_setup_delays += 1 self.add_receivers() def add_receivers(self): if len(self.receivers) < self.count: self.receivers.append( self.container.create_receiver( self.conn_b, "address.%d" % len(self.receivers))) if self.n_setup_delays < self.delay_count: self.delay_timer = self.reactor.schedule(2.0, DelayTimeout(self)) else: while len(self.receivers) < self.count: self.receivers.append( self.container.create_receiver( self.conn_b, "address.%d" % len(self.receivers))) def on_start(self, event): self.container = event.container self.reactor = event.reactor self.timer = self.reactor.schedule(TIMEOUT, TestTimeout(self)) self.conn_a = self.container.connect(self.host_a) self.conn_b = self.container.connect(self.host_b) self.probe_receiver = self.container.create_receiver(self.conn_a, dynamic=True) self.last_action = "on_start - opened connections" def probe(self): self.probe_sender.send(self.proxy.query_addresses()) def on_link_opened(self, event): if event.receiver == self.probe_receiver: self.probe_reply = self.probe_receiver.remote_source.address self.proxy = MgmtMsgProxy(self.probe_reply) self.probe_sender = self.container.create_sender( self.conn_a, '$management') elif event.sender == self.probe_sender: ## # Create listeners for an address per count ## self.add_receivers() self.last_action = "started slow creation of receivers" elif event.receiver in self.receivers: self.n_receivers += 1 if self.n_receivers == self.count: self.expect = "not-found" self.probe() self.last_action = "started probe expecting addresses not found" def on_message(self, event): if event.receiver == self.probe_receiver: response = self.proxy.response(event.message) if response.status_code < 200 or response.status_code > 299: self.fail("Unexpected operation failure: (%d) %s" % (response.status_code, response.status_description)) if self.expect == "not-found": response = self.proxy.response(event.message) for addr in response.results: if "address." in addr.name: self.fail( "Found address on host-a when we didn't expect it - %s" % addr.name) ## # Hook up the two routers to start the sync-up ## self.probe_sender.send( self.proxy.create_connector("IR", port=self.inter_router_port, role="inter-router")) self.expect = "create-success" self.last_action = "created inter-router connector" elif self.expect == "create-success": ## # Start polling for the addresses on host_a ## response = self.proxy.response(event.message) self.probe_sender.send(self.proxy.query_addresses()) self.expect = "query-success" self.last_action = "started probing host_a for addresses" elif self.expect == "query-success": response = self.proxy.response(event.message) got_count = 0 for addr in response.results: if "address." in addr.name: got_count += 1 self.last_action = "Got a query response with %d of the expected addresses" % ( got_count) if got_count == self.count: self.fail(None) else: self.poll_timer = self.reactor.schedule( 0.5, PollTimeout(self)) def run(self): container = Container(self) container.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()