Ejemplo n.º 1
0
    def test_speedChangePropagates(self):
        mockTime = self.mockTime

        a = self.newSysClock(tickRate=1000)

        mockTime.timeNow = 5

        a1 = CorrelatedClock(a, tickRate=1000, correlation=Correlation(50, 0))
        a2 = CorrelatedClock(a1,
                             tickRate=100,
                             correlation=Correlation(28, 999))
        a3 = CorrelatedClock(a2, tickRate=50, correlation=Correlation(5, 1003))
        a4 = CorrelatedClock(a3, tickRate=25, correlation=Correlation(1000, 9))
        b3 = CorrelatedClock(a2,
                             tickRate=1000,
                             correlation=Correlation(500, 20))
        b4 = CorrelatedClock(b3,
                             tickRate=2000,
                             correlation=Correlation(15, 90))

        at1, a1t1, a2t1, a3t1, a4t1, b3t1, b4t1 = [
            x.ticks for x in [a, a1, a2, a3, a4, b3, b4]
        ]
        a3.speed = 0.5
        a4.speed = 0.2
        self.assertEquals(1.0, a.getEffectiveSpeed())
        self.assertEquals(1.0, a1.getEffectiveSpeed())
        self.assertEquals(1.0, a2.getEffectiveSpeed())
        self.assertEquals(0.5, a3.getEffectiveSpeed())
        self.assertEquals(0.1, a4.getEffectiveSpeed())

        a3.speed = 0
        a4.speed = 1.0
        self.assertEquals(1.0, a.getEffectiveSpeed())
        self.assertEquals(1.0, a1.getEffectiveSpeed())
        self.assertEquals(1.0, a2.getEffectiveSpeed())
        self.assertEquals(0.0, a3.getEffectiveSpeed())
        self.assertEquals(0.0, a4.getEffectiveSpeed())

        mockTime.timeNow = 6  # advance time 1 second

        at2, a1t2, a2t2, a3t2, a4t2, b3t2, b4t2 = [
            x.ticks for x in [a, a1, a2, a3, a4, b3, b4]
        ]

        self.assertEquals(at2, at1 + 1000)  # a  still advances
        self.assertEquals(a1t2, a1t1 + 1000)  # a1 still advances
        self.assertEquals(a2t2, a2t1 + 100)  # a2 still advances
        self.assertEquals(
            a3t2, 1003)  # a3 is speed zero, is now at the correlation point
        self.assertEquals(
            a4t2, 10.5
        )  # a4 is speed zero, a3.ticks is 3 ticks from correlation point for a4, translating to 1.5 ticks from a4 correlation point at its tickRate
        self.assertEquals(b3t2, b3t1 + 1000)  # b3 still advances
        self.assertEquals(b4t2, b4t1 + 2000)  # b4 is paused
Ejemplo n.º 2
0
    def test_speedChangePropagates(self):
        mockTime = self.mockTime
        
        mockTime.timeNow = 5
        
        a = SysClock(tickRate=1000)
        a1 = CorrelatedClock(a, tickRate=1000, correlation=(50,0))
        a2 = CorrelatedClock(a1, tickRate=100, correlation=(28,999))
        a3 = CorrelatedClock(a2, tickRate=50, correlation=(5,1003))
        a4 = CorrelatedClock(a3, tickRate=25, correlation=(1000,9))
        b3 = CorrelatedClock(a2, tickRate=1000, correlation=(500,20))
        b4 = CorrelatedClock(b3, tickRate=2000, correlation=(15,90))
        
        at1, a1t1, a2t1, a3t1, a4t1, b3t1, b4t1 = [x.ticks for x in [a,a1,a2,a3,a4,b3,b4]]
        a3.speed = 0.5
        a4.speed = 0.2
        self.assertEquals(1.0, a.getEffectiveSpeed())
        self.assertEquals(1.0, a1.getEffectiveSpeed())
        self.assertEquals(1.0, a2.getEffectiveSpeed())
        self.assertEquals(0.5, a3.getEffectiveSpeed())
        self.assertEquals(0.1, a4.getEffectiveSpeed())


        a3.speed = 0
        a4.speed = 1.0
        self.assertEquals(1.0, a.getEffectiveSpeed())
        self.assertEquals(1.0, a1.getEffectiveSpeed())
        self.assertEquals(1.0, a2.getEffectiveSpeed())
        self.assertEquals(0.0, a3.getEffectiveSpeed())
        self.assertEquals(0.0, a4.getEffectiveSpeed())

        mockTime.timeNow = 6 # advance time 1 second
        
        at2, a1t2, a2t2, a3t2, a4t2, b3t2, b4t2 = [x.ticks for x in [a,a1,a2,a3,a4,b3,b4]]
        
        self.assertEquals(at2,  at1 + 1000)  # a  still advances
        self.assertEquals(a1t2, a1t1 + 1000) # a1 still advances
        self.assertEquals(a2t2, a2t1 + 100)  # a2 still advances
        self.assertEquals(a3t2, 1003)        # a3 is speed zero, is now at the correlation point
        self.assertEquals(a4t2, 10.5)        # a4 is speed zero, a3.ticks is 3 ticks from correlation point for a4, translating to 1.5 ticks from a4 correlation point at its tickRate
        self.assertEquals(b3t2, b3t1 + 1000) # b3 still advances
        self.assertEquals(b4t2, b4t1 + 2000) # b4 is paused
Ejemplo n.º 3
0
class Test_OffsetClock(unittest.TestCase):
    """\
    Tests for OffsetClock
    """
    def setUp(self):
        self.root = SysClock(
        )  # init before installing mock, so it can do its precision measurement while time still flows
        self.mockTime = MockTime()
        self.mockTime.install()
        self.parent = CorrelatedClock(parentClock=self.root, tickRate=1000)
        self.altParent = CorrelatedClock(parentClock=self.root, tickRate=50)

    def tearDown(self):
        self.mockTime.uninstall()

    def test_speedAlways1(self):
        oc = OffsetClock(parentClock=self.parent)
        self.assertEquals(oc.speed, 1)
        self.parent.speed = 2.7
        self.assertEquals(oc.speed, 1)

    def test_effectiveSpeedSameAsParents(self):
        oc = OffsetClock(parentClock=self.parent)
        self.assertEquals(oc.getEffectiveSpeed(),
                          self.parent.getEffectiveSpeed())
        self.parent.speed = 2.7
        self.assertEquals(oc.getEffectiveSpeed(),
                          self.parent.getEffectiveSpeed())

    def test_inheritsTickRateFromParent(self):
        oc = OffsetClock(parentClock=self.parent)
        self.assertEquals(oc.tickRate, self.parent.tickRate)
        self.parent.tickRate = 25
        self.assertEquals(oc.tickRate, self.parent.tickRate)

    def test_offsetAppliedAtSpeed1(self):
        OC_AHEAD_BY = 0.050
        oc = OffsetClock(parentClock=self.parent, offset=OC_AHEAD_BY)
        self.parent.speed = 1
        t = oc.ticks
        # advance time and see if OffsetClock was indeed ahead by OC_AHEAD_BY seconds
        self.mockTime.timeNow = self.mockTime.timeNow + OC_AHEAD_BY
        t2 = self.parent.ticks
        self.assertEquals(t, t2)

    def test_offsetAppliedAtSpeed0(self):
        OC_AHEAD_BY = 0.098
        oc = OffsetClock(parentClock=self.parent, offset=OC_AHEAD_BY)
        self.parent.speed = 0
        t = oc.ticks
        # advance time and see if OffsetClock was indeed ahead by OC_AHEAD_BY seconds
        self.mockTime.timeNow = self.mockTime.timeNow + OC_AHEAD_BY
        t2 = self.parent.ticks
        self.assertEquals(t, t2)

    def test_offsetAppliedAtSpeedGreaterThan1(self):
        OC_AHEAD_BY = 0.02
        oc = OffsetClock(parentClock=self.parent, offset=OC_AHEAD_BY)
        self.parent.speed = 2
        t = oc.ticks
        # advance time and see if OffsetClock was indeed ahead by OC_AHEAD_BY seconds
        self.mockTime.timeNow = self.mockTime.timeNow + OC_AHEAD_BY
        t2 = self.parent.ticks
        self.assertEquals(t, t2)

    def test_noOffsetWorks(self):
        oc = OffsetClock(parentClock=self.parent, offset=0)
        self.assertEquals(oc.ticks, self.parent.ticks)

    def test_offsetChangeNewOffsetUsed(self):
        oc = OffsetClock(parentClock=self.parent, offset=0.040)
        self.assertEquals(oc.ticks, self.parent.ticks + 40)
        oc.offset = 0.065
        self.assertEquals(oc.ticks, self.parent.ticks + 65)

    def test_parentChangedOffsetStillApplied(self):
        oc = OffsetClock(parentClock=self.parent, offset=0.040)
        self.assertEquals(oc.getParent(), self.parent)
        self.assertEquals(oc.ticks, self.parent.ticks + 40)
        oc.setParent(self.altParent)
        self.assertEquals(oc.getParent(), self.altParent)
        self.assertEquals(oc.ticks, self.altParent.ticks + 2)

    def test_parentChangeCausesNotification(self):
        oc = OffsetClock(parentClock=self.parent, offset=0.040)
        dep = MockDependent()
        oc.bind(dep)
        dep.assertNotNotified()
        oc.setParent(self.altParent)
        dep.assertNotificationsEqual([oc])

    def test_negativeOffsetWorks(self):
        OC_BEHIND_BY = 0.050
        oc = OffsetClock(parentClock=self.parent, offset=-OC_BEHIND_BY)
        self.parent.speed = 1
        t = oc.ticks
        # regress time and see if OffsetClock was indeed behind by OC_BEHIND_BY seconds
        self.mockTime.timeNow = self.mockTime.timeNow - OC_BEHIND_BY
        t2 = self.parent.ticks
        self.assertEquals(t, t2)

    def test_toRootTicks(self):
        OC_AHEAD_BY = 0.124
        oc = OffsetClock(parentClock=self.parent, offset=OC_AHEAD_BY)
        t = 1285.2
        rt = oc.toRootTicks(t)
        rt2 = self.parent.toRootTicks(t)
        self.assertEquals(rt + OC_AHEAD_BY * self.root.tickRate, rt2)

    def test_fromRootTicks(self):
        OC_AHEAD_BY = 0.124
        oc = OffsetClock(parentClock=self.parent, offset=OC_AHEAD_BY)
        rt = 22849128
        t = oc.fromRootTicks(rt)
        t2 = self.parent.fromRootTicks(rt + OC_AHEAD_BY * self.root.tickRate)
        self.assertEquals(t, t2)