download: improve test coverage on our IDownloadTarget classes, make FileHandle return the filehandle when its done so that it is easier to close

This commit is contained in:
Brian Warner 2007-04-16 13:07:36 -07:00
parent 1a6da72861
commit 7dabb68a51
2 changed files with 30 additions and 1 deletions

View File

@ -372,6 +372,11 @@ class Data:
return self.data
class FileHandle:
"""Use me to download data to a pre-defined filehandle-like object. I
will use the target's write() method. I will *not* close the filehandle:
I leave that up to the originator of the filehandle. The download process
will return the filehandle when it completes.
"""
implements(IDownloadTarget)
def __init__(self, filehandle):
self._filehandle = filehandle
@ -387,7 +392,7 @@ class FileHandle:
def register_canceller(self, cb):
pass
def finish(self):
pass
return self._filehandle
class Downloader(service.MultiService):
"""I am a service that allows file downloading.

View File

@ -126,7 +126,9 @@ class SystemTest(unittest.TestCase):
d.addCallback(_do_upload)
def _upload_done(uri):
log.msg("upload finished: uri is %s" % (uri,))
self.uri = uri
dl = self.clients[1].getServiceNamed("downloader")
self.downloader = dl
d1 = dl.download_to_data(uri)
return d1
d.addCallback(_upload_done)
@ -134,6 +136,28 @@ class SystemTest(unittest.TestCase):
log.msg("download finished")
self.failUnlessEqual(data, DATA)
d.addCallback(_download_done)
target_filename = os.path.join(self.basedir, "download.target")
def _download_to_filename(res):
return self.downloader.download_to_filename(self.uri,
target_filename)
d.addCallback(_download_to_filename)
def _download_to_filename_done(res):
newdata = open(target_filename, "rb").read()
self.failUnlessEqual(newdata, DATA)
d.addCallback(_download_to_filename_done)
target_filename2 = os.path.join(self.basedir, "download.target2")
def _download_to_filehandle(res):
fh = open(target_filename2, "wb")
return self.downloader.download_to_filehandle(self.uri, fh)
d.addCallback(_download_to_filehandle)
def _download_to_filehandle_done(fh):
fh.close()
newdata = open(target_filename2, "rb").read()
self.failUnlessEqual(newdata, DATA)
d.addCallback(_download_to_filehandle_done)
return d
test_upload_and_download.timeout = 300