Skip to content

Commit 6509728

Browse files
committed
fix resource contention issues in web tests
1 parent 551efdf commit 6509728

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

tests/test_web/test_webapi_mode.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
f, AX = plt.subplots()
4242

4343

44+
@pytest.fixture
45+
def unique_project_name(tmp_path):
46+
"""Generate unique project name using tmp_path's unique directory."""
47+
# tmp_path.name gives us something like 'test_upload0'
48+
return f"default_{tmp_path.name}"
49+
50+
4451
def make_mode_sim():
4552
"""Simple mode solver"""
4653

@@ -73,13 +80,13 @@ def set_api_key(monkeypatch):
7380

7481

7582
@pytest.fixture
76-
def mock_upload(monkeypatch, set_api_key):
83+
def mock_upload(monkeypatch, set_api_key, unique_project_name):
7784
"""Mocks webapi.upload."""
7885
responses.add(
7986
responses.GET,
8087
f"{Env.current.web_api_endpoint}/tidy3d/project",
81-
match=[matchers.query_param_matcher({"projectName": PROJECT_NAME})],
82-
json={"data": {"projectId": FOLDER_ID, "projectName": PROJECT_NAME}},
88+
match=[matchers.query_param_matcher({"projectName": unique_project_name})],
89+
json={"data": {"projectId": FOLDER_ID, "projectName": unique_project_name}},
8390
status=200,
8491
)
8592

@@ -274,21 +281,21 @@ def mock_webapi(
274281

275282

276283
@responses.activate
277-
def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata):
284+
def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata, unique_project_name):
278285
sim = make_mode_sim()
279286
assert sim != get_reduced_simulation(sim, reduce_simulation=True)
280-
assert upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=True)
287+
assert upload(sim, TASK_NAME, unique_project_name, reduce_simulation=True)
281288

282289

283290
@pytest.mark.parametrize("reduce_simulation", [True, False])
284291
@responses.activate
285292
def test_upload_with_reduction_parameter(
286-
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation
293+
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation, unique_project_name
287294
):
288295
"""Test that simulation reduction is properly applied before upload based on reduce_simulation parameter."""
289296
sim = make_mode_sim()
290297

291-
upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=reduce_simulation)
298+
upload(sim, TASK_NAME, unique_project_name, reduce_simulation=reduce_simulation)
292299

293300
if reduce_simulation:
294301
expected_sim = get_reduced_simulation(sim, reduce_simulation=True)
@@ -346,13 +353,13 @@ def mock_download(*args, **kwargs):
346353

347354

348355
@responses.activate
349-
def test_run(mock_webapi, monkeypatch, tmp_path):
356+
def test_run(mock_webapi, monkeypatch, tmp_path, unique_project_name):
350357
sim = make_mode_sim()
351358
monkeypatch.setattr(f"{api_path}.load", lambda *args, **kwargs: True)
352359
assert run(
353360
sim,
354361
task_name=TASK_NAME,
355-
folder_name=PROJECT_NAME,
362+
folder_name=unique_project_name,
356363
path=str(tmp_path / "web_test_tmp.json"),
357364
)
358365

@@ -380,10 +387,10 @@ def test_abort_task(set_api_key, mock_get_info):
380387

381388

382389
@responses.activate
383-
def test_job(mock_webapi, monkeypatch, tmp_path):
390+
def test_job(mock_webapi, monkeypatch, tmp_path, unique_project_name):
384391
monkeypatch.setattr("tidy3d.web.api.container.Job.load", lambda *args, **kwargs: True)
385392
sim = make_mode_sim()
386-
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=PROJECT_NAME)
393+
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=unique_project_name)
387394

388395
_ = j.run(path=str(tmp_path / "web_test_tmp.json"))
389396
_ = j.status
@@ -400,13 +407,13 @@ def mock_job_status(monkeypatch):
400407

401408

402409
@responses.activate
403-
def test_batch(mock_webapi, mock_job_status, tmp_path, monkeypatch):
410+
def test_batch(mock_webapi, mock_job_status, tmp_path, monkeypatch, unique_project_name):
404411
# monkeypatch.setattr("tidy3d.web.api.container.Batch.monitor", lambda self: time.sleep(0.1))
405412
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
406413
monkeypatch.setattr(f"{api_path}.load", lambda *args, **kwargs: True)
407414

408415
sims = {TASK_NAME: make_mode_sim()}
409-
b = Batch(simulations=sims, folder_name=PROJECT_NAME)
416+
b = Batch(simulations=sims, folder_name=unique_project_name)
410417
b.estimate_cost()
411418
_ = b.run(path_dir=str(tmp_path))
412419
assert b.real_cost() == FLEX_UNIT * len(sims)
@@ -416,16 +423,16 @@ def test_batch(mock_webapi, mock_job_status, tmp_path, monkeypatch):
416423

417424

418425
@responses.activate
419-
def test_async(mock_webapi, mock_job_status, monkeypatch):
426+
def test_async(mock_webapi, mock_job_status, monkeypatch, unique_project_name):
420427
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
421428
monkeypatch.setattr(f"{api_path}.load", lambda *args, **kwargs: True)
422429

423430
sims = {TASK_NAME: make_mode_sim()}
424-
_ = run_async(sims, folder_name=PROJECT_NAME)
431+
_ = run_async(sims, folder_name=unique_project_name)
425432

426433

427434
@responses.activate
428-
def test_patch_data(mock_webapi, monkeypatch, tmp_path):
435+
def test_patch_data(mock_webapi, monkeypatch, tmp_path, unique_project_name):
429436
"""Test that mode solver is patched with remote data after run"""
430437

431438
def get_sim_and_data():
@@ -454,7 +461,7 @@ def check_patched(result, sim, data_local, data_remote):
454461
result = run(
455462
sim,
456463
task_name=TASK_NAME,
457-
folder_name=PROJECT_NAME,
464+
folder_name=unique_project_name,
458465
path=str(tmp_path / "web_test_tmp.json"),
459466
)
460467

@@ -464,7 +471,7 @@ def check_patched(result, sim, data_local, data_remote):
464471

465472
sim, data_local, data_remote = get_sim_and_data()
466473

467-
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=PROJECT_NAME)
474+
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=unique_project_name)
468475

469476
result = j.run(path=str(tmp_path / "web_test_tmp.json"))
470477

@@ -475,7 +482,7 @@ def check_patched(result, sim, data_local, data_remote):
475482
sim, data_local, data_remote = get_sim_and_data()
476483

477484
sims = {TASK_NAME: sim}
478-
b = Batch(simulations=sims, folder_name=PROJECT_NAME)
485+
b = Batch(simulations=sims, folder_name=unique_project_name)
479486
result = b.run(path_dir=str(tmp_path))
480487

481488
check_patched(result[TASK_NAME], sim, data_local, data_remote)

tests/test_web/test_webapi_mode_sim.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ def make_mode_sim():
5959
return td.ModeSimulation.from_mode_solver(ms)
6060

6161

62+
@pytest.fixture
63+
def unique_project_name(tmp_path):
64+
"""Generate unique project name using tmp_path's unique directory."""
65+
# tmp_path.name gives us something like 'test_upload0'
66+
return f"default_{tmp_path.name}"
67+
68+
6269
@pytest.fixture
6370
def set_api_key(monkeypatch):
6471
"""Set the api key."""
@@ -69,13 +76,13 @@ def set_api_key(monkeypatch):
6976

7077

7178
@pytest.fixture
72-
def mock_upload(monkeypatch, set_api_key):
79+
def mock_upload(monkeypatch, set_api_key, unique_project_name):
7380
"""Mocks webapi.upload."""
7481
responses.add(
7582
responses.GET,
7683
f"{Env.current.web_api_endpoint}/tidy3d/project",
77-
match=[matchers.query_param_matcher({"projectName": PROJECT_NAME})],
78-
json={"data": {"projectId": FOLDER_ID, "projectName": PROJECT_NAME}},
84+
match=[matchers.query_param_matcher({"projectName": unique_project_name})],
85+
json={"data": {"projectId": FOLDER_ID, "projectName": unique_project_name}},
7986
status=200,
8087
)
8188

@@ -270,21 +277,21 @@ def mock_webapi(
270277

271278

272279
@responses.activate
273-
def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata):
280+
def test_upload(monkeypatch, mock_upload, mock_get_info, mock_metadata, unique_project_name):
274281
sim = make_mode_sim()
275282
assert sim != get_reduced_simulation(sim, reduce_simulation=True)
276-
assert upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=True)
283+
assert upload(sim, TASK_NAME, unique_project_name, reduce_simulation=True)
277284

278285

279286
@pytest.mark.parametrize("reduce_simulation", [True, False])
280287
@responses.activate
281288
def test_upload_with_reduction_parameter(
282-
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation
289+
monkeypatch, mock_upload, mock_get_info, mock_metadata, reduce_simulation, unique_project_name
283290
):
284291
"""Test that simulation reduction is properly applied before upload based on reduce_simulation parameter."""
285292
sim = make_mode_sim()
286293

287-
upload(sim, TASK_NAME, PROJECT_NAME, reduce_simulation=reduce_simulation)
294+
upload(sim, TASK_NAME, unique_project_name, reduce_simulation=reduce_simulation)
288295

289296
if reduce_simulation:
290297
expected_sim = get_reduced_simulation(sim, reduce_simulation=True)
@@ -341,13 +348,13 @@ def mock_download(*args, **kwargs):
341348

342349

343350
@responses.activate
344-
def test_run(mock_webapi, monkeypatch, tmp_path):
351+
def test_run(mock_webapi, monkeypatch, tmp_path, unique_project_name):
345352
sim = make_mode_sim()
346353
monkeypatch.setattr(f"{api_path}.load", lambda *args, **kwargs: True)
347354
assert run(
348355
sim,
349356
task_name=TASK_NAME,
350-
folder_name=PROJECT_NAME,
357+
folder_name=unique_project_name,
351358
path=str(tmp_path / "web_test_tmp.json"),
352359
)
353360

@@ -375,10 +382,10 @@ def test_abort_task(set_api_key, mock_get_info):
375382

376383

377384
@responses.activate
378-
def test_job(mock_webapi, monkeypatch, tmp_path):
385+
def test_job(mock_webapi, monkeypatch, tmp_path, unique_project_name):
379386
monkeypatch.setattr("tidy3d.web.api.container.Job.load", lambda *args, **kwargs: True)
380387
sim = make_mode_sim()
381-
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=PROJECT_NAME)
388+
j = Job(simulation=sim, task_name=TASK_NAME, folder_name=unique_project_name)
382389

383390
_ = j.run(path=str(tmp_path / "web_test_tmp.json"))
384391
_ = j.status
@@ -395,12 +402,12 @@ def mock_job_status(monkeypatch):
395402

396403

397404
@responses.activate
398-
def test_batch(mock_webapi, mock_job_status, tmp_path):
405+
def test_batch(mock_webapi, mock_job_status, tmp_path, unique_project_name):
399406
# monkeypatch.setattr("tidy3d.web.api.container.Batch.monitor", lambda self: time.sleep(0.1))
400407
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
401408

402409
sims = {TASK_NAME: make_mode_sim()}
403-
b = Batch(simulations=sims, folder_name=PROJECT_NAME)
410+
b = Batch(simulations=sims, folder_name=unique_project_name)
404411
b.estimate_cost()
405412
_ = b.run(path_dir=str(tmp_path))
406413
assert b.real_cost() == FLEX_UNIT * len(sims)
@@ -410,7 +417,7 @@ def test_batch(mock_webapi, mock_job_status, tmp_path):
410417

411418

412419
@responses.activate
413-
def test_async(mock_webapi, mock_job_status):
420+
def test_async(mock_webapi, mock_job_status, unique_project_name):
414421
# monkeypatch.setattr("tidy3d.web.api.container.Job.status", property(lambda self: "success"))
415422
sims = {TASK_NAME: make_mode_sim()}
416-
_ = run_async(sims, folder_name=PROJECT_NAME)
423+
_ = run_async(sims, folder_name=unique_project_name)

0 commit comments

Comments
 (0)