1###
2# Copyright (c) 2002-2005, Jeremiah Fincher
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8#   * Redistributions of source code must retain the above copyright notice,
9#     this list of conditions, and the following disclaimer.
10#   * Redistributions in binary form must reproduce the above copyright notice,
11#     this list of conditions, and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#   * Neither the name of the author of this software nor the name of
14#     contributors to this software may be used to endorse or promote products
15#     derived from this software without specific prior written consent.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28###
29
30from supybot.test import *
31
32import time
33
34import supybot.schedule as schedule
35
36class TestSchedule(SupyTestCase):
37    def testSchedule(self):
38        sched = schedule.Schedule()
39        i = [0]
40        def add10():
41            i[0] = i[0] + 10
42        def add1():
43            i[0] = i[0] + 1
44
45        sched.addEvent(add10, time.time() + 3)
46        sched.addEvent(add1, time.time() + 1)
47        time.sleep(1.2)
48        sched.run()
49        self.assertEqual(i[0], 1)
50        time.sleep(1.9)
51        sched.run()
52        self.assertEqual(i[0], 11)
53
54        sched.addEvent(add10, time.time() + 3, 'test')
55        sched.run()
56        self.assertEqual(i[0], 11)
57        sched.removeEvent('test')
58        self.assertEqual(i[0], 11)
59        time.sleep(3)
60        self.assertEqual(i[0], 11)
61
62    def testReschedule(self):
63        sched = schedule.Schedule()
64        i = [0]
65        def inc():
66            i[0] += 1
67        n = sched.addEvent(inc, time.time() + 1)
68        sched.rescheduleEvent(n, time.time() + 3)
69        time.sleep(1.2)
70        sched.run()
71        self.assertEqual(i[0], 0)
72        time.sleep(2)
73        sched.run()
74        self.assertEqual(i[0], 1)
75
76    def testPeriodic(self):
77        sched = schedule.Schedule()
78        i = [0]
79        def inc():
80            i[0] += 1
81        n = sched.addPeriodicEvent(inc, 1, name='test_periodic')
82        time.sleep(0.6)
83        sched.run() # 0.6
84        self.assertEqual(i[0], 1)
85        time.sleep(0.6)
86        sched.run() # 1.2
87        self.assertEqual(i[0], 2)
88        time.sleep(0.6)
89        sched.run() # 1.8
90        self.assertEqual(i[0], 2)
91        time.sleep(0.6)
92        sched.run() # 2.4
93        self.assertEqual(i[0], 3)
94        sched.removePeriodicEvent(n)
95        time.sleep(1)
96        sched.run() # 3.4
97        self.assertEqual(i[0], 3)
98
99    def testCountedPeriodic(self):
100        sched = schedule.Schedule()
101        i = [0]
102        def inc():
103            i[0] += 1
104        n = sched.addPeriodicEvent(inc, 1, name='test_periodic', count=3)
105        time.sleep(0.6)
106        sched.run() # 0.6
107        self.assertEqual(i[0], 1)
108        time.sleep(0.6)
109        sched.run() # 1.2
110        self.assertEqual(i[0], 2)
111        time.sleep(0.6)
112        sched.run() # 1.8
113        self.assertEqual(i[0], 2)
114        time.sleep(0.6)
115        sched.run() # 2.4
116        self.assertEqual(i[0], 3)
117        time.sleep(1)
118        sched.run() # 3.4
119        self.assertEqual(i[0], 3)
120
121
122# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
123
124