chiark / gitweb /
dump.py exercises tags db harder
authorRichard Kettlewell <rjk@greenend.org.uk>
Sun, 2 Dec 2007 12:34:13 +0000 (12:34 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sun, 2 Dec 2007 12:34:13 +0000 (12:34 +0000)
dtest.py checks that the speaker socket is going to be ok, formerly tests
would sometimes nang if they involved stopping and restarting the daemon.

python/disorder.py.in
tests/dtest.py
tests/dump.py

index 2ad2b44f791c920f796f338a4032082e077ee469..a8afcba9401dfaef6995eeb0755b56edac62808e 100644 (file)
@@ -659,6 +659,14 @@ class client:
     self._simple("search", _quote(words))
     return self._body()
 
     self._simple("search", _quote(words))
     return self._body()
 
+  def tags(self):
+    """List all tags
+
+    The return value is a list of all tags which apply to at least one
+    track."""
+    self._simple("tags")
+    return self._body()
+
   def stats(self):
     """Get server statistics.
 
   def stats(self):
     """Get server statistics.
 
index cd14cfbbce5cd7df210bcd9241c25153cd510079..3f0788f20feb93e481ed6b207e4c5b11de61a93c 100644 (file)
@@ -21,7 +21,7 @@
 
 """Utility module used by tests"""
 
 
 """Utility module used by tests"""
 
-import os,os.path,subprocess,sys,re,time,unicodedata,random
+import os,os.path,subprocess,sys,re,time,unicodedata,random,socket
 
 def fatal(s):
     """Write an error message and exit"""
 
 def fatal(s):
     """Write an error message and exit"""
@@ -150,12 +150,29 @@ def stdtracks():
     maketrack("misc/blahblahblah.ogg")
     maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
     maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
     maketrack("misc/blahblahblah.ogg")
     maketrack("Various/Greatest Hits/01:Jim Whatever - Spong.ogg")
     maketrack("Various/Greatest Hits/02:Joe Bloggs - Yadda.ogg")
+
+def bindable(p):
+    """bindable(P)
+
+    Return True iff UDP port P is bindable, else False"""
+    s = socket.socket(socket.AF_INET,
+                      socket.SOCK_DGRAM,
+                      socket.IPPROTO_UDP)
+    try:
+        s.bind(("127.0.0.1", p))
+        s.close()
+        return True
+    except:
+        return False
+
 def common_setup():
     remove_dir(testroot)
     os.mkdir(testroot)
     global port
     port = random.randint(49152, 65535)
 def common_setup():
     remove_dir(testroot)
     os.mkdir(testroot)
     global port
     port = random.randint(49152, 65535)
+    while not bindable(port):
+        print "port %d is not bindable, trying another" % port
+        port = random.randint(49152, 65535)
     open("%s/config" % testroot, "w").write(
     """home %s
 collection fs UTF-8 %s/tracks
     open("%s/config" % testroot, "w").write(
     """home %s
 collection fs UTF-8 %s/tracks
@@ -193,8 +210,13 @@ def start_daemon():
     """start_daemon()
 
 Start the daemon."""
     """start_daemon()
 
 Start the daemon."""
-    global daemon, errs
+    global daemon, errs, port
     assert daemon == None, "no daemon running"
     assert daemon == None, "no daemon running"
+    if not bindable(port):
+        print "waiting for speaker socket to become bindable again..."
+        time.sleep(1)
+        while not bindable(port):
+            time.sleep(1)
     print " starting daemon"
     # remove the socket if it exists
     socket = "%s/socket" % testroot
     print " starting daemon"
     # remove the socket if it exists
     socket = "%s/socket" % testroot
@@ -297,15 +319,25 @@ Recursively delete directory D"""
         else:
             os.remove(d)
 
         else:
             os.remove(d)
 
+def lists_have_same_contents(l1, l2):
+    """lists_have_same_contents(L1, L2)
+
+    Return True if L1 and L2 have equal members, in any order; else False."""
+    s1 = []
+    s1.extend(l1)
+    s1.sort()
+    s2 = []
+    s2.extend(l2)
+    s2.sort()
+    return s1 == s2
+
 def check_files():
     c = disorder.client()
     failures = 0
     for d in dirs_by_dir:
         xdirs = dirs_by_dir[d]
         dirs = c.directories(d)
 def check_files():
     c = disorder.client()
     failures = 0
     for d in dirs_by_dir:
         xdirs = dirs_by_dir[d]
         dirs = c.directories(d)
-        xdirs.sort()
-        dirs.sort()
-        if dirs != xdirs:
+        if not lists_have_same_contents(xdirs, dirs):
             print
             print "directory: %s" % d
             print "expected:  %s" % xdirs
             print
             print "directory: %s" % d
             print "expected:  %s" % xdirs
@@ -314,9 +346,7 @@ def check_files():
     for d in files_by_dir:
         xfiles = files_by_dir[d]
         files = c.files(d)
     for d in files_by_dir:
         xfiles = files_by_dir[d]
         files = c.files(d)
-        xfiles.sort()
-        files.sort()
-        if files != xfiles:
+        if not lists_have_same_contents(xfiles, files):
             print
             print "directory: %s" % d
             print "expected:  %s" % xfiles
             print
             print "directory: %s" % d
             print "expected:  %s" % xfiles
index 8cf0943cf508e111227b500c6e0ea11ff95ae8f6..2db150500e0223ee650223c2e2381119f423e6ba 100755 (executable)
@@ -33,7 +33,15 @@ def test():
     c.setglobal("foo", "before");
     assert c.getglobal("foo") == "before", "checking global foo=before"
     print "adding a tag"
     c.setglobal("foo", "before");
     assert c.getglobal("foo") == "before", "checking global foo=before"
     print "adding a tag"
-    c.set(track, "tags", "wibble")
+    # Exercise the tags-changed code
+    c.set(track, "tags", "first tag, another tag")
+    assert dtest.lists_have_same_contents(c.tags(),
+                                          [u"another tag", u"first tag"]),\
+           "checking tag list(1)"
+    c.set(track, "tags", "wibble, another tag")
+    assert dtest.lists_have_same_contents(c.tags(),
+                                          [u"another tag", u"wibble"]),\
+           "checking tag list(2)"
     print "checking track appears in tag search"
     tracks = c.search(["tag:wibble"])
     assert len(tracks) == 1, "checking there is exactly one search result"
     print "checking track appears in tag search"
     tracks = c.search(["tag:wibble"])
     assert len(tracks) == 1, "checking there is exactly one search result"
@@ -69,6 +77,9 @@ def test():
     tracks = c.search(["tag:wibble"])
     assert len(tracks) == 1, "checking there is exactly one search result"
     assert tracks[0] == track, "checking for right search result"
     tracks = c.search(["tag:wibble"])
     assert len(tracks) == 1, "checking there is exactly one search result"
     assert tracks[0] == track, "checking for right search result"
+    assert dtest.lists_have_same_contents(c.tags(),
+                                          [u"another tag", u"wibble"]),\
+           "checking tag list(3)"
 
 if __name__ == '__main__':
     dtest.run()
 
 if __name__ == '__main__':
     dtest.run()