Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for free-threaded python #189

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,28 @@ jobs:
architecture: x64
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools numpy scipy hypothesis
python -m pip install --upgrade pip setuptools numpy scipy hypothesis pytest
pip install --progress-bar off .
- run: python -m unittest
- run: python -m pytest tests --ignore=tests/test_free_threaded.py
test-free-threaded:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# TODO: Replace deadsnakes with setup-python when the support for Python 3.13t is added
- name: Setup Python 3.13t
uses: deadsnakes/[email protected]
with:
python-version: "3.13-dev"
nogil: true
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools numpy hypothesis pytest pytest-freethreaded
pip install --progress-bar off .
- run: python -m pytest --threads 1 --iterations 1 tests --ignore=tests/test_free_threaded.py
# TODO: Using `unittest` style causes `pytest-freethreaded` to fail with `ConcurrencyError`.
# Rewriting as top-level functions works,
# so a follow-up is needed to refactor from `unittest` to `pytest`.
- run: python -m pytest --threads 1 --iterations 1 --require-gil-disabled tests/test_free_threaded.py
test-numpy2:
runs-on: ubuntu-latest
steps:
Expand All @@ -51,7 +70,7 @@ jobs:
architecture: x64
- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools scipy hypothesis
python -m pip install --upgrade pip setuptools scipy hypothesis pytest
python -m pip install --pre --upgrade numpy
pip install --progress-bar off .
- run: python -m unittest
- run: python -m pytest tests --ignore=tests/test_free_threaded.py
22 changes: 22 additions & 0 deletions tests/test_free_threaded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import numpy as np
import pytest
from cmaes import CMA


@pytest.mark.freethreaded(threads=10, iterations=200)
def test_simple_optimization():
optimizer = CMA(mean=np.zeros(2), sigma=1.3)

def quadratic(x1: float, x2: float) -> float:
return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2

while True:
solutions = []
for _ in range(optimizer.population_size):
x = optimizer.ask()
value = quadratic(x[0], x[1])
solutions.append((x, value))
optimizer.tell(solutions)

if optimizer.should_stop():
break
Loading