From 7dabb68a51b673499c5894439d6d1c51456d43d5 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Mon, 16 Apr 2007 13:07:36 -0700
Subject: [PATCH] download: improve test coverage on our IDownloadTarget
 classes, make FileHandle return the filehandle when its done so that it is
 easier to close

---
 src/allmydata/download.py         |  7 ++++++-
 src/allmydata/test/test_system.py | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/allmydata/download.py b/src/allmydata/download.py
index 959d3fa7..9eba5681 100644
--- a/src/allmydata/download.py
+++ b/src/allmydata/download.py
@@ -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.
diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py
index 83d03f97..08e83689 100644
--- a/src/allmydata/test/test_system.py
+++ b/src/allmydata/test/test_system.py
@@ -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
 
-- 
2.45.2