From da7778774b658dd3408b4e2e59ab993a4d1f27ab Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Wed, 9 Jan 2008 20:25:50 -0700 Subject: [PATCH] offloaded: basic test for client-side of AssistedUploader --- src/allmydata/test/test_helper.py | 75 +++++++++++++++++++++++++++++++ src/allmydata/upload.py | 17 ++++--- 2 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 src/allmydata/test/test_helper.py diff --git a/src/allmydata/test/test_helper.py b/src/allmydata/test/test_helper.py new file mode 100644 index 00000000..ae6f213d --- /dev/null +++ b/src/allmydata/test/test_helper.py @@ -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 + diff --git a/src/allmydata/upload.py b/src/allmydata/upload.py index 63e98a11..56c79ba8 100644 --- a/src/allmydata/upload.py +++ b/src/allmydata/upload.py @@ -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) -- 2.45.2