From 88e0c9f792e414735c913139c33e59d030779fc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kriszti=C3=A1n=20Sz=C5=B1cs?= Date: Mon, 23 Sep 2019 22:32:03 +0200 Subject: [PATCH] py2 compat --- python/pyarrow/tests/conftest.py | 16 ++++++++++------ python/pyarrow/tests/test_flight.py | 12 +----------- python/pyarrow/util.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/python/pyarrow/tests/conftest.py b/python/pyarrow/tests/conftest.py index 1fb57ffffb27b..59a276327c879 100644 --- a/python/pyarrow/tests/conftest.py +++ b/python/pyarrow/tests/conftest.py @@ -27,6 +27,8 @@ except ImportError: import pathlib2 as pathlib # py2 compat +from pyarrow.util import find_free_port + # setup hypothesis profiles h.settings.register_profile('ci', max_examples=1000) @@ -246,10 +248,10 @@ def __exit__(self, exc_type, exc_value, traceback): shutil.rmtree(self.tmp) -@pytest.fixture(scope='module') +@pytest.fixture(scope='session') @pytest.mark.s3 def minio_server(): - host, port = 'localhost', 9000 + host, port = 'localhost', find_free_port() access_key, secret_key = 'arrow', 'apachearrow' address = '{}:{}'.format(host, port) @@ -263,14 +265,16 @@ def minio_server(): with TemporaryDirectory() as tempdir: args = ['minio', '--compat', 'server', '--quiet', '--address', address, tempdir] - with subprocess.Popen(args, env=env) as proc: + try: + proc = subprocess.Popen(args, env=env) yield address, access_key, secret_key - proc.terminate() + finally: + proc.kill() except IOError: pytest.skip('`minio` command cannot be located') -@pytest.fixture(scope='module') +@pytest.fixture(scope='session') def minio_client(minio_server): minio = pytest.importorskip('minio') address, access_key, secret_key = minio_server @@ -282,7 +286,7 @@ def minio_client(minio_server): ) -@pytest.fixture(scope='module') +@pytest.fixture(scope='session') def minio_bucket(minio_client): bucket_name = 'pyarrow-bucket' minio_client.make_bucket(bucket_name) diff --git a/python/pyarrow/tests/test_flight.py b/python/pyarrow/tests/test_flight.py index 83afba6de65ec..40099b06aec63 100644 --- a/python/pyarrow/tests/test_flight.py +++ b/python/pyarrow/tests/test_flight.py @@ -17,9 +17,7 @@ # under the License. import base64 -import contextlib import os -import socket import struct import tempfile import threading @@ -30,7 +28,7 @@ import pyarrow as pa from pyarrow.compat import tobytes -from pyarrow.util import pathlib +from pyarrow.util import pathlib, find_free_port try: from pyarrow import flight @@ -48,14 +46,6 @@ pytestmark = pytest.mark.flight -def find_free_port(): - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - with contextlib.closing(sock) as sock: - sock.bind(('', 0)) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - return sock.getsockname()[1] - - def test_import(): # So we see the ImportError somewhere import pyarrow.flight # noqa diff --git a/python/pyarrow/util.py b/python/pyarrow/util.py index 5e4fb3579372a..7219a447f3565 100644 --- a/python/pyarrow/util.py +++ b/python/pyarrow/util.py @@ -19,8 +19,10 @@ from __future__ import absolute_import +import contextlib import functools import six +import socket import warnings @@ -125,3 +127,11 @@ def get_contiguous_span(shape, strides, itemsize): if end - start != itemsize * product(shape): raise ValueError('array data is non-contiguous') return start, end + + +def find_free_port(): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + with contextlib.closing(sock) as sock: + sock.bind(('', 0)) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + return sock.getsockname()[1]