From 18d8911c51a17ea2c90ab014c632d7565450f079 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 1 Dec 2006 19:17:26 -0700 Subject: [PATCH] change bucket writer protocol, give Encoder a RemoteReference instead of a file-like object --- allmydata/test/test_upload.py | 31 ++++++++++++++++++++++++++++--- allmydata/upload.py | 28 +--------------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/allmydata/test/test_upload.py b/allmydata/test/test_upload.py index efcbd74e..349133d5 100644 --- a/allmydata/test/test_upload.py +++ b/allmydata/test/test_upload.py @@ -1,9 +1,36 @@ from twisted.trial import unittest from twisted.internet import defer +from cStringIO import StringIO from allmydata import upload +class StringBucketProxy: + # This is for unit tests: make a StringIO look like a RIBucketWriter. + + def __init__(self): + self.data = StringIO() + self.size = None + self.done = False + + def callRemote(self, methname, **kwargs): + if methname == "write": + return defer.maybeDeferred(self.write, **kwargs) + elif methname == "set_size": + return defer.maybeDeferred(self.set_size, **kwargs) + elif methname == "close": + return defer.maybeDeferred(self.close, **kwargs) + else: + return defer.fail(NameError("no such method named %s" % methname)) + + def write(self, data): + self.data.write(data) + def set_size(self, size): + self.size = size + def close(self): + self.done = True + + class FakePeer: def __init__(self, peerid, response): self.peerid = peerid @@ -55,9 +82,7 @@ class NextPeer(unittest.TestCase): def compare_landlords(self, u, c, expected): exp = [(peerid, bucketnum, c.peers[peerid]) for peerid, bucketnum in expected] - landlords = [(peerid, bucketnum, proxy.remote_bucket) - for peerid, bucketnum, proxy in u.landlords] - self.failUnlessEqual(landlords, exp) + self.failUnlessEqual(u.landlords, exp) def test_0(self): c = FakeClient([]) diff --git a/allmydata/upload.py b/allmydata/upload.py index f9f91567..ae659c03 100644 --- a/allmydata/upload.py +++ b/allmydata/upload.py @@ -13,31 +13,6 @@ class HaveAllPeersError(Exception): class TooFullError(Exception): pass -class WriterProxy: - # make this look like a writable file - def __init__(self, remote_bucket): - self.remote_bucket = remote_bucket - self.good = True - - def _broken(self, why): - self.good = False - # do something else here - return why - - def write(self, data): - d = self.remote_bucket.callRemote("write", data=data) - d.addErrback(self._broken) - - def seek(self, offset, whence=0): - d = self.remote_bucket.callRemote("seek", offset=offset, whence=whence) - d.addErrback(self._broken) - - def close(self): - if self._broken: - raise SomethingBrokeError() - d = self.remote_bucket.callRemote("close") - d.addErrback(self._broken) - class Uploader: debug = False @@ -98,8 +73,7 @@ class Uploader: def _allocate_response(bucket): if self.debug: print " peerid %s will grant us a lease" % peerid - writer = WriterProxy(bucket) - self.landlords.append( (peerid, bucket_num, writer) ) + self.landlords.append( (peerid, bucket_num, bucket) ) self.goodness_points += 1 if self.goodness_points >= self.target_goodness: if self.debug: print " we're done!" -- 2.45.2