|
1 | 1 | from concurrent.futures import ThreadPoolExecutor |
2 | 2 | from contextlib import suppress |
3 | 3 | from io import BytesIO |
| 4 | +from pathlib import Path |
4 | 5 | from types import SimpleNamespace |
5 | 6 |
|
6 | 7 | import pytest |
|
10 | 11 | from devito.arch import BLACKWELL, NVIDIAX, archinfo, platform_registry |
11 | 12 | from devito.arch.archinfo import AppleArm, get_apple_platform |
12 | 13 | from devito.arch.compiler import ( |
13 | | - CudaCompiler, GNUCompiler, compiler_registry, sniff_compiler_version |
| 14 | + Compiler, CudaCompiler, GNUCompiler, compiler_registry, sniff_compiler_version |
14 | 15 | ) |
| 16 | +from devito.tools import os_helper |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @pytest.mark.parametrize("cc", [ |
@@ -151,3 +153,41 @@ def popen(command, **kwargs): |
151 | 153 |
|
152 | 154 | assert info[f'mem.{memory}'](0) == 1024**3 |
153 | 155 | assert info[f'mem.{memory}'](1) == 80*1024**3 |
| 156 | + |
| 157 | + |
| 158 | +class TestLibraryPublication: |
| 159 | + """Concurrent Operator restoration must never expose a partial library.""" |
| 160 | + |
| 161 | + def test_complete_binary(self, tmp_path, monkeypatch): |
| 162 | + compiler = SimpleNamespace(get_jit_dir=lambda: tmp_path, so_ext='.so') |
| 163 | + target = tmp_path/'shared.so' |
| 164 | + binary = b'ELF-binary-content'*1024 |
| 165 | + original = os_helper.os.link |
| 166 | + publications = [] |
| 167 | + |
| 168 | + def observe(source, destination): |
| 169 | + assert not target.exists() |
| 170 | + assert Path(source).read_bytes() == binary |
| 171 | + publications.append(source) |
| 172 | + original(source, destination) |
| 173 | + |
| 174 | + monkeypatch.setattr(os_helper.os, 'link', observe) |
| 175 | + Compiler.save(compiler, 'shared', binary) |
| 176 | + assert publications |
| 177 | + assert target.read_bytes() == binary |
| 178 | + assert list(tmp_path.iterdir()) == [target] |
| 179 | + Compiler.save(compiler, 'shared', b'different') |
| 180 | + assert target.read_bytes() == binary |
| 181 | + |
| 182 | + def test_concurrent_binary(self, tmp_path): |
| 183 | + compiler = SimpleNamespace(get_jit_dir=lambda: tmp_path, so_ext='.so') |
| 184 | + target = tmp_path/'shared.so' |
| 185 | + binary = b'ELF-binary-content'*(1 << 18) |
| 186 | + |
| 187 | + def restore(_): |
| 188 | + Compiler.save(compiler, 'shared', binary) |
| 189 | + assert target.read_bytes() == binary |
| 190 | + |
| 191 | + with ThreadPoolExecutor(max_workers=8) as pool: |
| 192 | + list(pool.map(restore, range(32))) |
| 193 | + assert list(tmp_path.iterdir()) == [target] |
0 commit comments