offloaded: basic test for client-side of AssistedUploader
authorBrian Warner <warner@allmydata.com>
Thu, 10 Jan 2008 03:25:50 +0000 (20:25 -0700)
committerBrian Warner <warner@allmydata.com>
Thu, 10 Jan 2008 03:25:50 +0000 (20:25 -0700)
src/allmydata/test/test_helper.py [new file with mode: 0644]
src/allmydata/upload.py

diff --git a/src/allmydata/test/test_helper.py b/src/allmydata/test/test_helper.py
new file mode 100644 (file)
index 0000000..ae6f213
--- /dev/null
@@ -0,0 +1,75 @@
+
+from twisted.trial import unittest
+from twisted.application import service
+
+from foolscap import Tub, eventual
+from foolscap.logging import log
+
+from allmydata import upload, offloaded
+
+class FakeCHKUploadHelper(offloaded.CHKUploadHelper):
+    pass
+
+class FakeHelper(offloaded.Helper):
+    chk_upload_helper_class = FakeCHKUploadHelper
+
+class FakeClient(service.MultiService):
+    def log(self, msg, **kwargs):
+        return log.msg(msg, **kwargs)
+    def get_push_to_ourselves(self):
+        return True
+    def get_encoding_parameters(self):
+        return None
+
+def flush_but_dont_ignore(res):
+    d = eventual.flushEventualQueue()
+    def _done(ignored):
+        return res
+    d.addCallback(_done)
+    return d
+
+class AssistedUpload(unittest.TestCase):
+    def setUp(self):
+        self.s = FakeClient()
+        self.s.startService()
+
+        self.tub = t = Tub()
+        t.setServiceParent(self.s)
+        self.s.tub = t
+        # we never actually use this for network traffic, so it can use a
+        # bogus host/port
+        t.setLocation("bogus:1234")
+
+
+        h = FakeHelper(".")
+        h.setServiceParent(self.s)
+        self.helper_furl = t.registerReference(h)
+
+    def tearDown(self):
+        d = self.s.stopService()
+        d.addCallback(eventual.fireEventually)
+        d.addBoth(flush_but_dont_ignore)
+        return d
+
+
+    def test_one(self):
+        u = upload.Uploader(self.helper_furl)
+        u.setServiceParent(self.s)
+
+        # wait a few turns
+        d = eventual.fireEventually()
+        d.addCallback(eventual.fireEventually)
+        d.addCallback(eventual.fireEventually)
+
+        def _ready(res):
+            assert u._helper
+
+            DATA = "I need help\n" * 1000
+            return u.upload_data(DATA)
+        d.addCallback(_ready)
+        def _uploaded(uri):
+            assert "CHK" in uri
+        d.addCallback(_uploaded)
+
+        return d
+
index 63e98a1182bc1a08095f9f1aa04df85ad7718cac..56c79ba865bef448e0eee0fe9f91d880d518529e 100644 (file)
@@ -1,10 +1,11 @@
 
 import os
 from zope.interface import implements
-from twisted.python import log, failure
+from twisted.python import failure
 from twisted.internet import defer
 from twisted.application import service
 from foolscap import Referenceable
+from foolscap.logging import log
 
 from allmydata.util.hashutil import file_renewal_secret_hash, \
      file_cancel_secret_hash, bucket_renewal_secret_hash, \
@@ -514,6 +515,7 @@ class CHKUploader:
                            )
         return u.to_string()
 
+
 def read_this_many_bytes(uploadable, size, prepend_data=[]):
     if size == 0:
         return defer.succeed([])
@@ -581,10 +583,15 @@ class AssistedUploader:
     def __init__(self, helper, options={}):
         self._helper = helper
         self._options = options
-        self._log_number = self._client.log("AssistedUploader starting")
+        self._log_number = log.msg("AssistedUploader starting")
+
+    def log(self, msg, parent=None, **kwargs):
+        if parent is None:
+            parent = self._log_number
+        return log.msg(msg, parent=parent, **kwargs)
 
     def set_params(self, encoding_parameters):
-        pass
+        self._needed_shares, happy, self._total_shares = encoding_parameters
 
     def start(self, uploadable):
         u = IUploadable(uploadable)
@@ -617,7 +624,7 @@ class AssistedUploader:
         d = self._helper.callRemote("upload", self._storage_index)
         d.addCallback(self._contacted_helper)
         return d
-    def _contacted_helper(self, upload_results, upload_helper):
+    def _contacted_helper(self, (upload_results, upload_helper)):
         if upload_helper:
             # we need to upload the file
             reu = RemoteEncryptedUploabable(self._encuploadable)
@@ -751,7 +758,7 @@ class Uploader(service.MultiService):
                 uploader = LiteralUploader(self.parent, options,
                                            wait_for_numpeers)
             elif self._helper:
-                uploader = AssistedUploader(self.parent, options)
+                uploader = AssistedUploader(self._helper, options)
             else:
                 uploader = self.uploader_class(self.parent, options,
                                                wait_for_numpeers)