Skip to content

Commit 76014cc

Browse files
lukebaumanncopybara-github
authored andcommitted
Add cleanup to restore JAX config and environment variables in tests.
PiperOrigin-RevId: 875255664
1 parent 33458bf commit 76014cc

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

pathwaysutils/test/initialize_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222

2323
class InitializeTest(parameterized.TestCase):
2424

25+
def setUp(self):
26+
super().setUp()
27+
28+
orig_jax_platforms = getattr(jax.config, "jax_platforms", None)
29+
self.addCleanup(jax.config.update, "jax_platforms", orig_jax_platforms)
30+
31+
self._orig_environ = os.environ.copy()
32+
self.addCleanup(self._restore_environ)
33+
34+
def _restore_environ(self):
35+
os.environ.clear()
36+
os.environ.update(self._orig_environ)
37+
2538
def test_first_initialize(self):
2639
jax.config.update("jax_platforms", "proxy")
2740
_initialize._initialization_count = 0

pathwaysutils/test/persistence_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ class PersistenceTest(absltest.TestCase):
1616
timeout = datetime.timedelta(seconds=3)
1717

1818
def setUp(self):
19-
jax.config.update("jax_platforms", "cpu")
2019
super().setUp()
2120

21+
orig_jax_platforms = getattr(jax.config, "jax_platforms", None)
22+
self.addCleanup(jax.config.update, "jax_platforms", orig_jax_platforms)
23+
24+
jax.config.update("jax_platforms", "cpu")
25+
2226
def test_get_read_request(self):
2327
devices = jax.devices()
2428
# 1d sharding

pathwaysutils/test/plugin_executable_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
class PluginExecutableTest(absltest.TestCase):
1515

1616
def setUp(self):
17-
jax.config.update("jax_platforms", "cpu")
1817
super().setUp()
1918

19+
orig_jax_platforms = getattr(jax.config, "jax_platforms", None)
20+
self.addCleanup(jax.config.update, "jax_platforms", orig_jax_platforms)
21+
22+
jax.config.update("jax_platforms", "cpu")
23+
2024
def test_bad_json_program(self):
2125
with self.assertRaisesRegex(XlaRuntimeError, "INVALID_ARGUMENT"):
2226
PluginExecutable('{"printTextRequest":{"badParamName":"foo"}}')

pathwaysutils/test/proxy_backend_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ class ProxyBackendTest(absltest.TestCase):
2626

2727
def setUp(self):
2828
super().setUp()
29+
orig_jax_platforms = getattr(jax.config, "jax_platforms", None)
30+
orig_jax_backend_target = getattr(jax.config, "jax_backend_target", None)
31+
32+
self.addCleanup(jax.config.update, "jax_platforms", orig_jax_platforms)
33+
self.addCleanup(
34+
jax.config.update, "jax_backend_target", orig_jax_backend_target
35+
)
36+
self.addCleanup(backend.clear_backends)
37+
2938
jax.config.update("jax_platforms", "proxy")
3039
jax.config.update("jax_backend_target", "grpc://localhost:12345")
3140
backend.clear_backends()

0 commit comments

Comments
 (0)