chiark / gitweb /
fe3c5c3ffe2698591717a813c7de932d0c158b3a
[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 2 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, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # 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, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
20 #
21 import dtest,disorder,time,string
22
23 def test():
24     """Exercise schedule support"""
25     dtest.start_daemon()
26     dtest.create_user()
27     c = disorder.client()
28     c.random_disable()
29     dtest.rescan()
30     # Wait until there's no track playing
31     print " waiting for nothing to be playing"
32     while c.playing() is not None:
33         time.sleep(1)
34         print "  ."
35     now = int(time.time())
36     track = "%s/Joe Bloggs/First Album/05:Fifth track.ogg" % dtest.tracks
37     print " scheduling a track for the future"
38     c.schedule_add(now + 4, "normal", "play", track)
39     print " disorder schedule-list output:"
40     print string.join(dtest.command(["disorder",
41                                      "--config", disorder._configfile,
42                                      "--no-per-user-config",
43                                      "schedule-list"]), ""),
44     print " waiting for it to play"
45     waited = 0
46     p = c.playing()
47     while p is None and waited < 10:
48         time.sleep(1)
49         print "  ."
50         waited += 1
51         p = c.playing()
52     assert waited < 10, "checking track played within a reasonable period"
53     assert waited > 2, "checking track didn't play immediately"
54     assert p["track"] == track, "checking right track played"
55     assert c.schedule_list() == [], "checking schedule is empty"
56     print " waiting for nothing to be playing"
57     while c.playing() is not None:
58         time.sleep(1)
59         print "  ."
60     print " scheduling an enable-random for the future"
61     now = int(time.time())
62     c.schedule_add(now + 4, "normal", "set-global", "random-play", "yes")
63     print " disorder schedule-list output:"
64     print string.join(dtest.command(["disorder",
65                                      "--config", disorder._configfile,
66                                      "--no-per-user-config",
67                                      "schedule-list"]), ""),
68     print " waiting for it to take effect"
69     waited = 0
70     p = c.playing()
71     while p is None and waited < 10:
72         time.sleep(1)
73         print "  ."
74         waited += 1
75         p = c.playing()
76     assert waited < 10, "checking a track played within a reasonable period"
77     assert waited > 2, "checking a track didn't play immediately"
78     print " disabling random play"
79     c.random_disable()
80     print " waiting for nothing to be playing"
81     while c.playing() is not None:
82         time.sleep(1)
83         print "  ."
84     print " scheduling track to play later via command line"
85     now = int(time.time())
86     dtest.command(["disorder",
87                    "--config", disorder._configfile,
88                    "--no-per-user-config",
89                    "schedule-play",
90                    time.strftime("%Y-%m-%d %H:%M:%S",
91                                  time.localtime(now + 4)),
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"]), ""),
99     print " waiting for it to play"
100     waited = 0
101     p = c.playing()
102     while p is None and waited < 10:
103         time.sleep(1)
104         print "  ."
105         waited += 1
106         p = c.playing()
107     assert waited < 10, "checking track played within a reasonable period"
108     assert waited > 2, "checking track didn't play immediately"
109     assert p["track"] == track, "checking right track played"
110     assert c.schedule_list() == [], "checking schedule is empty"
111     print " waiting for nothing to be playing"
112     while c.playing() is not None:
113         time.sleep(1)
114         print "  ."
115     print " scheduling an enable-random for later via command line"
116     now = int(time.time())
117     dtest.command(["disorder",
118                    "--config", disorder._configfile,
119                    "--no-per-user-config",
120                    "schedule-set-global",
121                    time.strftime("%Y-%m-%d %H:%M:%S",
122                                  time.localtime(now + 4)),
123                    "normal",
124                    "random-play",
125                    "yes"])
126     print " disorder schedule-list output:"
127     print string.join(dtest.command(["disorder",
128                                      "--config", disorder._configfile,
129                                      "--no-per-user-config",
130                                      "schedule-list"]), ""),
131     print " waiting for it to take effect"
132     waited = 0
133     p = c.playing()
134     while p is None and waited < 10:
135         time.sleep(1)
136         print "  ."
137         waited += 1
138         p = c.playing()
139     assert waited < 10, "checking a track played within a reasonable period"
140     assert waited > 2, "checking a track didn't play immediately"
141     print " disabling random play"
142     c.random_disable()
143     print " waiting for nothing to be playing"
144     while c.playing() is not None:
145         time.sleep(1)
146         print "  ."
147     print " scheduling a track for the future"
148     now = int(time.time())
149     c.schedule_add(now + 4, "normal", "play", track)
150     print " schedule via python:"
151     s = c.schedule_list()
152     for event in s:
153         e = c.schedule_get(event)
154         print "item %s: %s" % (event, e)
155     print " deleting item %s" % s[0]
156     c.schedule_del(s[0])
157     print " checking it's really gone"
158     s = c.schedule_list()
159     assert s == [], "checking schedule is empty"
160     waited = 0
161     p = c.playing()
162     while p is None and waited < 10:
163         time.sleep(1)
164         print "  ."
165         waited += 1
166         p = c.playing()
167     assert p is None, "checking deleted scheduled event did not run"
168     print " checking you can't schedule events for the past"
169     try:
170         now = int(time.time())
171         c.schedule_add(now - 4, "normal", "play", track)
172         assert False, "checking schedule_add failed"
173     except disorder.operationError:
174       pass                              # good
175     print " checking scheduled events survive restarts"
176     c.schedule_add(now + 4, "normal", "play", track)
177     dtest.stop_daemon()
178     print " dumping database"
179     dump = "%s/dumpfile" % dtest.testroot
180     print dtest.command(["disorder-dump", "--config", disorder._configfile,
181                          "--dump", dump])
182     print "restoring database"
183     print dtest.command(["disorder-dump", "--config", disorder._configfile,
184                          "--undump", dump])
185     dtest.start_daemon()
186     c = disorder.client()
187     print " waiting for track to play"
188     waited = 0
189     p = c.playing()
190     while p is None and waited < 10:
191         time.sleep(1)
192         print "  ."
193         waited += 1
194         p = c.playing()
195     assert waited < 10, "checking track played within a reasonable period"
196     assert p["track"] == track, "checking right track played"
197     assert c.schedule_list() == [], "checking schedule is empty"
198
199 if __name__ == '__main__':
200     dtest.run()