chiark / gitweb /
Tests terminate server with SIGTERM rather than trying to send a
[disorder] / tests / play.py
1 #! /usr/bin/env python
2 #
3 # This file is part of DisOrder.
4 # Copyright (C) 2007, 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,time,disorder,re,sys
20
21 def test():
22     """Play some tracks"""
23     dtest.start_daemon()
24     dtest.create_user()
25     dtest.rescan()                      # ensure all files are scanned
26     c = disorder.client()
27     c.random_disable()
28     assert c.random_enabled() == False
29     track = u"%s/Joe Bloggs/First Album/02:Second track.ogg" % dtest.tracks
30     print " adding track to queue"
31     c.disable()
32     assert c.enabled() == False
33     c.play(track)
34     print " checking track turned up in queue"
35     q = c.queue()
36     ts = filter(lambda t: t['track'] == track and 'submitter' in t, q)
37     assert len(ts) == 1, "checking track appears exactly once in queue"
38     t = ts[0]
39     assert t['submitter'] == u'fred', "check queue submitter"
40     i = t['id']
41     print " waiting for track"
42     c.enable()
43     assert c.enabled() == True
44     p = c.playing()
45     r = c.recent()
46     limit = 60
47     while not((p is not None and p['id'] == i)
48               or (len(filter(lambda t: t['track'] == track
49                              and 'submitter' in t, r)) > 0)) and limit > 0:
50         time.sleep(1)
51         p = c.playing()
52         r = c.recent()
53         limit -= 1
54     assert limit > 0, "check track did complete in a reasonable time"
55     print " checking track turned up in recent list"
56     while (p is not None and p['id'] == i):
57         time.sleep(1)
58         p = c.playing()
59     r = c.recent()
60     ts = filter(lambda t: t['track'] == track and 'submitter' in t, r)
61     assert len(ts) == 1, "check track appears exactly once in recent"
62     t = ts[0]
63     assert t['submitter'] == u'fred', "check recent entry submitter"
64
65     print " testing scratches"
66     retry = False
67     scratchlimit = 5
68     while scratchlimit > 0:
69         scratchlimit -= 1
70         c.disable()
71         print " starting a track"
72         c.play(track)
73         c.enable()
74         p = c.playing()
75         if p is None:
76             print " track played too quickly, trying again..."
77             continue
78         print " scratching track"
79         i = p['id']
80         c.scratch(i)
81         print " waiting for track to finish"
82         p = c.playing()
83         limit = 60
84         while (p is not None and p['id'] == i) and limit > 0:
85             time.sleep(1)
86             p = c.playing()
87             limit -= 1
88         assert limit > 0, "check track finishes in a reasonable period"
89         print " checking scratched track turned up in recent list"
90         r = c.recent()
91         ts = filter(lambda t: t['id'] == i, r)
92         assert len(ts) == 1, "check scratched track appears exactly once in recent"
93         if ts[0]['state'] == 'ok':
94             print " track played too quickly, trying again..."
95             continue
96         assert ts[0]['state'] == 'scratched', "checking track scratched"
97         break
98     if scratchlimit == 0:
99         # TODO this is really not a great approach!
100         print " didn't complete in a reasonable time"
101         sys.exit(77)
102     print " waiting for scratch to complete"
103     p = c.recent()
104     while p is not None:
105         time.sleep(1)
106         p = c.playing()
107     assert p is None, "checking nothing is playing"
108     assert c.enabled() == True
109     c.random_enable()
110     assert c.random_enabled() == True
111
112 if __name__ == '__main__':
113     dtest.run()