]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
download: improve test coverage on our IDownloadTarget classes, make FileHandle retur...
authorBrian Warner <warner@allmydata.com>
Mon, 16 Apr 2007 20:07:36 +0000 (13:07 -0700)
committerBrian Warner <warner@allmydata.com>
Mon, 16 Apr 2007 20:07:36 +0000 (13:07 -0700)
src/allmydata/download.py
src/allmydata/test/test_system.py

index 959d3fa72af033be80c3f6098b0d49e231bf1b10..9eba5681bdb859dec5d3c239236b5f3fb5a0e155 100644 (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.
index 83d03f97fb91395e71317afa6dee659bab468089..08e83689c8cf1a7f9c5c7e726e90625bf15c7de5 100644 (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