chiark / gitweb /
Python module now supports the same authorization hash functions as C.
[disorder] / tests / schedule.py
1 #! /usr/bin/env python
2 #
3 # This file is part of DisOrder.
4 # Copyright (C) 2008 Richard Kettlewell
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 import dtest,disorder,time,string
20
21 def now():
22     """Return the current time in whole seconds"""
23     return int(time.time())
24
25 def next_playing(c):
26     print " waiting for track to play"
27     p = c.playing()
28     waited = 0
29     while p is None and waited < 10:
30         time.sleep(1)
31         print "  ."
32         p = c.playing()
33     assert waited < 10, "track played in a reasonable time"
34     return p
35
36 def wait_idle(c):
37     print " waiting for nothing to be playing"
38     p = c.playing()
39     waited = 0
40     while p is not None and waited < 20:
41         time.sleep(1)
42         print "  ."
43         p = c.playing()
44     assert waited < 20, "idled in a reasonable time"
45
46 def test():
47     """Exercise schedule support"""
48     dtest.start_daemon()
49     dtest.create_user()
50     c = disorder.client()
51     c.random_disable()
52     dtest.rescan()
53     # Wait until there's no track playing
54     print " waiting for nothing to be playing"
55     while c.playing() is not None:
56         time.sleep(1)
57         print "  ."
58     track = "%s/Joe Bloggs/First Album/05:Fifth track.ogg" % dtest.tracks
59     print " scheduling a track for the future"
60     when = now() + 3
61     c.schedule_add(when, "normal", "play", track)
62     print " disorder schedule-list output:"
63     print string.join(dtest.command(["disorder",
64                                      "--config", disorder._configfile,
65                                      "--no-per-user-config",
66                                      "schedule-list"]), ""),
67     p = next_playing(c)
68     assert p["track"] == track, "checking right track played"
69     assert int(p["when"]) >= when, "checking track played at right time"
70     assert c.schedule_list() == [], "checking schedule is empty"
71     wait_idle(c)
72     print " scheduling an enable-random for the future"
73     c.schedule_add(now() + 3, "junk", "set-global", "random-play", "yes")
74     print " disorder schedule-list output:"
75     print string.join(dtest.command(["disorder",
76                                      "--config", disorder._configfile,
77                                      "--no-per-user-config",
78                                      "schedule-list"]), ""),
79     next_playing(c)
80     print " disabling random play"
81     c.random_disable()
82     wait_idle(c)
83     print " scheduling track to play later via command line"
84     when = now() + 3
85     dtest.command(["disorder",
86                    "--config", disorder._configfile,
87                    "--no-per-user-config",
88                    "schedule-play",
89                    time.strftime("%Y-%m-%d %H:%M:%S",
90                                  time.localtime(when)),
91                    "normal",
92                    track])
93     print " disorder schedule-list output:"
94     print string.join(dtest.command(["disorder",
95                                      "--config", disorder._configfile,
96                                      "--no-per-user-config",
97                                      "schedule-list"]), ""),
98     p = next_playing(c)
99     assert p["track"] == track, "checking right track played"
100     assert p["when"] >= when, "checking track played at right time"
101     assert c.schedule_list() == [], "checking schedule is empty"
102     wait_idle(c)
103     print " scheduling an enable-random for later via command line"
104     dtest.command(["disorder",
105                    "--config", disorder._configfile,
106                    "--no-per-user-config",
107                    "schedule-set-global",
108                    time.strftime("%Y-%m-%d %H:%M:%S",
109                                  time.localtime(now() + 3)),
110                    "normal",
111                    "random-play",
112                    "yes"])
113     print " disorder schedule-list output:"
114     print string.join(dtest.command(["disorder",
115                                      "--config", disorder._configfile,
116                                      "--no-per-user-config",
117                                      "schedule-list"]), ""),
118     p = next_playing(c)
119     print " disabling random play"
120     c.random_disable()
121     print " waiting for nothing to be playing"
122     while c.playing() is not None:
123         time.sleep(1)
124         print "  ."
125     print " scheduling a track for the future"
126     c.schedule_add(now() + 3, "normal", "play", track)
127     print " schedule via python:"
128     s = c.schedule_list()
129     for event in s:
130         e = c.schedule_get(event)
131         print "item %s: %s" % (event, e)
132     print " deleting item %s" % s[0]
133     c.schedule_del(s[0])
134     print " checking it's really gone"
135     s = c.schedule_list()
136     assert s == [], "checking schedule is empty"
137     waited = 0
138     p = c.playing()
139     while p is None and waited < 5:
140         time.sleep(1)
141         print "  ."
142         waited += 1
143         p = c.playing()
144     assert p is None, "checking deleted scheduled event did not run"
145     print " checking you can't schedule events for the past"
146     try:
147         c.schedule_add(now() - 4, "normal", "play", track)
148         assert False, "checking schedule_add failed"
149     except disorder.operationError:
150       pass                              # good
151     print " checking scheduled events survive restarts"
152     when = now() + 3
153     c.schedule_add(when, "normal", "play", track)
154     dtest.stop_daemon()
155     print " dumping database"
156     dump = "%s/dumpfile" % dtest.testroot
157     print dtest.command(["disorder-dump", "--config", disorder._configfile,
158                          "--dump", dump])
159     print "restoring database"
160     print dtest.command(["disorder-dump", "--config", disorder._configfile,
161                          "--undump", dump])
162     dtest.start_daemon()
163     c = disorder.client()
164     p = next_playing(c)
165     print " waiting for track to play"
166     assert p["track"] == track, "checking right track played"
167     assert p["when"] >= when, "checking track played at right time"
168     assert c.schedule_list() == [], "checking schedule is empty"
169     print " checking junk events do not survive restarts"
170     c.schedule_add(now() + 2, "junk", "play", track)
171     s = c.schedule_list()
172     print s
173     dtest.stop_daemon()
174     time.sleep(3)
175     dtest.start_daemon()
176     c = disorder.client()
177     print " checking schedule is empty"
178     s = c.schedule_list()
179     print s
180     assert s == [], "checking schedule is empty"
181
182 if __name__ == '__main__':
183     dtest.run()