From ef1bfdd2bf4246a817b37db85cf2a46c346013d0 Mon Sep 17 00:00:00 2001
From: Zooko O'Whielacronx <zooko@zooko.com>
Date: Tue, 10 Feb 2009 12:12:45 -0700
Subject: [PATCH] immutable: repairer: add a simple test to exercise the
 "leftover" code path, fix the bug (and rename the variable "leftover" to
 "extra")

---
 src/allmydata/immutable/repairer.py |  6 +++---
 src/allmydata/test/test_repairer.py | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/allmydata/immutable/repairer.py b/src/allmydata/immutable/repairer.py
index 1fae5cd6..880188c8 100644
--- a/src/allmydata/immutable/repairer.py
+++ b/src/allmydata/immutable/repairer.py
@@ -139,9 +139,9 @@ class DownUpConnector(log.PrefixingLogMixin):
                 res.append(nextbuf)
                 ressize += len(nextbuf)
                 if ressize > nrl:
-                    leftover = ressize - nrl
-                    self.bufs.appendleft(nextbuf[leftover:])
-                    res[-1] = nextbuf[:leftover]
+                    extra = ressize - nrl
+                    self.bufs.appendleft(nextbuf[:-extra])
+                    res[-1] = nextbuf[:-extra]
             self.bufsiz -= nrl
             if self.bufsiz < self.buflim and self.producer:
                 self.producer.resumeProducing()
diff --git a/src/allmydata/test/test_repairer.py b/src/allmydata/test/test_repairer.py
index cea1713c..d15f2eae 100644
--- a/src/allmydata/test/test_repairer.py
+++ b/src/allmydata/test/test_repairer.py
@@ -353,6 +353,20 @@ class DownUpConnector(unittest.TestCase):
         duc.write('\x02')
         return d
 
+    def test_leftovers(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\0x03')
+        return d
+
 class Repairer(common.ShareManglingMixin, unittest.TestCase):
     def test_test_code(self):
         # The following process of stashing the shares, running
-- 
2.45.2