From 31773020eedbe29daff0a407619df9607e569e22 Mon Sep 17 00:00:00 2001 Message-Id: <31773020eedbe29daff0a407619df9607e569e22.1714969858.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sun, 2 Dec 2007 12:34:13 +0000 Subject: [PATCH] dump.py exercises tags db harder 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. Organization: Straylight/Edgeware From: Richard Kettlewell --- python/disorder.py.in | 8 ++++++++ tests/dtest.py | 48 +++++++++++++++++++++++++++++++++++-------- tests/dump.py | 13 +++++++++++- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/python/disorder.py.in b/python/disorder.py.in index 2ad2b44..a8afcba 100644 --- a/python/disorder.py.in +++ b/python/disorder.py.in @@ -659,6 +659,14 @@ class client: 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. diff --git a/tests/dtest.py b/tests/dtest.py index cd14cfb..3f0788f 100644 --- a/tests/dtest.py +++ b/tests/dtest.py @@ -21,7 +21,7 @@ """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""" @@ -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") - + +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) + 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 @@ -193,8 +210,13 @@ def start_daemon(): """start_daemon() Start the daemon.""" - global daemon, errs + global daemon, errs, port 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 @@ -297,15 +319,25 @@ Recursively delete directory 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) - xdirs.sort() - dirs.sort() - if dirs != xdirs: + if not lists_have_same_contents(xdirs, dirs): 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) - xfiles.sort() - files.sort() - if files != xfiles: + if not lists_have_same_contents(xfiles, files): print print "directory: %s" % d print "expected: %s" % xfiles diff --git a/tests/dump.py b/tests/dump.py index 8cf0943..2db1505 100755 --- a/tests/dump.py +++ b/tests/dump.py @@ -33,7 +33,15 @@ def test(): 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" @@ -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" + assert dtest.lists_have_same_contents(c.tags(), + [u"another tag", u"wibble"]),\ + "checking tag list(3)" if __name__ == '__main__': dtest.run() -- [mdw]