1 # Written by Njaal Borch
2 # see LICENSE.txt for license information
8 from BaseLib.Core.Statistics.Status import Status
9 from BaseLib.Core.Statistics.Status import LivingLabReporter
11 class TestOnChangeStatusReporter(Status.OnChangeStatusReporter):
16 def report(self, element):
17 self.name = element.name
18 self.value = element.value
20 class TestPeriodicStatusReporter(Status.PeriodicStatusReporter):
24 elements = self.get_elements()
26 assert len(elements) == 1
27 self.last_value = elements[0].get_value()
29 class StatusTest(unittest.TestCase):
31 Unit tests for the Status class
43 status = Status.get_status_holder("UnitTest")
46 self.assertNotEqual(status, None)
48 self.assertEquals(status.get_name(), "UnitTest")
52 status = Status.get_status_holder("UnitTest")
54 self.assertNotEqual(status, None)
56 i = status.create_status_element("TestInteger")
57 self.assertEquals(i.get_name(), "TestInteger")
59 x = status.get_status_element("TestInteger")
60 self.assertEquals(x, i)
62 # Test set and get values
65 self.assertEquals(i.get_value(), j)
68 status.remove_status_element(i)
70 status.get_status_element("TestInteger")
71 self.fail("Remove does not remove status element 'TestInteger'")
72 except Status.NoSuchElementException, e:
76 def testInvalid(self):
77 status = Status.get_status_holder("UnitTest")
81 i = status.create_status_element(None)
82 self.fail("Does not throw exception with no name")
83 except AssertionError, e:
87 status.get_status_element(None)
88 self.fail("Invalid get_status_element does not throw exception")
89 except AssertionError,e:
93 status.remove_status_element(None)
94 self.fail("Invalid remove_status_element does not throw exception")
95 except AssertionError,e:
98 elem = Status.StatusElement("name", "description")
100 status.remove_status_element(elem)
101 self.fail("Invalid remove_status_element does not throw exception")
102 except Status.NoSuchElementException,e:
106 def testPolicy_ON_CHANGE(self):
108 status = Status.get_status_holder("UnitTest")
110 reporter = TestOnChangeStatusReporter("On change")
111 status.add_reporter(reporter)
112 i = status.create_status_element("TestInteger")
114 for x in range(0, 10):
116 if x != reporter.value:
117 self.fail("Callback does not work for ON_CHANGE policy")
118 if reporter.name != "TestInteger":
119 self.fail("On_Change callback get's the wrong parameter, got '%s', expected 'TestInteger'"%reporter.name)
122 status.remove_status_element(i)
125 def testPolicy_PERIODIC(self):
127 status = Status.get_status_holder("UnitTest")
130 reporter = TestPeriodicStatusReporter("Periodic, 0.4sec", 0.4)
131 status.add_reporter(reporter)
132 i = status.create_status_element("TestInteger")
134 for x in range(0, 5):
136 self.assertEquals(reporter.last_value, None) # Not updated yet
140 assert reporter.last_value == 4
142 for x in range(5, 9):
143 self.assertEquals(reporter.last_value, 4) # Not updated yet
147 self.assertEquals(reporter.last_value, 8)
150 status.remove_status_element(i)
154 def test_LLReporter_element(self):
156 status = Status.get_status_holder("UnitTest")
158 reporter = TestLivingLabPeriodicReporter("Living lab test reporter", 1.0)
159 status.add_reporter(reporter)
160 i = status.create_status_element("TestInteger")
163 b = status.create_status_element("Binary")
164 b.set_value("".join([chr(n) for n in range(0, 255)]))
166 reporter.wait_for_post(5.0)
171 self.assertEquals(len(reporter.get_errors()), 0)
173 status.remove_status_element(i)
174 status.remove_status_element(b)
176 def test_LLReporter_event(self):
178 status = Status.get_status_holder("UnitTest")
180 reporter = TestLivingLabPeriodicReporter("Living lab test reporter", 1.0)
181 status.add_reporter(reporter)
182 event = status.create_event("SomeEvent")
183 event.add_value("123")
184 event.add_value("456")
185 status.add_event(event)
187 reporter.wait_for_post(5.0)
192 self.assertEquals(len(reporter.get_errors()), 0)
194 status.remove_event(event)
196 class TestLivingLabPeriodicReporter(LivingLabReporter.LivingLabPeriodicReporter):
198 def __init__(self, name, report_time):
200 LivingLabReporter.LivingLabPeriodicReporter.__init__(self, name, report_time, "myid", self.count_errors)
202 self.cond = threading.Condition()
204 def wait_for_post(self, timeout):
210 self.cond.wait(timeout)
213 raise Exception("Timeout")
219 # TODO: Check the XML?
223 self.cond.notifyAll()
226 def count_errors(self, zero, error):
228 self.errors.append(error)
230 def get_errors(self):
233 if __name__ == "__main__":
235 print "Testing Status module"