diff --git a/.coveragerc b/.coveragerc index 4f0ef0cc5..a9e3cd02c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -31,9 +31,7 @@ omit = */faust/tables/recovery.py # not needed - */faust/utils/functional.py */faust/utils/iso8601.py - */faust/utils/platforms.py */faust/utils/tracing.py */faust/types/* */faust/__main__.py diff --git a/tests/unit/utils/test_agent_stopper.py b/tests/unit/utils/test_agent_stopper.py new file mode 100644 index 000000000..5020f2fcc --- /dev/null +++ b/tests/unit/utils/test_agent_stopper.py @@ -0,0 +1,13 @@ +from unittest.mock import Mock + +import pytest + +from faust.utils.agent_stopper import agent_stopper + + +@pytest.mark.asyncio +async def test_agent_stopper_crashes_app(): + app = Mock(name="app") + await agent_stopper(app) + # It must force a non-zero exit by crashing the app with a RuntimeError. + app._crash.assert_called_once_with(RuntimeError) diff --git a/tests/unit/utils/test_functional.py b/tests/unit/utils/test_functional.py index e24f91a36..c6fa89b00 100644 --- a/tests/unit/utils/test_functional.py +++ b/tests/unit/utils/test_functional.py @@ -1,6 +1,6 @@ import pytest -from faust.utils.functional import consecutive_numbers +from faust.utils.functional import consecutive_numbers, translate @pytest.mark.parametrize( @@ -14,3 +14,17 @@ ) def test_consecutive_numbers(numbers, expected): assert next(consecutive_numbers(numbers), None) == expected + + +@pytest.mark.parametrize( + "table,s,expected", + [ + ({".": "_", "@": "."}, "foo.bar@baz", "foo_bar.baz"), + ({".": "_"}, "foo.bar", "foo_bar"), + # multi-character patterns/replacements, not just single chars + ({"foo": "bar"}, "foofoo", "barbar"), + ({}, "unchanged", "unchanged"), + ], +) +def test_translate(table, s, expected): + assert translate(table, s) == expected diff --git a/tests/unit/utils/test_platforms.py b/tests/unit/utils/test_platforms.py new file mode 100644 index 000000000..1b05a33a6 --- /dev/null +++ b/tests/unit/utils/test_platforms.py @@ -0,0 +1,35 @@ +import builtins +import resource +from unittest.mock import patch + +from faust.utils.platforms import max_open_files + + +@patch("resource.getrlimit", return_value=(1024, 4096)) +def test_max_open_files__returns_hard_limit(getrlimit): + assert max_open_files() == 4096 + + +@patch("platform.system", return_value="Linux") +@patch("resource.getrlimit", return_value=(1024, resource.RLIM_INFINITY)) +def test_max_open_files__infinity_non_darwin(getrlimit, system): + assert max_open_files() is None + + +@patch("subprocess.check_output", return_value=b"kern.maxfilesperproc: 24576") +@patch("platform.system", return_value="Darwin") +@patch("resource.getrlimit", return_value=(1024, resource.RLIM_INFINITY)) +def test_max_open_files__infinity_darwin_uses_sysctl(getrlimit, system, check_output): + assert max_open_files() == 24576 + + +def test_max_open_files__no_resource_module(): + real_import = builtins.__import__ + + def fake_import(name, *args, **kwargs): + if name == "resource": + raise ImportError("no resource module") + return real_import(name, *args, **kwargs) + + with patch("builtins.__import__", side_effect=fake_import): + assert max_open_files() is None