chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / tests / schedule.py
CommitLineData
17360d2f
RK
1#! /usr/bin/env python
2#
3# This file is part of DisOrder.
4778e044 4# Copyright (C) 2008, 2009 Richard Kettlewell
17360d2f 5#
e7eb3a27 6# This program is free software: you can redistribute it and/or modify
17360d2f 7# it under the terms of the GNU General Public License as published by
e7eb3a27 8# the Free Software Foundation, either version 3 of the License, or
17360d2f
RK
9# (at your option) any later version.
10#
e7eb3a27
RK
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#
17360d2f 16# You should have received a copy of the GNU General Public License
e7eb3a27 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
17360d2f 18#
758aa6c3 19import dtest,disorder,time,string
17360d2f 20
5dc143a4
RK
21def now():
22 """Return the current time in whole seconds"""
23 return int(time.time())
24
25def 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
36def 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
17360d2f
RK
46def 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)
067eeb5f 57 print " ."
17360d2f
RK
58 track = "%s/Joe Bloggs/First Album/05:Fifth track.ogg" % dtest.tracks
59 print " scheduling a track for the future"
5dc143a4
RK
60 when = now() + 3
61 c.schedule_add(when, "normal", "play", track)
758aa6c3
RK
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"]), ""),
5dc143a4 67 p = next_playing(c)
17360d2f 68 assert p["track"] == track, "checking right track played"
ce09e249 69 print " when=%d expected at least %d" % (int(p["when"]), when)
5dc143a4 70 assert int(p["when"]) >= when, "checking track played at right time"
6c13a317 71 assert c.schedule_list() == [], "checking schedule is empty"
5dc143a4 72 wait_idle(c)
17360d2f 73 print " scheduling an enable-random for the future"
5dc143a4 74 c.schedule_add(now() + 3, "junk", "set-global", "random-play", "yes")
758aa6c3
RK
75 print " disorder schedule-list output:"
76 print string.join(dtest.command(["disorder",
77 "--config", disorder._configfile,
78 "--no-per-user-config",
79 "schedule-list"]), ""),
5dc143a4 80 next_playing(c)
6c13a317
RK
81 print " disabling random play"
82 c.random_disable()
5dc143a4 83 wait_idle(c)
6c13a317 84 print " scheduling track to play later via command line"
5dc143a4 85 when = now() + 3
6c13a317
RK
86 dtest.command(["disorder",
87 "--config", disorder._configfile,
88 "--no-per-user-config",
89 "schedule-play",
d436bd52 90 time.strftime("%Y-%m-%d %H:%M:%S",
5dc143a4 91 time.localtime(when)),
6c13a317
RK
92 "normal",
93 track])
94 print " disorder schedule-list output:"
95 print string.join(dtest.command(["disorder",
96 "--config", disorder._configfile,
97 "--no-per-user-config",
98 "schedule-list"]), ""),
5dc143a4 99 p = next_playing(c)
6c13a317 100 assert p["track"] == track, "checking right track played"
5dc143a4 101 assert p["when"] >= when, "checking track played at right time"
6c13a317 102 assert c.schedule_list() == [], "checking schedule is empty"
5dc143a4 103 wait_idle(c)
6c13a317 104 print " scheduling an enable-random for later via command line"
6c13a317
RK
105 dtest.command(["disorder",
106 "--config", disorder._configfile,
107 "--no-per-user-config",
108 "schedule-set-global",
d436bd52 109 time.strftime("%Y-%m-%d %H:%M:%S",
5dc143a4 110 time.localtime(now() + 3)),
6c13a317
RK
111 "normal",
112 "random-play",
113 "yes"])
114 print " disorder schedule-list output:"
115 print string.join(dtest.command(["disorder",
116 "--config", disorder._configfile,
117 "--no-per-user-config",
118 "schedule-list"]), ""),
5dc143a4 119 p = next_playing(c)
6c13a317
RK
120 print " disabling random play"
121 c.random_disable()
122 print " waiting for nothing to be playing"
123 while c.playing() is not None:
124 time.sleep(1)
067eeb5f 125 print " ."
6c13a317 126 print " scheduling a track for the future"
5dc143a4 127 c.schedule_add(now() + 3, "normal", "play", track)
6c13a317
RK
128 print " schedule via python:"
129 s = c.schedule_list()
130 for event in s:
131 e = c.schedule_get(event)
132 print "item %s: %s" % (event, e)
133 print " deleting item %s" % s[0]
134 c.schedule_del(s[0])
135 print " checking it's really gone"
136 s = c.schedule_list()
137 assert s == [], "checking schedule is empty"
138 waited = 0
139 p = c.playing()
5dc143a4 140 while p is None and waited < 5:
6c13a317 141 time.sleep(1)
067eeb5f 142 print " ."
6c13a317
RK
143 waited += 1
144 p = c.playing()
145 assert p is None, "checking deleted scheduled event did not run"
146 print " checking you can't schedule events for the past"
147 try:
5dc143a4 148 c.schedule_add(now() - 4, "normal", "play", track)
6c13a317
RK
149 assert False, "checking schedule_add failed"
150 except disorder.operationError:
151 pass # good
99bd6109 152 print " checking scheduled events survive restarts"
5dc143a4
RK
153 when = now() + 3
154 c.schedule_add(when, "normal", "play", track)
99bd6109 155 dtest.stop_daemon()
0e464e60
RK
156 print " dumping database"
157 dump = "%s/dumpfile" % dtest.testroot
158 print dtest.command(["disorder-dump", "--config", disorder._configfile,
159 "--dump", dump])
160 print "restoring database"
161 print dtest.command(["disorder-dump", "--config", disorder._configfile,
162 "--undump", dump])
99bd6109
RK
163 dtest.start_daemon()
164 c = disorder.client()
5dc143a4 165 p = next_playing(c)
99bd6109 166 print " waiting for track to play"
99bd6109 167 assert p["track"] == track, "checking right track played"
5dc143a4 168 assert p["when"] >= when, "checking track played at right time"
99bd6109 169 assert c.schedule_list() == [], "checking schedule is empty"
5dc143a4
RK
170 print " checking junk events do not survive restarts"
171 c.schedule_add(now() + 2, "junk", "play", track)
172 s = c.schedule_list()
173 print s
174 dtest.stop_daemon()
175 time.sleep(3)
176 dtest.start_daemon()
177 c = disorder.client()
178 print " checking schedule is empty"
179 s = c.schedule_list()
180 print s
181 assert s == [], "checking schedule is empty"
17360d2f
RK
182
183if __name__ == '__main__':
184 dtest.run()