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.failUnlessEqual(100, fit)
36
def testBucketDrian(self):
37
"""Testing the bucket's drain rate."""
42
self.failUnlessEqual(20, fit)
44
class TestBucketNesting(TestBucketBase):
46
TestBucketBase.setUp(self)
47
self.parent = SomeBucket()
48
self.child1 = SomeBucket(self.parent)
49
self.child2 = SomeBucket(self.parent)
51
def testBucketParentSize(self):
52
# Use up most of the parent bucket.
54
fit = self.child2.add(90)
55
self.failUnlessEqual(10, fit)
57
def testBucketParentRate(self):
58
# Make the parent bucket drain slower.
60
# Fill both child1 and parent.
63
fit = self.child1.add(100)
64
# How much room was there? The child bucket would have had 20,
65
# but the parent bucket only ten (so no, it wouldn't make too much
66
# sense to have a child bucket draining faster than its parent in a real
68
self.failUnlessEqual(10, fit)
71
# TODO: Test the Transport stuff?
73
from test_pcp import DummyConsumer
75
class ConsumerShaperTest(TestBucketBase):
77
TestBucketBase.setUp(self)
78
self.underlying = DummyConsumer()
79
self.bucket = SomeBucket()
80
self.shaped = htb.ShapedConsumer(self.underlying, self.bucket)
83
# Start off with a full bucket, so the burst-size dosen't factor in
84
# to the calculations.
87
self.shaped.write("x" * 100)
88
self.clock.set(delta_t)
89
self.shaped.resumeProducing()
90
self.failUnlessEqual(len(self.underlying.getvalue()),
91
delta_t * self.bucket.rate)
93
def testBucketRefs(self):
94
self.failUnlessEqual(self.bucket._refcount, 1)
95
self.shaped.stopProducing()
96
self.failUnlessEqual(self.bucket._refcount, 0)