Skip to content

Commit

Permalink
"slave" -> "worker" for xdist compat
Browse files Browse the repository at this point in the history
Closes #34.
  • Loading branch information
Zac-HD committed Jun 16, 2020
1 parent 9a085e7 commit edf4728
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -11,3 +11,4 @@ These people have contributed to `pytest-services`, in alphabetical order:
* `Jason R. Coombs <jaraco@jaraco.com>`_
* `Joep van Dijken <joepvandijken@github.com>`_
* `Oleg Pidsadnyi <oleg.pidsadnyi@gmail.com>`_
* `Zac Hatfield-Dodds <zac@zhd.dev>`_
6 changes: 6 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,12 @@
Changelog
=========

2.1.0
-----

- #34: Deprecated ``slave_id`` fixture in favor of ``worker_id``,
for compatibility with ``pytest-xdist`` 2.

2.0.1
-----

Expand Down
5 changes: 3 additions & 2 deletions README.rst
Expand Up @@ -48,9 +48,10 @@ Fixtures
Infrastructure fixtures
***********************

* slave_id
Id of the slave if tests are run using pytest-xdist_. It is set to `local` if tests are not run using
* worker_id
Id of the worker if tests are run using pytest-xdist_. It is set to `local` if tests are not run using
pytest-xdist_ (with `--dist` command line option set to `load`).
Has a deprecated alias ``slave_id`` which will be removed in a future version.
* session_id
Test session id. Globally unique, and of course also guaranteed to be different for potentially multiple test
sessions running on same test node via pytest-xdist_.
Expand Down
6 changes: 3 additions & 3 deletions pytest_services/log.py
Expand Up @@ -8,8 +8,8 @@


@pytest.fixture(scope='session')
def services_log(slave_id):
"""A services_logger with the slave id."""
def services_log(worker_id):
"""A services_logger with the worker id."""
handler = None
for kwargs in (dict(socktype=socket.SOCK_RAW), dict(socktype=socket.SOCK_STREAM), dict()):
try:
Expand All @@ -18,7 +18,7 @@ def services_log(slave_id):
break
except (IOError, TypeError):
pass
logger = logging.getLogger('[{slave_id}] {name}'.format(name=__name__, slave_id=slave_id))
logger = logging.getLogger('[{worker_id}] {name}'.format(name=__name__, worker_id=worker_id))
logger.setLevel(logging.DEBUG)
if handler and workaround_issue_20(handler):
logger.propagate = 0
Expand Down
23 changes: 15 additions & 8 deletions pytest_services/service.py
Expand Up @@ -15,36 +15,43 @@


@pytest.fixture(scope='session')
def run_services(request, slave_id):
def run_services(request, worker_id):
"""Indicate whether the services should run or not."""
return slave_id != 'local' or request.config.option.run_services
return worker_id != 'local' or request.config.option.run_services


@pytest.fixture(scope='session')
def slave_id(request):
def worker_id(request):
"""
The id of the slave if tests are run using xdist.
The id of the worker if tests are run using xdist.
It is set to `'local'` if tests are not run using xdist.
The id is unique for a test run. An id may clash if there are two workers
that belong to different test sessions.
"""
return WRONG_FILE_NAME_CHARS_RE.sub('_', getattr(request.config, 'slaveinput', {}).get('slaveid', 'local'))
return WRONG_FILE_NAME_CHARS_RE.sub('_', getattr(request.config, 'workerinput', {}).get('workerid', 'local'))


@pytest.fixture(scope='session')
def session_id(request, slave_id, run_services):
def slave_id(request, worker_id):
msg = "The `slave_id` fixture is deprecated; use `worker_id` instead."
warnings.warn(msg, DeprecationWarning)
return worker_id


@pytest.fixture(scope='session')
def session_id(request, worker_id, run_services):
"""The test session id.
It is supposed to be globally unique.
"""
# UUID should be enough, other fields are added for the debugging purposes.
session_id = '{random}-{slave_id}'.format(
session_id = '{random}-{worker_id}'.format(
random=uuid.uuid4().hex,
slave_id=slave_id,
worker_id=worker_id,
)

return session_id
Expand Down

0 comments on commit edf4728

Please sign in to comment.