|
| 1 | +# Copyright 2025 The Orbax Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import time |
| 17 | +import unittest |
| 18 | +from unittest import mock |
| 19 | +from absl.testing import absltest |
| 20 | +from etils import epath |
| 21 | +from orbax.checkpoint._src.path import async_path |
| 22 | +from orbax.checkpoint._src.path import atomicity |
| 23 | +from orbax.checkpoint.experimental.v1._src.path import async_utils |
| 24 | + |
| 25 | + |
| 26 | +class AsyncUtilsTest(absltest.TestCase, unittest.IsolatedAsyncioTestCase): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + super().setUp() |
| 30 | + |
| 31 | + orig_create_paths = atomicity._create_paths |
| 32 | + |
| 33 | + async def _sleep_and_create_paths( |
| 34 | + *args, |
| 35 | + **kwargs, |
| 36 | + ): |
| 37 | + await asyncio.sleep(1) |
| 38 | + return await orig_create_paths(*args, **kwargs) |
| 39 | + |
| 40 | + self.enter_context( |
| 41 | + mock.patch.object( |
| 42 | + atomicity, '_create_paths', new=_sleep_and_create_paths |
| 43 | + ) |
| 44 | + ) |
| 45 | + self.directory = epath.Path(self.create_tempdir().full_path) |
| 46 | + |
| 47 | + async def assertExists(self, path: epath.Path): |
| 48 | + self.assertTrue(await async_path.exists(path)) |
| 49 | + |
| 50 | + async def assertNotExists(self, path: epath.Path): |
| 51 | + self.assertFalse(await async_path.exists(path)) |
| 52 | + |
| 53 | + def assertBetween(self, a, b, c): |
| 54 | + self.assertGreater(b, a) |
| 55 | + self.assertGreater(c, b) |
| 56 | + |
| 57 | + async def test_async_mkdir(self): |
| 58 | + tmpdir = atomicity.AtomicRenameTemporaryPath( |
| 59 | + self.directory / 'tmp', self.directory / 'final' |
| 60 | + ) |
| 61 | + start = time.time() |
| 62 | + p = async_utils.start_async_mkdir(tmpdir, operation_id='op1') |
| 63 | + await p.await_creation() |
| 64 | + self.assertBetween(1, time.time() - start, 2) |
| 65 | + await self.assertExists(self.directory / 'tmp') |
| 66 | + |
| 67 | + async def test_async_mkdir_with_subdirectories(self): |
| 68 | + tmpdir = atomicity.AtomicRenameTemporaryPath( |
| 69 | + self.directory / 'tmp', self.directory / 'final' |
| 70 | + ) |
| 71 | + start = time.time() |
| 72 | + p = async_utils.start_async_mkdir(tmpdir, ['a', 'b'], operation_id='op1') |
| 73 | + await p.await_creation() |
| 74 | + self.assertBetween(1, time.time() - start, 2) |
| 75 | + await self.assertExists(self.directory / 'tmp') |
| 76 | + await self.assertExists(self.directory / 'tmp' / 'a') |
| 77 | + await self.assertExists(self.directory / 'tmp' / 'b') |
| 78 | + |
| 79 | + async def test_async_mkdir_sequential(self): |
| 80 | + tmpdir1 = atomicity.AtomicRenameTemporaryPath( |
| 81 | + self.directory / 'tmp1', self.directory / 'final1' |
| 82 | + ) |
| 83 | + tmpdir2 = atomicity.AtomicRenameTemporaryPath( |
| 84 | + self.directory / 'tmp2', self.directory / 'final2' |
| 85 | + ) |
| 86 | + start = time.time() |
| 87 | + p1 = async_utils.start_async_mkdir(tmpdir1, operation_id='op1') |
| 88 | + p2 = async_utils.start_async_mkdir(tmpdir2, operation_id='op2') |
| 89 | + await p1.await_creation() |
| 90 | + await p2.await_creation() |
| 91 | + # Awaiting sequentially does not take any longer than awaiting in parallel, |
| 92 | + # because the operations are already in progress. |
| 93 | + self.assertBetween(1, time.time() - start, 2) |
| 94 | + await self.assertExists(self.directory / 'tmp1') |
| 95 | + await self.assertExists(self.directory / 'tmp2') |
| 96 | + |
| 97 | + async def test_async_mkdir_parallel(self): |
| 98 | + tmpdir1 = atomicity.AtomicRenameTemporaryPath( |
| 99 | + self.directory / 'tmp1', self.directory / 'final1' |
| 100 | + ) |
| 101 | + tmpdir2 = atomicity.AtomicRenameTemporaryPath( |
| 102 | + self.directory / 'tmp2', self.directory / 'final2' |
| 103 | + ) |
| 104 | + start = time.time() |
| 105 | + p1 = async_utils.start_async_mkdir(tmpdir1, operation_id='op1') |
| 106 | + p2 = async_utils.start_async_mkdir(tmpdir2, operation_id='op2') |
| 107 | + await asyncio.gather(p1.await_creation(), p2.await_creation()) |
| 108 | + self.assertBetween(1, time.time() - start, 2) |
| 109 | + await self.assertExists(self.directory / 'tmp1') |
| 110 | + await self.assertExists(self.directory / 'tmp2') |
| 111 | + |
| 112 | + async def test_async_mkdir_with_delayed_wait(self): |
| 113 | + tmpdir = atomicity.AtomicRenameTemporaryPath( |
| 114 | + self.directory / 'tmp', self.directory / 'final' |
| 115 | + ) |
| 116 | + p = async_utils.start_async_mkdir(tmpdir, operation_id='op1') |
| 117 | + await self.assertNotExists(self.directory / 'tmp') |
| 118 | + await asyncio.sleep(1) |
| 119 | + await p.await_creation() |
| 120 | + await self.assertExists(self.directory / 'tmp') |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + absltest.main() |
0 commit comments