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
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([])
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
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!"