Jason R. Coombs avatar Jason R. Coombs committed 6ec9561

Added tests for the throttler

Comments (0)

Files changed (1)

irc/tests/test_client.py

+from __future__ import print_function
+
+import itertools
+import time
+
 import pytest
 import mock
 
 		handler2 = irc.client.PrioritizedHandler(1, lambda: 'other')
 		assert not handler1 < handler2
 		assert not handler2 < handler1
+
+class TestThrottler(object):
+	def test_function_throttled(self):
+		"""
+		Ensure the throttler actually throttles calls.
+		"""
+		# set up a function to be called
+		counter = itertools.count()
+		# set up a version of `next` that is only called 30 times per second
+		limited_next = irc.client.Throttler(next, 30)
+		# for one second, call next as fast as possible
+		deadline = time.time() + 1
+		while time.time() < deadline:
+			limited_next(counter)
+		# ensure the counter was advanced about 30 times
+		assert 29 <= next(counter) <= 31
+
+	def test_reconstruct_unwraps(self):
+		"""
+		The throttler should be re-usable - if one wants to throttle a
+		function that's aready throttled, the original function should be
+		used.
+		"""
+		wrapped = irc.client.Throttler(next, 30)
+		wrapped_again = irc.client.Throttler(wrapped, 60)
+		assert wrapped_again.func is next
+		assert wrapped_again.max_rate == 60
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.