-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_launcher.py
126 lines (98 loc) · 3.51 KB
/
test_launcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pytest
import multiprocessing as mp
from pathlib import Path
import json
import copy
from blendtorch import btt
BLENDDIR = Path(__file__).parent / "blender"
LAUNCH_ARGS = dict(
scene="",
script=str(BLENDDIR / "launcher.blend.py"),
num_instances=2,
named_sockets=["DATA", "GYM"],
background=True,
instance_args=[["--x", "3"], ["--x", "4"]],
seed=10,
)
def _validate_result(items):
assert len(items) == 2
first, second = 0, 1
if items[0]["btid"] == 1:
first, second = second, first
assert items[first]["btargs"]["btid"] == 0
assert items[second]["btargs"]["btid"] == 1
assert items[first]["btargs"]["btseed"] == 10
assert items[second]["btargs"]["btseed"] == 11
assert items[first]["btargs"]["btsockets"]["DATA"].startswith("tcp://")
assert items[first]["btargs"]["btsockets"]["GYM"].startswith("tcp://")
assert items[second]["btargs"]["btsockets"]["DATA"].startswith("tcp://")
assert items[second]["btargs"]["btsockets"]["GYM"].startswith("tcp://")
assert items[first]["remainder"] == ["--x", "3"]
assert items[second]["remainder"] == ["--x", "4"]
#@pytest.mark.background
def test_launcher():
with btt.BlenderLauncher(**LAUNCH_ARGS) as bl:
addr = bl.launch_info.addresses["DATA"]
ds = btt.RemoteIterableDataset(addr, max_items=2)
items = [item for item in ds]
_validate_result(items)
def _launch(q, tmp_path):
with btt.BlenderLauncher(**LAUNCH_ARGS) as bl:
path = Path(tmp_path / "addresses.json")
btt.LaunchInfo.save_json(path, bl.launch_info)
q.put(path)
bl.wait()
#@pytest.mark.background
def test_launcher_connected_remote(tmp_path):
# Simulates BlenderLauncher called from a separate process and
# shows how one can connect to already launched instances through
# serialization of addresses.
q = mp.Queue()
p = mp.Process(target=_launch, args=(q, tmp_path))
p.start()
path = q.get()
launch_info = btt.LaunchInfo.load_json(path)
ds = btt.RemoteIterableDataset(launch_info.addresses["DATA"], max_items=2)
items = [item for item in ds]
_validate_result(items)
p.join()
def _launch_app(tmp_path, args):
from blendtorch.btt.apps import launch
with open(tmp_path / "launchargs.json", "w") as fp:
fp.write(json.dumps(args, indent=4))
launch.main(
[
"--out-launch-info",
str(tmp_path / "launchinfo.json"),
str(tmp_path / "launchargs.json"),
]
)
#@pytest.mark.background
def test_launcher_app(tmp_path):
p = mp.Process(target=_launch_app, args=(tmp_path, LAUNCH_ARGS))
p.start()
import time
path = tmp_path / "launchinfo.json"
while not Path.exists(path):
time.sleep(1)
launch_info = btt.LaunchInfo.load_json(path)
ds = btt.RemoteIterableDataset(launch_info.addresses["DATA"], max_items=2)
items = [item for item in ds]
_validate_result(items)
p.join()
def test_launcher_app_primaryip(tmp_path):
# Same with primary ip resolver
args = copy.deepcopy(LAUNCH_ARGS)
args["bind_addr"] = "primaryip"
p = mp.Process(target=_launch_app, args=(tmp_path, args))
p.start()
import time
path = tmp_path / "launchinfo.json"
while not Path.exists(path):
time.sleep(1)
launch_info = btt.LaunchInfo.load_json(path)
print(launch_info.addresses)
ds = btt.RemoteIterableDataset(launch_info.addresses["DATA"], max_items=2)
items = [item for item in ds]
_validate_result(items)
p.join()