Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_htb.py 3.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- Python -*-
  2. __version__ = "$Revision: 1.3 $"[11:-2]
  3. from twisted.protocols import htb
  4. from twisted.trial import unittest
  5. from .test_pcp import DummyConsumer
  6. class DummyClock:
  7. time = 0
  8. def set(self, when):
  9. self.time = when
  10. def __call__(self):
  11. return self.time
  12. class SomeBucket(htb.Bucket):
  13. maxburst = 100
  14. rate = 2
  15. class TestBucketBase(unittest.TestCase):
  16. def setUp(self):
  17. self._realTimeFunc = htb.time
  18. self.clock = DummyClock()
  19. htb.time = self.clock
  20. def tearDown(self):
  21. htb.time = self._realTimeFunc
  22. class BucketTests(TestBucketBase):
  23. def testBucketSize(self):
  24. """
  25. Testing the size of the bucket.
  26. """
  27. b = SomeBucket()
  28. fit = b.add(1000)
  29. self.assertEqual(100, fit)
  30. def testBucketDrain(self):
  31. """
  32. Testing the bucket's drain rate.
  33. """
  34. b = SomeBucket()
  35. fit = b.add(1000)
  36. self.clock.set(10)
  37. fit = b.add(1000)
  38. self.assertEqual(20, fit)
  39. def test_bucketEmpty(self):
  40. """
  41. L{htb.Bucket.drip} returns C{True} if the bucket is empty after that drip.
  42. """
  43. b = SomeBucket()
  44. b.add(20)
  45. self.clock.set(9)
  46. empty = b.drip()
  47. self.assertFalse(empty)
  48. self.clock.set(10)
  49. empty = b.drip()
  50. self.assertTrue(empty)
  51. class BucketNestingTests(TestBucketBase):
  52. def setUp(self):
  53. TestBucketBase.setUp(self)
  54. self.parent = SomeBucket()
  55. self.child1 = SomeBucket(self.parent)
  56. self.child2 = SomeBucket(self.parent)
  57. def testBucketParentSize(self):
  58. # Use up most of the parent bucket.
  59. self.child1.add(90)
  60. fit = self.child2.add(90)
  61. self.assertEqual(10, fit)
  62. def testBucketParentRate(self):
  63. # Make the parent bucket drain slower.
  64. self.parent.rate = 1
  65. # Fill both child1 and parent.
  66. self.child1.add(100)
  67. self.clock.set(10)
  68. fit = self.child1.add(100)
  69. # How much room was there? The child bucket would have had 20,
  70. # but the parent bucket only ten (so no, it wouldn't make too much
  71. # sense to have a child bucket draining faster than its parent in a real
  72. # application.)
  73. self.assertEqual(10, fit)
  74. # TODO: Test the Transport stuff?
  75. class ConsumerShaperTests(TestBucketBase):
  76. def setUp(self):
  77. TestBucketBase.setUp(self)
  78. self.underlying = DummyConsumer()
  79. self.bucket = SomeBucket()
  80. self.shaped = htb.ShapedConsumer(self.underlying, self.bucket)
  81. def testRate(self):
  82. # Start off with a full bucket, so the burst-size doesn't factor in
  83. # to the calculations.
  84. delta_t = 10
  85. self.bucket.add(100)
  86. self.shaped.write("x" * 100)
  87. self.clock.set(delta_t)
  88. self.shaped.resumeProducing()
  89. self.assertEqual(len(self.underlying.getvalue()), delta_t * self.bucket.rate)
  90. def testBucketRefs(self):
  91. self.assertEqual(self.bucket._refcount, 1)
  92. self.shaped.stopProducing()
  93. self.assertEqual(self.bucket._refcount, 0)