Skip to content

Commit

Permalink
Merge pull request #189 from porink0424/feat/test-free-threaded
Browse files Browse the repository at this point in the history
Add tests for free-threaded python
  • Loading branch information
c-bata authored Oct 30, 2024
2 parents 0254e36 + 9ece7b1 commit 766ffe9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
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/action@v3.1.0
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

0 comments on commit 766ffe9

Please sign in to comment.