diff --git a/HACKING.rst b/HACKING.rst index 49bda8500597..8f84aa820f09 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -77,6 +77,8 @@ Nova Specific Commandments - [N370] Don't use or import six - [N371] You must explicitly import python's mock: ``from unittest import mock`` - [N372] Don't use the setDaemon method. Use the daemon attribute instead. +- [N373] Don't use eventlet specific concurrency primitives. Use the one + from stdlib instead. E.g. eventlet.sleep => time.sleep Creating Unit Tests ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 704538250fe6..c7dea258bb08 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -143,6 +143,8 @@ rwlock_re = re.compile( six_re = re.compile(r"^(import six(\..*)?|from six(\..*)? import .*)$") # Regex for catching the setDaemon method set_daemon_re = re.compile(r"\.setDaemon\(") +eventlet_stdlib_primitives_re = re.compile( + r".*(eventlet|greenthread)\.sleep\(.*") class BaseASTChecker(ast.NodeVisitor): @@ -1099,3 +1101,20 @@ def check_set_daemon(logical_line): if res: yield (0, "N372: Don't use the setDaemon method. " "Use the daemon attribute instead.") + + +@core.flake8ext +def check_eventlet_primitives(logical_line, filename): + """Check for use of any eventlet primitives where the stdlib equivalent + should be used + + N373 + """ + msg = ( + "N373: Use the stdlib concurrency primitive instead of the Eventelt " + "specific one") + + match = re.match(eventlet_stdlib_primitives_re, logical_line) + + if match: + yield (0, msg) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 7dbb36484b9d..899e1ab938f9 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -1064,3 +1064,20 @@ class HackingTestCase(test.NoDBTestCase): self.thread.setdaemon(True) """ self._assert_has_no_errors(code, checks.check_set_daemon) + + def test_check_eventlet_primitives(self): + code = """ + eventlet.sleep(0) + eventlet.sleep(1) + greenthread.sleep(0) + greenthread.sleep(1) + """ + errors = [(x + 1, 0, 'N373') for x in range(4)] + self._assert_has_errors( + code, checks.check_eventlet_primitives, expected_errors=errors) + + code = """ + time.sleep(0) + time.sleep(1) + """ + self._assert_has_no_errors(code, checks.check_eventlet_primitives) diff --git a/tox.ini b/tox.ini index 0c5d29535a19..941f3639b183 100644 --- a/tox.ini +++ b/tox.ini @@ -358,6 +358,7 @@ extension = N370 = checks:check_six N371 = checks:import_stock_mock N372 = checks:check_set_daemon + N373 = checks:check_eventlet_primitives paths = ./nova/hacking