# Module:   test_timers
# Date:     10th February 2010
# Author:   James Mills, prologic at shortcircuit dot net dot au

"""Timers Tests"""

from time import sleep
from datetime import datetime, timedelta

from circuits import Event, Component, Timer

class Test(Event):
    """Test Event"""

class App(Component):

    flag = False

    def timer(self):
        self.flag = True

app = App()
app.start()

def test_timer():
    timer = Timer(0.1, Test(), "timer")
    timer.register(app)
    sleep(0.2)
    assert app.flag

def test_persistentTimer():
    timer = Timer(0.1, Test(), "timer", persist=True)
    timer.register(app)

    for i in xrange(2):
        sleep(0.2)
        assert app.flag
        app.flag = False

    timer.unregister()

def test_datetime():
    now = datetime.now()
    d = now + timedelta(seconds=1)
    timer = Timer(d, Test(), "timer")
    timer.register(app)
    sleep(1)
    assert app.flag

@@ -492,24 +492,44 @@
  sleep(0.2)%0A
+    flag = app.flag%0A
     assert a
@@ -523,28 +523,24 @@
 %0A    assert 
-app.
 flag%0A%0Adef te
@@ -679,24 +679,48 @@
  sleep(0.2)%0A
+        flag = app.flag%0A
         asse
@@ -718,28 +718,24 @@
      assert 
-app.
 flag%0A       
@@ -933,16 +933,36 @@
 leep(1)%0A
+    flag = app.flag%0A
     asse
@@ -964,17 +964,13 @@
  assert 
-app.
 flag%0A

Fix possible assertion failures (that pass on re-evaluation)