immutable: tighten preconditions -- you can write empty strings or read zero bytes, and add the first simple unit test of DownUpConnector

This commit is contained in:
Zooko O'Whielacronx 2009-02-10 00:56:47 -07:00
parent d436c6235d
commit 75e4e67ed7
2 changed files with 18 additions and 0 deletions

View File

@ -164,6 +164,7 @@ class DownUpConnector(log.PrefixingLogMixin):
self.storageindex = storageindex self.storageindex = storageindex
self._storageindex_osol.fire(self.storageindex) self._storageindex_osol.fire(self.storageindex)
def write(self, data): def write(self, data):
precondition(data) # please don't write empty strings
self.bufs.append(data) self.bufs.append(data)
self.bufsiz += len(data) self.bufsiz += len(data)
self._satisfy_reads_if_possible() self._satisfy_reads_if_possible()
@ -191,6 +192,7 @@ class DownUpConnector(log.PrefixingLogMixin):
return self._encodingparams_osol.when_fired() return self._encodingparams_osol.when_fired()
def read_encrypted(self, length, hash_only): def read_encrypted(self, length, hash_only):
""" Returns a deferred which eventually fired with the requested ciphertext. """ """ Returns a deferred which eventually fired with the requested ciphertext. """
precondition(length) # please don't ask to read 0 bytes
d = defer.Deferred() d = defer.Deferred()
self.next_read_ds.append(d) self.next_read_ds.append(d)
self.next_read_lens.append(length) self.next_read_lens.append(length)

View File

@ -2,6 +2,7 @@ from allmydata.test import common
from allmydata.monitor import Monitor from allmydata.monitor import Monitor
from allmydata import check_results from allmydata import check_results
from allmydata.interfaces import NotEnoughSharesError from allmydata.interfaces import NotEnoughSharesError
from allmydata.immutable import repairer
from twisted.internet import defer from twisted.internet import defer
from twisted.trial import unittest from twisted.trial import unittest
import random import random
@ -337,6 +338,21 @@ WRITE_LEEWAY = 35
# Optimally, you could repair one of these (small) files in a single write. # Optimally, you could repair one of these (small) files in a single write.
DELTA_WRITES_PER_SHARE = 1 * WRITE_LEEWAY DELTA_WRITES_PER_SHARE = 1 * WRITE_LEEWAY
class DownUpConnector(unittest.TestCase):
def test_deferred_satisfaction(self):
duc = repairer.DownUpConnector()
duc.registerProducer(None, True) # just because you have to call registerProducer first
# case 1: total data in buf is < requested data at time of request
duc.write('\x01')
d = duc.read_encrypted(2, False)
def _then(data):
self.failUnlessEqual(len(data), 2)
self.failUnlessEqual(data[0], '\x01')
self.failUnlessEqual(data[1], '\x02')
d.addCallback(_then)
duc.write('\x02')
return d
class Repairer(common.ShareManglingMixin, unittest.TestCase): class Repairer(common.ShareManglingMixin, unittest.TestCase):
def test_test_code(self): def test_test_code(self):
# The following process of stashing the shares, running # The following process of stashing the shares, running