Commit 9654b7d8 authored by Charles Ferguson's avatar Charles Ferguson
Browse files

Add a few simple tests for the Timer.

The timer needs to be tested; might as well start doing something.
parent 664c6000
......@@ -155,6 +155,14 @@ class AlarmManager(object):
Alarm = AlarmManager()
class TimerError(Exception):
pass
class TimerCannotStartError(Exception):
pass
class Timer(object):
"""
Timer, optionally with timeout handler.
......@@ -212,7 +220,7 @@ class Timer(object):
self.idname = name or id(self)
self.name = name or '<unnamed>'
self.elapsed = 0
self._elapsed = 0
self.running = False
self.start_time = None
self.end_time = None
......@@ -222,12 +230,16 @@ class Timer(object):
self.exception = None
self.alarm = None
def __repr__(self):
elapsed = self.elapsed
@property
def elapsed(self):
elapsed = self._elapsed
if self.running:
elapsed += time.time() - self.start_time
return elapsed
def __repr__(self):
state = 'running' if self.running else 'waiting'
return "Timer(%s, %s, %.2f of %.2f elapsed)" % (self.name, state, elapsed, self.timeout_in_seconds)
return "Timer(%s, %s, %.2f of %.2f elapsed)" % (self.name, state, self.elapsed, self.timeout_in_seconds)
def __del__(self):
if getattr(self, 'alarm', None):
......@@ -244,7 +256,7 @@ class Timer(object):
def reset(self, timeout):
self.stop()
self.timeout_in_seconds = timeout
self.elapsed = 0
self._elapsed = 0
self.start()
def delay(self, extra_timeout):
......@@ -254,7 +266,7 @@ class Timer(object):
def start(self):
if self.running:
raise TypeError('Timer object is already running (cannot be started)')
raise TimerCannotStartError('Timer object is already running (cannot be started)')
print("Start Timer %s" % (self.name,))
self.start_time = time.time()
if self.timeout_in_seconds is not None:
......@@ -275,7 +287,7 @@ class Timer(object):
Alarm.clear(self.alarm)
self.alarm = None
self.end_time = time.time()
self.elapsed += self.end_time - self.start_time
self._elapsed += self.end_time - self.start_time
self.running = False
def __enter__(self):
......@@ -284,17 +296,3 @@ class Timer(object):
def __exit__(self, exc_class, exc, msg):
self.stop()
with Timer(2, name='Outer') as t1:
try:
for i in range(0, 12):
print("Iteration %s (t1=%s)" % (i, t1))
t2 = Timer(5, name='Inner')
try:
with t2:
time.sleep(10)
except t2.exception as ex:
print("Got a 't2' exception: %s" % (ex,))
except t1.exception as ex:
print("!!! Got a 't1' exception: %s" % (ex,))
#!/usr/bin/env python
"""
Test that our timer code works ok.
SUT: Test code framework
Area: Timer alert and alarms
Class: Functional
Type: Unit test
"""
from __future__ import print_function
import os
import sys
import time
import unittest
import nose
Timer = None
# Ensure that the Coverage module is configured to instrument the package we
# care about (and to play nice with other people)
if os.environ.get('NOSE_WITH_COVERAGE') or '--with-coverage' in sys.argv:
package = os.environ.get('NOSE_COVER_PACKAGE', '').split(',')
package = set(pkg for pkg in package if pkg is not '')
package.add('Timer')
os.environ['NOSE_COVER_PACKAGE'] = ",".join(sorted(package))
class Test01Import(unittest.TestCase):
def test_000_import(self):
global Timer
import Timer # pylint: disable=redefined-outer-name
class Test02Construct(unittest.TestCase):
def test_001_basic(self):
_ = Timer.Timer()
def test_002_arg_timeout(self):
_ = Timer.Timer(timeout_in_seconds=2)
def test_003_arg_timeout_unnamed(self):
_ = Timer.Timer(2)
def test_004_arg_handler(self):
_ = Timer.Timer(handler=lambda: False)
def test_005_arg_name(self):
_ = Timer.Timer(name='New name')
class Test10PlainTimer(unittest.TestCase):
def test_001_elapsed(self):
timer = Timer.Timer()
try:
self.assertEqual(timer.elapsed, 0, "The timer should be 0")
finally:
timer.stop()
def test_002_start(self):
timer = Timer.Timer()
try:
timer.start()
self.assertGreater(timer.elapsed, 0, "The timer should have counted some time")
finally:
timer.stop()
def test_003_stop(self):
timer = Timer.Timer()
try:
timer.start()
timer.stop()
self.assertGreater(timer.elapsed, 0, "The timer should have counted some time")
finally:
timer.stop()
def test_010_start_start(self):
timer = Timer.Timer()
try:
timer.start()
with self.assertRaises(Timer.TimerCannotStartError):
timer.start()
finally:
timer.stop()
class Test20Timeout(unittest.TestCase):
def test_001_simple(self):
timer = Timer.Timer(1)
try:
timer.start()
with self.assertRaises(Timer.AlarmError):
time.sleep(2)
finally:
timer.stop()
def test_002_no_timeout(self):
timer = Timer.Timer(1)
try:
timer.start()
time.sleep(0.5)
timer.stop()
finally:
timer.stop()
def test_003_add_delay(self):
timer = Timer.Timer(1)
try:
timer.start()
time.sleep(0.5)
timer.delay(2)
time.sleep(1)
timer.stop()
finally:
timer.stop()
if False:
with Timer.Timer(2, name='Outer') as t1:
try:
for i in range(0, 12):
print("Iteration %s (t1=%s)" % (i, t1))
t2 = Timer.Timer(5, name='Inner')
try:
with t2:
time.sleep(10)
except t2.exception as ex:
print("Got a 't2' exception: %s" % (ex,))
except t1.exception as ex:
print("!!! Got a 't1' exception: %s" % (ex,))
if __name__ == '__main__':
__name__ = os.path.basename(sys.argv[0][:-3]) # pylint: disable=redefined-builtin
env = os.environ
env['NOSE_WITH_XUNIT'] = '1'
exit(nose.runmodule(env=env))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment