Skip to content

Commit

Permalink
Bugfix to NotImplemented exceptions, and added setParent()
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-hammond-001 committed Jul 20, 2016
1 parent 77e7230 commit d4576d7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -79,6 +79,7 @@ Equivalent new way (0.4 and later):

#### Summary of changes:

* API addition: can setParent() on CorrelatedClock, RangeCorrelatedClock and TunableClock.
* API change: All clock objects modified to be able to track error values and calculate dispersions and clock `availability`
* API change: `CorrelatedClock` class modified to use a `Correlation` object instead of a tuple.
* API change: `TunableClock` reimplemented as subclass of `CorrelatedClock`
Expand Down
45 changes: 37 additions & 8 deletions dvbcss/clock.py
Expand Up @@ -400,7 +400,7 @@ def ticks(self):
|stub-method|
"""
raise NotImplemented
raise NotImplementedError()

@property
def speed(self):
Expand Down Expand Up @@ -436,7 +436,7 @@ def tickRate(self):
|stub-method|
"""
raise NotImplemented
raise NotImplementedError()

@property
def nanos(self):
Expand Down Expand Up @@ -520,7 +520,7 @@ def calcWhen(self, ticksWhen):
|stub-method|
"""
raise NotImplemented
raise NotImplementedError()

def getRoot(self):
"""\
Expand Down Expand Up @@ -633,7 +633,7 @@ def toParentTicks(self, ticks):
:throws StopIteration: if this clock has no parent
"""
raise NotImplemented
raise NotImplementedError()

def fromParentTicks(self, ticks):
"""\
Expand All @@ -647,15 +647,24 @@ def fromParentTicks(self, ticks):
:throws StopIteration: if this clock has no parent
"""
raise NotImplemented
raise NotImplementedError()

def getParent(self):
"""\
|stub-method|
:returns: :class:`ClockBase` representing the immediate parent of this clock, or None if it is a root clock.
"""
raise NotImplemented
raise NotImplementedError()

def setParent(self,newParent):
"""\
|stub-method|
Change the parent of this clock (if supported). Will generate a notification of
change.
"""
raise NotImplementedError()

def clockDiff(self, otherClock):
"""\
Expand Down Expand Up @@ -717,7 +726,7 @@ def _errorAtTime(self, t):
.. versionadded:: 0.4
"""
raise NotImplemented
raise NotImplementedError()

def getRootMaxFreqError(self):
"""\
Expand All @@ -734,7 +743,7 @@ def getRootMaxFreqError(self):
"""
root = self.getRoot()
if root == self:
raise NotImplemented
raise NotImplementedError()
else:
return self.getRoot().getRootMaxFreqError()

Expand Down Expand Up @@ -1112,6 +1121,16 @@ def fromParentTicks(self, ticks):

def getParent(self):
return self._parent

def setParent(self,newParent):
if self._parent != newParent:
if self._parent:
self._parent.unbind(self)
self._parent = None
self._parent = newParent
if self._parent:
self._parent.bind(self)
self.notify(self)

def quantifyChange(self, newCorrelation, newSpeed):
"""\
Expand Down Expand Up @@ -1357,6 +1376,16 @@ def fromParentTicks(self, ticks):
def getParent(self):
return self._parent

def setParent(self,newParent):
if self._parent != newParent:
if self._parent:
self._parent.unbind(self)
self._parent = None
self._parent = newParent
if self._parent:
self._parent.bind(self)
self.notify(self)

def _errorAtTime(self, t):
pt = self.toParentTicks(t)
deltaSecs1 = (pt - self._correlation1.parentTicks) / self._parent.tickRate
Expand Down
55 changes: 55 additions & 0 deletions test/test_Clock.py
Expand Up @@ -103,6 +103,11 @@ def test_dispersion(self):
newNow = now + sysClock.tickRate * 1000
self.assertAlmostEquals(0.000001, sysClock.dispersionAtTime(newNow), delta=0.0000001)

def test_cannotSetParent(self):
sysClock = self.newSysClock(tickRate=1000000000)
sysClock2 = self.newSysClock(tickRate=1000000000)
self.assertRaises(NotImplementedError, lambda : sysClock.setParent(sysClock2))

class Test_ClockBase(unittest.TestCase):

def test_dependents(self):
Expand Down Expand Up @@ -437,6 +442,31 @@ def test_quantifyChange(self):
b.speed = 0.0
self.assertEquals(0.005, b.quantifyChange( Correlation(0, 5), 0.0))

def test_setParent(self):
a = self.newSysClock(tickRate=1000)
b = CorrelatedClock(a, 1000, correlation=Correlation(0,0))
c = CorrelatedClock(a, 1000, correlation=Correlation(10,0))
d = MockDependent()
b.bind(d)

d.assertNotNotified()
b.setParent(c)
d.assertNotificationsEqual([b])
self.assertEquals(b.getParent(), c)

def test_setParentNoChangeNoNotify(self):
a = self.newSysClock(tickRate=1000)
b = CorrelatedClock(a, 1000, correlation=Correlation(0,0))
c = CorrelatedClock(a, 1000, correlation=Correlation(10,0))
d = MockDependent()
b.bind(d)

d.assertNotNotified()
b.setParent(a)
d.assertNotNotified()
self.assertEquals(b.getParent(), a)


class Test_RangeCorrelatedClock(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -490,6 +520,31 @@ def test_withTuples(self):
self.assertEqual(Correlation(100,2000), c.correlation1)
self.assertEqual(Correlation(200,2110), c.correlation2)
self.assertEqual(c.ticks, 2110)

def test_setParent(self):
a = self.newSysClock(tickRate=1000)
b = RangeCorrelatedClock(a, 1000, correlation1=(100,1000), correlation2=(200,1110))
c = RangeCorrelatedClock(a, 1000, correlation1=(200,1000), correlation2=(300,1110))
d = MockDependent()
b.bind(d)

d.assertNotNotified()
b.setParent(c)
d.assertNotificationsEqual([b])
self.assertEquals(b.getParent(), c)

def test_setParentNoChangeNoNotify(self):
a = self.newSysClock(tickRate=1000)
b = RangeCorrelatedClock(a, 1000, correlation1=(100,1000), correlation2=(200,1110))
c = RangeCorrelatedClock(a, 1000, correlation1=(200,1000), correlation2=(300,1110))
d = MockDependent()
b.bind(d)

d.assertNotNotified()
b.setParent(a)
d.assertNotNotified()
self.assertEquals(b.getParent(), a)


class Test_TunableClock(unittest.TestCase):

Expand Down

0 comments on commit d4576d7

Please sign in to comment.