test_server_components.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2009, Willow Garage, Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Willow Garage, Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 
29 # Author: Alexander Sorokin.
30 
31 PKG = 'actionlib'
32 import rospy
33 
34 import unittest
35 import threading
36 
37 from actionlib import goal_id_generator, status_tracker
38 import actionlib_msgs.msg
39 
40 
41 # A sample python unit test
42 class TestGoalIDGenerator(unittest.TestCase):
43 
44  def test_generator(self):
45  gen1 = goal_id_generator.GoalIDGenerator()
46 
47  id1 = gen1.generate_ID()
48  id2 = gen1.generate_ID()
49  id3 = gen1.generate_ID()
50  nn1, s1, ts1 = id1.id.split('-')
51  nn2, s2, ts2 = id2.id.split('-')
52  nn3, s3, ts3 = id3.id.split('-')
53 
54  self.assertEquals(nn1, "/test_goal_id_generator", "node names are different")
55  self.assertEquals(nn1, nn2, "node names are different")
56  self.assertEquals(nn1, nn3, "node names are different")
57 
58  self.assertEquals(s1, "1", "Sequence numbers mismatch")
59  self.assertEquals(s2, "2", "Sequence numbers mismatch")
60  self.assertEquals(s3, "3", "Sequence numbers mismatch")
61 
63  """
64  This test checks that all the ids are generated unique. This test should fail if the synchronization is set incorrectly.
65  @note this test is can succeed when the errors are present.
66  """
67  global ids_lists
68  ids_lists = {}
69 
70  def gen_ids(tID=1, num_ids=1000):
71  gen = goal_id_generator.GoalIDGenerator()
72  for i in range(0, num_ids):
73  id = gen.generate_ID()
74  ids_lists[tID].append(id)
75 
76  num_ids = 1000
77  num_threads = 200
78  threads = []
79  for tID in range(0, num_threads):
80  ids_lists[tID] = []
81  t = threading.Thread(None, gen_ids, None, (), {'tID': tID, 'num_ids': num_ids})
82  threads.append(t)
83  t.start()
84 
85  for t in threads:
86  t.join()
87 
88  all_ids = {}
89  for tID in range(0, num_threads):
90  self.assertEquals(len(ids_lists[tID]), num_ids)
91  for id in ids_lists[tID]:
92  nn, s, ts = id.id.split('-')
93  self.assertFalse(s in all_ids, "Duplicate ID found")
94  all_ids[s] = 1
95 
97  tracker = status_tracker.StatusTracker("test-id", actionlib_msgs.msg.GoalStatus.PENDING)
98  self.assertEquals(tracker.status.goal_id, "test-id")
99  self.assertEquals(tracker.status.status, actionlib_msgs.msg.GoalStatus.PENDING)
100 
101 
102 if __name__ == '__main__':
103  import rostest
104  rospy.init_node("test_goal_id_generator")
105  rostest.rosrun(PKG, 'test_goal_id_generator', TestGoalIDGenerator)


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep, Mikael Arguedas
autogenerated on Tue Sep 3 2019 03:41:41