3 __version__ = '$Revision: 1.3 $'[11:-2]
5 from twisted.trial import unittest
6 from twisted.protocols import htb
16 class SomeBucket(htb.Bucket):
20 class TestBucketBase(unittest.TestCase):
22 self._realTimeFunc = htb.time
23 self.clock = DummyClock()
27 htb.time = self._realTimeFunc
29 class TestBucket(TestBucketBase):
30 def testBucketSize(self):
31 """Testing the size of the bucket."""
34 self.assertEqual(100, fit)
36 def testBucketDrain(self):
37 """Testing the bucket's drain rate."""
42 self.assertEqual(20, fit)
44 def test_bucketEmpty(self):
46 L{htb.Bucket.drip} returns C{True} if the bucket is empty after that drip.
52 self.assertFalse(empty)
55 self.assertTrue(empty)
57 class TestBucketNesting(TestBucketBase):
59 TestBucketBase.setUp(self)
60 self.parent = SomeBucket()
61 self.child1 = SomeBucket(self.parent)
62 self.child2 = SomeBucket(self.parent)
64 def testBucketParentSize(self):
65 # Use up most of the parent bucket.
67 fit = self.child2.add(90)
68 self.assertEqual(10, fit)
70 def testBucketParentRate(self):
71 # Make the parent bucket drain slower.
73 # Fill both child1 and parent.
76 fit = self.child1.add(100)
77 # How much room was there? The child bucket would have had 20,
78 # but the parent bucket only ten (so no, it wouldn't make too much
79 # sense to have a child bucket draining faster than its parent in a real
81 self.assertEqual(10, fit)
84 # TODO: Test the Transport stuff?
86 from test_pcp import DummyConsumer
88 class ConsumerShaperTest(TestBucketBase):
90 TestBucketBase.setUp(self)
91 self.underlying = DummyConsumer()
92 self.bucket = SomeBucket()
93 self.shaped = htb.ShapedConsumer(self.underlying, self.bucket)
96 # Start off with a full bucket, so the burst-size dosen't factor in
97 # to the calculations.
100 self.shaped.write("x" * 100)
101 self.clock.set(delta_t)
102 self.shaped.resumeProducing()
103 self.assertEqual(len(self.underlying.getvalue()),
104 delta_t * self.bucket.rate)
106 def testBucketRefs(self):
107 self.assertEqual(self.bucket._refcount, 1)
108 self.shaped.stopProducing()
109 self.assertEqual(self.bucket._refcount, 0)