diff --git a/tests/test_multiprocessing.py b/tests/test_multiprocessing.py index a749246c..f0cdd6c2 100644 --- a/tests/test_multiprocessing.py +++ b/tests/test_multiprocessing.py @@ -29,7 +29,14 @@ def r(self, request): valkey.Valkey, request=request, single_connection_client=False ) - def test_close_connection_in_child(self, master_host): + @pytest.fixture() + def multiprocessing_ctx(self): + curr_start_method = multiprocessing.get_start_method(allow_none=False) + if curr_start_method == "forkserver": + return multiprocessing.get_context("fork") + return multiprocessing.get_context(curr_start_method) + + def test_close_connection_in_child(self, master_host, multiprocessing_ctx): """ A connection owned by a parent and closed by a child doesn't destroy the file descriptors so a parent can still use it. @@ -43,7 +50,7 @@ def target(conn): assert conn.read_response() == b"PONG" conn.disconnect() - proc = multiprocessing.Process(target=target, args=(conn,)) + proc = multiprocessing_ctx.Process(target=target, args=(conn,)) proc.start() proc.join(3) assert proc.exitcode == 0 @@ -55,7 +62,7 @@ def target(conn): conn.send_command("ping") assert conn.read_response() == b"PONG" - def test_close_connection_in_parent(self, master_host): + def test_close_connection_in_parent(self, master_host, multiprocessing_ctx): """ A connection owned by a parent is unusable by a child if the parent (the owning process) closes the connection. @@ -73,7 +80,7 @@ def target(conn, ev): conn.send_command("ping") ev = multiprocessing.Event() - proc = multiprocessing.Process(target=target, args=(conn, ev)) + proc = multiprocessing_ctx.Process(target=target, args=(conn, ev)) proc.start() conn.disconnect() @@ -83,7 +90,7 @@ def target(conn, ev): assert proc.exitcode == 0 @pytest.mark.parametrize("max_connections", [1, 2, None]) - def test_pool(self, max_connections, master_host): + def test_pool(self, max_connections, master_host, multiprocessing_ctx): """ A child will create its own connections when using a pool created by a parent. @@ -107,7 +114,7 @@ def target(pool): assert conn.send_command("ping") is None assert conn.read_response() == b"PONG" - proc = multiprocessing.Process(target=target, args=(pool,)) + proc = multiprocessing_ctx.Process(target=target, args=(pool,)) proc.start() proc.join(3) assert proc.exitcode == 0 @@ -120,7 +127,9 @@ def target(pool): assert conn.read_response() == b"PONG" @pytest.mark.parametrize("max_connections", [1, 2, None]) - def test_close_pool_in_main(self, max_connections, master_host): + def test_close_pool_in_main( + self, max_connections, master_host, multiprocessing_ctx + ): """ A child process that uses the same pool as its parent isn't affected when the parent disconnects all connections within the pool. @@ -145,7 +154,7 @@ def target(pool, disconnect_event): ev = multiprocessing.Event() - proc = multiprocessing.Process(target=target, args=(pool, ev)) + proc = multiprocessing_ctx.Process(target=target, args=(pool, ev)) proc.start() pool.disconnect() @@ -153,7 +162,7 @@ def target(pool, disconnect_event): proc.join(3) assert proc.exitcode == 0 - def test_valkey_client(self, r): + def test_valkey_client(self, r, multiprocessing_ctx): "A valkey client created in a parent can also be used in a child" assert r.ping() is True @@ -161,7 +170,7 @@ def target(client): assert client.ping() is True del client - proc = multiprocessing.Process(target=target, args=(r,)) + proc = multiprocessing_ctx.Process(target=target, args=(r,)) proc.start() proc.join(3) assert proc.exitcode == 0