From 6a2f5a0bb39f0af085916cff1222b5b41b6ff9fa Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Wed, 17 Feb 2016 20:07:24 +0100 Subject: [PATCH] Fix wrong comparison in reject_when_reached We say we raise when backlog goes past max size, but actually we raise when backlogs tries to reach the max size. This patch fixes reject_when_reached to actually allow max size to be reached, but no more. Also adding missing unit test for reject_when_reached. Change-Id: Ide835b6e5fd15171b9bebc7ca112b84423b39fab --- futurist/rejection.py | 2 +- futurist/tests/test_executors.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/futurist/rejection.py b/futurist/rejection.py index 3ea19f8..5bb5105 100644 --- a/futurist/rejection.py +++ b/futurist/rejection.py @@ -23,7 +23,7 @@ def reject_when_reached(max_backlog): """Returns a function that will raise when backlog goes past max size.""" def _rejector(executor, backlog): - if backlog + 1 >= max_backlog: + if backlog >= max_backlog: raise futurist.RejectedSubmission("Current backlog %s is not" " allowed to go" " beyond %s" % (backlog, diff --git a/futurist/tests/test_executors.py b/futurist/tests/test_executors.py index f7311e5..c1ee549 100644 --- a/futurist/tests/test_executors.py +++ b/futurist/tests/test_executors.py @@ -18,6 +18,7 @@ import testscenarios from testtools import testcase import futurist +from futurist import rejection from futurist.tests import base @@ -137,3 +138,31 @@ class TestExecutors(testscenarios.TestWithScenarios, base.TestCase): self.assertEqual(10, len(happy_completed) + len(unhappy_completed)) self.assertEqual(5, len(unhappy_completed)) self.assertEqual(5, len(happy_completed)) + + +_REJECTION = rejection.reject_when_reached(1) + + +class TestRejection(testscenarios.TestWithScenarios, base.TestCase): + scenarios = [ + ('green', {'executor_cls': futurist.GreenThreadPoolExecutor, + 'executor_kwargs': {'check_and_reject': _REJECTION, + 'max_workers': 1}}), + ('thread', {'executor_cls': futurist.ThreadPoolExecutor, + 'executor_kwargs': {'check_and_reject': _REJECTION, + 'max_workers': 1}}), + ] + + def setUp(self): + super(TestRejection, self).setUp() + self.executor = self.executor_cls(**self.executor_kwargs) + + def test_rejection(self): + self.addCleanup(self.executor.shutdown) + + # 1 worker + 1 item of backlog + for _i in range(2): + self.executor.submit(delayed, 0.5) + + self.assertRaises(futurist.RejectedSubmission, + self.executor.submit, returns_one)