Files
urlbot-native/test_urlbot.py

58 lines
1.7 KiB
Python
Raw Normal View History

"""
To be executed with nose
2015-12-20 15:24:42 +01:00
TODO: test all plugins, maybe declare their sample input somewhere near the code
"""
import unittest
2015-11-28 01:16:54 +01:00
import time
from common import buckets, rate_limit, RATE_GLOBAL
2015-11-28 01:16:54 +01:00
class TestEventlooper(unittest.TestCase):
2015-11-30 19:17:40 +01:00
def test_broken_url(self):
"""
Test that broken socket calls are not breaking
"""
from common import fetch_page
broken_url = 'http://foo'
result = fetch_page(url=broken_url)
self.assertEqual(result, (None, None))
2015-11-28 01:16:54 +01:00
from collections import namedtuple
Bucket = namedtuple("BucketConfig", ["history", "period", "max_hist_len"])
class TestRateLimiting(unittest.TestCase):
2015-11-30 19:17:40 +01:00
def setUp(self):
# just for assertions
self.called = {
RATE_GLOBAL: [],
}
def say(self, msg, rate_class=RATE_GLOBAL):
if rate_limit(rate_class):
self.called[rate_class].append(msg)
# print(msg)
time.sleep(0.1)
def test_simple_burst(self):
messages = ["x_%d" % i for i in range(1, 9)]
for m in messages:
self.say(msg=m)
self.assertEqual(messages[:buckets[RATE_GLOBAL].max_hist_len], self.called[RATE_GLOBAL])
def test_msg_two_bursts(self):
# custom bucket, just for testing
buckets[0x42] = Bucket(history=[], period=1, max_hist_len=5)
self.called[0x42] = []
bucket = buckets[0x42]
messages = ["x_%d" % i for i in range(0, 15)]
for i, m in enumerate(messages):
if i % bucket.max_hist_len == 0:
time.sleep(bucket.period)
self.say(msg=m, rate_class=0x42)
self.assertEqual(messages, self.called[0x42])