Skip to content

Commit

Permalink
Fix #89 (#90)
Browse files Browse the repository at this point in the history
* Make memoize_s3 test less flaky

Instead of tests using the same cache directory under the fictitious S3
user `unittest`, the harness now appends `id(self)`. This makes
collisions much less likely when the same test is run in parallel.

Fix #89

* Annotate away Windows-only test failure
  • Loading branch information
atspaeth authored Aug 30, 2024
1 parent 2bb716b commit 53f1f08
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions tests/test_memoize_s3.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import sys
import unittest
import pytest

from unittest import mock

import pytest
from botocore.exceptions import ClientError

from braingeneers.utils.configure import skip_unittest_if_offline
from braingeneers.utils.memoize_s3 import memoize


# These have to all ignore UserWarnings because joblib generates them whenever
# the store backend takes more than a few hundred ms, which S3 often does.
@pytest.mark.filterwarnings("ignore::UserWarning")
class TestMemoizeS3(unittest.TestCase):
@skip_unittest_if_offline
@unittest.skip(reason="TODO: Passes rarely. Extremely flaky and needs fixing.")
@unittest.skipIf(sys.platform.startswith("win"), "TODO: Test is broken on Windows.")
def test(self):
# Run these checks in a context where S3_USER is set.
with mock.patch.dict("os.environ", {"S3_USER": "unittest"}):
unique_user = f"unittest-{id(self)}"
with mock.patch.dict("os.environ", {"S3_USER": unique_user}):
# Memoize a function that counts its calls.
@memoize()(ignore=["y"])
def foo(x, y):
Expand All @@ -23,7 +27,8 @@ def foo(x, y):
return x

self.assertEqual(
foo.store_backend.location, "s3://braingeneersdev/unittest/cache/joblib"
foo.store_backend.location,
f"s3://braingeneersdev/{unique_user}/cache/joblib",
)

# Call it a few times and make sure it only runs once.
Expand All @@ -38,7 +43,8 @@ def foo(x, y):
self.assertEqual(side_effect, 2)

# Clean up by reaching into the cache and clearing the directory
# without recreating the cache.
# without recreating the cache. This is important to avoid
# cluttering with fake user directories after tests are done.
foo.store_backend.clear()

@skip_unittest_if_offline
Expand Down

0 comments on commit 53f1f08

Please sign in to comment.