def test_example_requester_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback, frequency=1.0) self.next_step = self.step1 # first step of test sequence self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin()
def test_example_scheduler_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback) self.actions = collections.deque([self.step1, self.step2, self.step3]) self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin()
class ExampleRequester: def __init__(self): rospy.init_node("example_requester") self.rqr = Requester(self.feedback) self.request_turtlebot() self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin() def feedback(self, rset): """ Scheduler feedback function. """ for rq in rset.values(): if rq.msg.status == Request.WAITING: rospy.loginfo('Request queued: ' + str(rq.uuid)) elif rq.msg.status == Request.GRANTED: rospy.loginfo('Request granted: ' + str(rq.uuid)) elif rq.msg.status == Request.CLOSED: rospy.loginfo('Request closed: ' + str(rq.uuid)) elif rq.msg.status == Request.PREEMPTING: rospy.loginfo('Request preempted (reason=' + str(rq.msg.reason) + '): ' + str(rq.uuid)) rq.cancel() # release preempted resource immediately def periodic_update(self, event): """ Timer event handler for periodic request updates. """ try: # cancel the previous request, holding the Big Requester Lock with self.rqr.lock: self.rqr.rset[self.request_id].cancel() except KeyError: # previous request is gone, request another similar robot self.request_turtlebot() def request_turtlebot(self): """ Request any tutlebot able to run *example_rapp*. """ bot = Resource(rapp='example_rapp', uri='rocon:/turtlebot') self.request_id = self.rqr.new_request([bot]) self.rqr.send_requests()
class TestTimeoutRequester(unittest.TestCase): def test_example_requester_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback, frequency=1.0) self.next_step = self.step1 # first step of test sequence self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin() def feedback(self, rset): """ Scheduler feedback function. """ rospy.loginfo('feedback callback:') for rq in rset.values(): rospy.logdebug(' ' + str(rq)) if rq.msg.status == Request.WAITING: rospy.loginfo(' request queued: ' + str(rq.uuid)) elif rq.msg.status == Request.GRANTED: rospy.loginfo(' request granted: ' + str(rq.uuid)) elif rq.msg.status == Request.CLOSED: rospy.loginfo(' request closed: ' + str(rq.uuid)) elif rq.msg.status == Request.PREEMPTING: rospy.loginfo(' request preempted (reason=' + str(rq.msg.reason) + '): ' + str(rq.uuid)) rq.cancel() # release preempted resources immediately def periodic_update(self, event): """ Timer event handler for periodic request updates. Invokes self.next_step(), unless ``None``. This method runs in a different thread from the feedback callback, so acquire the Big Requester Lock for running each action step, even though most of them do not require it. """ if self.next_step is not None: # more to do? with self.rqr.lock: self.next_step() else: # no more steps rospy.signal_shutdown('test completed.') def request_turtlebot(self): """ Request any tutlebot able to run *example_rapp*. :returns: UUID of new request sent. """ bot = Resource(rapp='example_rapp', uri='rocon:/turtlebot') rq_id = self.rqr.new_request([bot]) rospy.loginfo(' new request: ' + str(rq_id)) return rq_id def verify(self, rq_list): self.assertEqual(len(self.rqr.rset), len(rq_list)) for rq in rq_list: self.assertTrue(rq in self.rqr.rset) def step1(self): rospy.loginfo('Step 1') # allocate two requests and send them immediately self.rq1 = self.request_turtlebot() self.rq2 = self.request_turtlebot() self.verify([self.rq1, self.rq2]) self.rqr.send_requests() self.next_step = self.step2 def step2(self): rospy.loginfo('Step 2') self.verify([self.rq1, self.rq2]) # send another request, which should wait self.rq3 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.send_requests() self.next_step = self.step3 def step3(self): rospy.loginfo('Step 3') self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.rset[self.rq2].cancel() self.rq4 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3, self.rq4]) self.rqr.send_requests() self.next_step = self.step4 def step4(self): rospy.loginfo('Step 4') self.verify([self.rq1, self.rq3, self.rq4]) self.next_step = None
class TestExampleScheduler(unittest.TestCase): def test_example_scheduler_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback) self.actions = collections.deque([self.step1, self.step2, self.step3]) self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin() def feedback(self, rset): """ Scheduler feedback function. """ print('feedback callback:') for rq in rset.values(): print(' ' + str(rq)) if rq.msg.status == Request.WAITING: print('Request queued: ' + str(rq.uuid)) elif rq.msg.status == Request.GRANTED: print('Request granted: ' + str(rq.uuid)) elif rq.msg.status == Request.CLOSED: print('Request closed: ' + str(rq.uuid)) elif rq.msg.status == Request.PREEMPTING: print('Request preempted (reason=' + str(rq.msg.reason) + '): ' + str(rq.uuid)) rq.cancel() # release preempted resource immediately def periodic_update(self, event): """ Timer event handler for periodic request updates. This method runs in a different thread from the feedback callback, so acquire the Big Requester Lock for running each action step, even though most of them do not require it. """ # Invoke self.actions in order. if len(self.actions) > 0: # actions remain? next_step = self.actions.popleft() with self.rqr.lock: next_step() else: # no more actions rospy.signal_shutdown('test completed.') def request_turtlebot(self): """ Request any tutlebot able to run *example_rapp*. :returns: UUID of new request sent. """ bot = Resource(rapp='example_rapp', uri='rocon:/turtlebot') request_id = self.rqr.new_request([bot]) self.rqr.send_requests() return request_id def step1(self): """ first step of test sequence. """ print('Step 1') self.rq1 = self.request_turtlebot() print('New request: ' + str(self.rq1)) def step2(self): """ second step of test sequence. """ print('Step 2') # verify that there is one active request self.assertEqual(len(self.rqr.rset), 1) self.assertTrue(self.rq1 in self.rqr.rset) # cancel previous request self.rqr.cancel_all() self.rqr.send_requests() # send a new one immediately self.rq2 = self.request_turtlebot() print('New request: ' + str(self.rq2)) # verify that there are two active requests self.assertEqual(len(self.rqr.rset), 2) self.assertTrue(self.rq2 in self.rqr.rset) def step3(self): """ third step of test sequence. """ # verify that there is only one active request print('Step 3') self.assertEqual(len(self.rqr.rset), 1) self.assertTrue(self.rq1 not in self.rqr.rset) self.assertTrue(self.rq2 in self.rqr.rset)
class TestTimeoutRequester(unittest.TestCase): def test_example_requester_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback, frequency=1.0) self.next_step = self.step1 # first step of test sequence self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin() def feedback(self, rset): """ Scheduler feedback function. """ rospy.loginfo('feedback callback:') for rq in rset.values(): rospy.logdebug(' ' + str(rq)) if rq.msg.status == Request.WAITING: rospy.loginfo(' request queued: ' + str(rq.uuid)) elif rq.msg.status == Request.GRANTED: rospy.loginfo(' request granted: ' + str(rq.uuid)) elif rq.msg.status == Request.CLOSED: rospy.loginfo(' request closed: ' + str(rq.uuid)) elif rq.msg.status == Request.PREEMPTING: rospy.loginfo(' request preempted (reason=' + str(rq.msg.reason) + '): ' + str(rq.uuid)) rq.cancel() # release preempted resources immediately def periodic_update(self, event): """ Timer event handler for periodic request updates. Invokes self.next_step(), unless ``None``. """ if self.next_step is not None: # more to do? self.next_step() else: # no more steps rospy.signal_shutdown('test completed.') def request_turtlebot(self): """ Request any tutlebot able to run *example_rapp*. :returns: UUID of new request sent. """ bot = Resource(rapp='tests/example_rapp', uri='rocon:/turtlebot') rq_id = self.rqr.new_request([bot]) rospy.loginfo(' new request: ' + str(rq_id)) return rq_id def verify(self, rq_list): self.assertEqual(len(self.rqr.rset), len(rq_list)) for rq in rq_list: self.assertTrue(rq in self.rqr.rset) def step1(self): rospy.loginfo('Step 1') # allocate two requests and send them immediately self.rq1 = self.request_turtlebot() self.rq2 = self.request_turtlebot() self.verify([self.rq1, self.rq2]) self.rqr.send_requests() self.next_step = self.step2 def step2(self): rospy.loginfo('Step 2') self.verify([self.rq1, self.rq2]) # send another request, which should wait self.rq3 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.send_requests() self.next_step = self.step3 def step3(self): rospy.loginfo('Step 3') self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.rset[self.rq2].cancel() self.rq4 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3, self.rq4]) self.rqr.send_requests() self.next_step = self.step4 def step4(self): rospy.loginfo('Step 4') self.verify([self.rq1, self.rq3, self.rq4]) self.next_step = None
class TestTimeoutRequester(unittest.TestCase): def test_timeout_requester_(self): """ Initialize ROCON scheduler node for example requester. """ rospy.init_node("test_example_scheduler") self.rqr = Requester(self.feedback, frequency=1.0) self.next_step = self.step1 # first step of test sequence self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin() def feedback(self, rset): """ Scheduler feedback function. """ rospy.loginfo('feedback callback:') for rq in rset.values(): rospy.logdebug(' ' + str(rq)) if rq.msg.status == Request.WAITING: rospy.loginfo(' request queued: ' + str(rq.uuid)) elif rq.msg.status == Request.GRANTED: rospy.loginfo(' request granted: ' + str(rq.uuid)) elif rq.msg.status == Request.CLOSED: rospy.loginfo(' request closed: ' + str(rq.uuid)) elif rq.msg.status == Request.PREEMPTING: rospy.loginfo(' request preempted (reason=' + str(rq.msg.reason) + '): ' + str(rq.uuid)) rq.cancel() # release preempted resources immediately def periodic_update(self, event): """ Timer event handler for periodic request updates. Invokes self.next_step(), unless ``None``. This method runs in a different thread from the feedback callback, so acquire the Big Requester Lock for running each action step, even though most of them do not require it. """ if self.next_step is not None: # more to do? with self.rqr.lock: self.next_step() else: # no more steps rospy.signal_shutdown('test completed.') def request_turtlebot(self): """ Request any tutlebot able to run *example_rapp*. :returns: UUID of new request sent. """ bot = Resource(rapp='example_rapp', uri='rocon:/turtlebot') rq_id = self.rqr.new_request([bot]) rospy.loginfo(' new request: ' + str(rq_id)) return rq_id def verify(self, rq_list): self.assertEqual(len(self.rqr.rset), len(rq_list)) for rq in rq_list: self.assertTrue(rq in self.rqr.rset) def step1(self): rospy.loginfo('Step 1') # allocate two requests and send them immediately self.rq1 = self.request_turtlebot() self.rq2 = self.request_turtlebot() self.verify([self.rq1, self.rq2]) self.rqr.send_requests() self.next_step = self.step2 def step2(self): rospy.loginfo('Step 2') self.verify([self.rq1, self.rq2]) # send another request, which should wait self.rq3 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.send_requests() self.next_step = self.step3 def step3(self): rospy.loginfo('Step 3') self.verify([self.rq1, self.rq2, self.rq3]) self.rqr.rset[self.rq2].cancel() self.rq4 = self.request_turtlebot() self.verify([self.rq1, self.rq2, self.rq3, self.rq4]) self.rqr.send_requests() self.next_step = self.step4 def step4(self): rospy.loginfo('Step 4') self.verify([self.rq1, self.rq3, self.rq4]) rospy.loginfo('disconnect requester') self.rqr._unregister() # simulate disconnection self.wait_cycles = 0 # cycle count self.next_step = self.step5 def step5(self): self.wait_cycles += 1 rospy.loginfo('Step 5.' + str(self.wait_cycles)) if self.wait_cycles < 12: # not done waiting? return # reconnect requester using the same UUID rospy.loginfo('reconnect requester') self.rqr._set_timer() # restart heartbeat self.rq5 = self.request_turtlebot() # make a new request self.rqr.send_requests() self.next_step = self.step6 def step6(self): rospy.loginfo('Step 6') self.verify([self.rq5]) self.next_step = None # done
def __init__(self): rospy.init_node("example_requester") self.rqr = Requester(self.feedback) self.request_turtlebot() self.timer = rospy.Timer(rospy.Duration(2.0), self.periodic_update) rospy.spin()