Skip to content

Commit 98c3f3c

Browse files
author
Charles Weir
committed
cw: Got all tests running correctly.
1 parent e649184 commit 98c3f3c

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

BrickPython/Coroutine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def call(self, param = None):
6262
self.callerSemaphore.acquire()
6363
if self.stopEvent.is_set():
6464
self.join() # Ensure that is_alive is false on exit.
65+
# For testing - assertions within coroutines must be passed back to main thread.
66+
if self.lastExceptionCaught != None and isinstance(self.lastExceptionCaught, AssertionError):
67+
raise self.lastExceptionCaught
6568
return self.callResult
6669

6770
def stop(self):

test/TestCoroutine.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ def dummyCoroutineFuncThatDoesCleanup():
3737

3838
def setUp(self):
3939
TestCoroutine.coroutineCalls = []
40+
TestCoroutine.oldCurrentTimeMillis = Coroutine.currentTimeMillis
41+
Coroutine.currentTimeMillis = Mock(side_effect = [1,10,500,1200])
4042

4143
def tearDown(self):
42-
pass
44+
Coroutine.currentTimeMillis = TestCoroutine.oldCurrentTimeMillis
4345

4446
def testCoroutinesGetCalledUntilDone(self):
4547
# When we start a coroutine
@@ -88,8 +90,6 @@ def dummyCoroutineFuncWaiting1Sec():
8890
Coroutine.waitMilliseconds(1000)
8991

9092
coroutine = Coroutine(dummyCoroutineFuncWaiting1Sec)
91-
# Doesn't matter not restoring this; tests never use real time:
92-
Coroutine.currentTimeMillis = Mock(side_effect = [1,10,500,1200])
9393
for i in range(1,3):
9494
coroutine.call()
9595
self.assertTrue(coroutine.is_alive(),"Coroutine dead at call %d" % i)
@@ -132,7 +132,6 @@ def testRunCoroutinesUntilAllComplete(self):
132132
self.assertEquals(TestCoroutine.coroutineCalls, [1,1,2,2,3,4,5])
133133

134134
def testWithTimeout(self):
135-
Coroutine.currentTimeMillis = Mock(side_effect = [1,10,500,1200])
136135
coroutine = Coroutine(TestCoroutine.dummyCoroutineFunc,1,20).withTimeout(1000)
137136
for i in range(1,20):
138137
coroutine.call()

test/TestMotor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
class TestMotor(unittest.TestCase):
1313
''' Tests for the Motor class, especially for PID Servo Motor functionality'''
1414
def setUp(self):
15+
self.saveTime = Scheduler.currentTimeMillis
16+
Scheduler.currentTimeMillis = Mock(side_effect = xrange(1,10000))
1517
self.bp = BrickPiWrapper()
1618
motor = self.motor = self.bp.motor( 'A' )
1719
#motor.position = Mock()
1820
motor.timeMillis = Mock()
1921
motor.timeMillis.side_effect = range(0,9999)
2022

23+
def tearDown(self):
24+
Scheduler.currentTimeMillis = self.saveTime
25+
unittest.TestCase.tearDown(self)
26+
2127
def testZeroPosition( self ):
2228
motor = self.motor
2329
# When the motor is zeroed at absolute position 1

test/TestScheduler.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def dummyCoroutineThatTakesTime():
4343

4444
@staticmethod
4545
def dummyCoroutineThatThrowsException():
46-
raise Exception()
46+
raise Exception("Hello")
4747
yield
4848

4949
@staticmethod
@@ -58,9 +58,13 @@ def checkCoroutineFinished(coroutine):
5858

5959
def setUp(self):
6060
TestScheduler.coroutineCalls = []
61-
self.scheduler = Scheduler()
62-
# Don't need to replace this afterward - tests never use real time.
61+
TestScheduler.oldCurrentTimeMillis = Scheduler.currentTimeMillis
6362
Scheduler.currentTimeMillis = Mock( side_effect = xrange(0,10000) ) # Each call answers the next integer
63+
self.scheduler = Scheduler()
64+
65+
def tearDown(self):
66+
Scheduler.currentTimeMillis = TestScheduler.oldCurrentTimeMillis
67+
6468

6569
def testCoroutinesGetCalledUntilDone(self):
6670
# When we start a motor coroutine
@@ -162,7 +166,8 @@ def testWaitMilliseconds(self):
162166
for i in self.scheduler.waitMilliseconds(10):
163167
pass
164168
# that's about the time that will have passed:
165-
assert( self.scheduler.currentTimeMillis() in range(10,12) )
169+
timeNow = self.scheduler.currentTimeMillis()
170+
assert( timeNow in range(10,12), "Wrong time " )
166171

167172
def testRunTillFirstCompletes(self):
168173
# When we run three coroutines using runTillFirstCompletes:
@@ -224,12 +229,12 @@ def timeCheckerCoroutine(self):
224229
while True:
225230
yield
226231
newTime = Scheduler.currentTimeMillis()
227-
self.assertNotEquals( newTime, time )
232+
self.assertNotEquals( newTime, time, "Time same for two scheduler calls" )
228233
time = newTime
229234

230235
def testEachCallToACoroutineGetsADifferentTime(self):
231-
scheduler = Scheduler()
232236
Scheduler.currentTimeMillis = Mock( side_effect = [0,0,0,0,0,0,0,0,0,0,1,2,3,4,5] )
237+
scheduler = Scheduler()
233238
# For any coroutine,
234239
scheduler.setUpdateCoroutine( self.timeCheckerCoroutine() )
235240
# We can guarantee that the timer always increments between calls (for speed calculations etc).

0 commit comments

Comments
 (0)