"""Test stopwatch.py""" import datetime import os localDir = os.path.dirname(__file__) import sys import threading import time import unittest import stopwatch class Thing(object): @stopwatch.autowatch() def f(self, a, b, c): time.sleep(0.2) for i in range(b): x = c() a += b return a thing = Thing() @stopwatch.autowatch() def g(x, y, z): time.sleep(0.2) for i in range(y): z() x += y return x h_sw = stopwatch.Stopwatch() def h(): h_sw.click() time.sleep(0.01) h_sw.click('sleep') for i in range(3): x = dict() h_sw.click('dict') h_sw.reset() a_sw = stopwatch.Stopwatch() def a(): for i in range(5): a_sw.click() a_sw.click(u'foo') if (i % 2): a_sw.click('urk') a_sw.click(u'bar') a_sw.reset() class Foo(object): @stopwatch.autowatch() def foo(self, a, b, c): time.sleep(0.2) for i in range(b): x = c() a += b return a class StopwatchTests(unittest.TestCase): def test_average(self): a_sw.clear() a_sw.columns = [] a() self.assertEqual(len(a_sw.average()), 3) def test_occasional_click_errors(self): # First, test errors when columns are not predefined. # The 'urk' clicks should be discarded silently. a_sw.clear() a_sw.columns = [] a() self.assertEqual(a_sw.columns, ['Point 0', 'foo', 'bar']) self.assertEqual(a_sw.errors.keys(), ['urk']) self.assertEqual([len(r) for r in a_sw.races], [3] * 5) def test_occasional_predefined_clicks(self): a_sw.clear() a_sw.columns = ['start', 'foo', 'urk', 'bar'] a() self.assertEqual(a_sw.columns, ['start', 'foo', 'urk', 'bar']) self.assertEqual(a_sw.errors, {}) self.assertEqual([len(r) for r in a_sw.races], [4] * 5) def test_csv_gen(self): h_sw.clear() for i in range(4): h() csv = list(h_sw.csv_gen(minimum=0.02)) self.assertEqual(csv[0], '"sleep","dict","Extra"' + os.linesep) for race, line in zip(h_sw.races, csv[1:]): self.assertEqual( line, ",".join(["%f" % (td.seconds + (td.microseconds / 1e6)) for td in race[1:] + [datetime.timedelta(0)] ]) + os.linesep ) class AutowatchTests(unittest.TestCase): def test_bare_func(self): for i in range(3): g(i, 10, dict) expected = [ "24:def g(x, y, z):", "25: time.sleep(0.2)", "26: for i in range(y):", "27: z()", "28: x += y", "29: return x"] self.assertEqual(g.watch.columns, expected) self.assertEqual([len(r) for r in g.watch.races], [6] * 3) def test_instancemethod(self): for i in range(3): thing.f(i, 300, dict) expected = [ "15: def f(self, a, b, c):", "16: time.sleep(0.2)", "17: for i in range(b):", "18: x = c()", "19: a += b", "20: return a"] self.assertEqual(Thing.f.watch.columns, expected) self.assertEqual([len(r) for r in Thing.f.watch.races], [6] * 3) def test_threaded_instancemethod(self): foo = Foo() ts = [threading.Thread(target=foo.foo, args=(i, 300, dict)) for i in range(10)] for t in ts: t.start() for t in ts: t.join() expected = [ "53: def foo(self, a, b, c):", "54: time.sleep(0.2)", "55: for i in range(b):", "56: x = c()", "57: a += b", "58: return a"] self.assertEqual(Foo.foo.watch.columns, expected) self.assertEqual([len(r) for r in Foo.foo.watch.races], [6] * 10) if __name__ == '__main__': unittest.main()