carbon_collector_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import unittest
5 from pal_carbon_collector.carbon_collector import CarbonCollector
6 from pal_statistics_msgs.msg import Statistic
7 from pal_statistics_msgs.msg import Statistics
8 
9 SYSTEM_NAME = 'test_system'
10 
11 class CarbonCollectorTest(unittest.TestCase):
12 
13  def setUp(self):
14  self.cc = CarbonCollector(topics=[], system_name=SYSTEM_NAME, dry_run=True)
15 
16  # no statistics
17  def test_statistics_1(self):
18  msg = Statistics()
19  result = self.cc.statistics_callback(msg)
20  self.assertEquals(result, '')
21 
22  # one statistic with default stamp
23  def test_statistics_2(self):
24  msg = Statistics()
25 
26  msg.header.stamp = rospy.Time()
27 
28  stat = Statistic()
29  stat.name = "test_stat"
30  stat.value = 1234.0
31  msg.statistics.append(stat)
32 
33  result = self.cc.statistics_callback(msg)
34  output = result.splitlines()
35  self.assertEquals(len(output), 1)
36  self.assertEquals(output[0], 'test_system.test_stat 1234.000000 0')
37 
38  # statistics with default stamp
39  def test_statistics_3(self):
40  msg = Statistics()
41 
42  msg.header.stamp = rospy.Time()
43 
44  num_stats = 5
45  for i in range(num_stats):
46  stat = Statistic()
47  stat.value = i
48  stat.name = "test_stat_" + str(i)
49  msg.statistics.append(stat)
50 
51  result = self.cc.statistics_callback(msg)
52  output = result.splitlines()
53  self.assertEquals(len(output), num_stats)
54  for i in range(num_stats):
55  parts = output[i].split(' ')
56  self.assertEquals(parts[0], 'test_system.test_stat_' + str(i))
57  self.assertEquals(float(parts[1]), float(i))
58  self.assertEquals(parts[2], '0')
59 
60 if __name__ == '__main__':
61  import rosunit
62  rospy.init_node('test_node')
63  rosunit.unitrun('pal_carbon_collector', 'carbon_collector_test', CarbonCollectorTest)
64 


pal_carbon_collector
Author(s): Jordan Palacios
autogenerated on Mon Feb 28 2022 23:04:04