Skip to content

Commit

Permalink
Add multistep and multidev tests (#66)
Browse files Browse the repository at this point in the history
* Fix readme

* Make sure multiple steps on one device get concatenated properly.

* Add multidev tests.

* Debug prints change.

* More verbose print

* Wait for "r" then for "c".

* Try fix multi pipeline tests
  • Loading branch information
PeterKraus authored Mar 6, 2024
1 parent 65c1374 commit 637b302
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 13 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
![Github link](https://badgen.net/github/tag/dgbowl/tomato/?icon=github)
![Github status](https://badgen.net/github/checks/dgbowl/tomato/?icon=github)

`tomato` is the instrument automation package developed at Empa. Currently supported
hardware is:
`tomato` is the instrument automation package developed at the [ConCat lab](https://tu.berlin/en/concat). Currently supported hardware is:

- `example_counter`: an example device for testing purposes.

Partially supported hardware includes:

- `dummy`: a Dummy device for testing purposes
- `biologic`: device for BioLogic potentiostats via the EC-Lib library, Windows only.

See the [Documentation](https://dgbowl.github.io/tomato) for more detailed info.
Expand Down
4 changes: 3 additions & 1 deletion src/tomato/drivers/example_counter/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run_counter(self, conn):
self.started_at = None

cmd = None
if conn.poll(1e-6):
if conn.poll(1e-3):
cmd, attr, val = conn.recv()

if cmd == "set":
Expand All @@ -58,3 +58,5 @@ def run_counter(self, conn):
elif cmd == "data":
conn.send(data)
data = []
else:
time.sleep(1e-3)
20 changes: 12 additions & 8 deletions src/tomato/tomato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,15 @@ def reload(
ret = _updater(
context, port, "driver", dict(name=drv.name, settings=drv.settings)
)
logger.critical(f"{ret=}")
if ret.success is False:
return ret

# check changes in devices
for dev in devs.values():
if (
dev.name not in daemon.devs
or dev.channels != daemon.devs[dev.name].channels
):
logger.error(f"Here {dev.channels=} {dev.name=}")
for channel in dev.channels:
params = dict(
address=dev.address,
Expand All @@ -390,9 +390,10 @@ def reload(
)
if ret.success is False:
return ret
params = dict(name=dev.name, channels=dev.channels)
params = dev.dict()
ret = _updater(context, port, "device", params)
logger.critical(f"{ret=}")
if ret.success is False:
return ret
elif dev != daemon.devs[dev.name]:
logger.error("updating devices not yet implemented")
for devname in daemon.devs:
Expand All @@ -402,16 +403,19 @@ def reload(
for pip in pips.values():
if pip.name not in daemon.pips:
ret = _updater(context, port, "pipeline", pip.dict())
logger.critical(f"{ret=}")
if ret.success is False:
return ret
else:
logger.error("updating pipelines not yet implemented")
for pip in daemon.pips.values():
if pip.name not in pips:
params = dict(name=pip.name, delete=True)
ret = _updater(context, port, "pipeline", params)
logger.critical(f"{ret=}")

req.send_pyobj(dict(cmd="status", with_data=True, sender=f"{__name__}.reload"))
if ret.success is False:
return ret
req.send_pyobj(
dict(cmd="setup", settings=settings, sender=f"{__name__}.reload")
)
rep = req.recv_pyobj()

if rep.msg == "running":
Expand Down
14 changes: 14 additions & 0 deletions tests/common/counter_multidev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "0.2"
sample:
name: counter_multidev
method:
- device: "counter-1"
technique: "count"
time: 1.0
delay: 0.1
- device: "counter-2"
technique: "count"
time: 1.0
delay: 0.2
tomato:
verbosity: "DEBUG"
14 changes: 14 additions & 0 deletions tests/common/counter_multistep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "0.2"
sample:
name: counter_multistep
method:
- device: "counter"
technique: "count"
time: 1.0
delay: 0.1
- device: "counter"
technique: "count"
time: 1.0
delay: 0.2
tomato:
verbosity: "DEBUG"
18 changes: 18 additions & 0 deletions tests/common/counter_multistep_multidev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "0.2"
sample:
name: counter_multistep_multidev
method:
- device: "counter-1"
technique: "count"
time: 1.0
delay: 0.2
- device: "counter-1"
technique: "count"
time: 1.0
delay: 0.2
- device: "counter-2"
technique: "count"
time: 2.0
delay: 0.1
tomato:
verbosity: "DEBUG"
35 changes: 35 additions & 0 deletions tests/common/devices_multidev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"devices": [
{
"name": "dev-counter-1",
"driver": "example_counter",
"address": "example-addr-1",
"channels": [1, 2, 3, 4],
"capabilities": ["count"],
"pollrate": 2
},
{
"name": "dev-counter-2",
"driver": "example_counter",
"address": "example-addr-2",
"channels": [5, 6, 7, 8],
"capabilities": ["count"],
"pollrate": 1
}
],
"pipelines": [
{
"name": "pip-counter",
"devices": [
{"tag": "counter", "name": "dev-counter-1", "channel": 1}
]
},
{
"name": "pip-multidev",
"devices": [
{"tag": "counter-1", "name": "dev-counter-1", "channel": 4},
{"tag": "counter-2", "name": "dev-counter-2", "channel": 5}
]
}
]
}
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def start_tomato_daemon(tmpdir: str, port: int = 12345):
# setup_stuff
os.chdir(tmpdir)
subprocess.run(["tomato", "init", "-p", f"{port}", "-A", ".", "-D", "."])
subprocess.run(["tomato", "start", "-p", f"{port}", "-A", ".", "-L", "."])
subprocess.run(["tomato", "start", "-p", f"{port}", "-A", ".", "-L", ".", "-vv"])
yield
# teardown_stuff

Expand Down
40 changes: 40 additions & 0 deletions tests/test_99_example_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import os
import subprocess
import time
import json
import yaml
import xarray as xr

from . import utils

PORT = 12345


@pytest.mark.parametrize(
"casename, npoints, prefix",
Expand All @@ -14,6 +18,7 @@
("counter_5_0.2", 25, "results.1"),
("counter_output_prefix", 20, "data"),
("counter_output_path", 20, os.path.join("newfolder", "results.1")),
("counter_multistep", 15, "results.1"),
],
)
def test_counter_npoints(
Expand Down Expand Up @@ -81,3 +86,38 @@ def test_counter_snapshot(
status = utils.job_status(1)["data"][1]["status"]
assert status == "c"
assert os.path.exists("snapshot.1.nc")


@pytest.mark.parametrize(
"casename, npoints",
[
("counter_multidev", 15),
("counter_multistep_multidev", 30),
],
)
def test_counter_multidev(
casename, npoints, datadir, start_tomato_daemon, stop_tomato_daemon
):
os.chdir(datadir)
with open("devices_multidev.json", "r") as inf:
jsdata = json.load(inf)
with open("devices.yml", "w") as ouf:
yaml.dump(jsdata, ouf)
utils.wait_until_tomato_running(port=PORT, timeout=3000)
subprocess.run(["tomato", "reload", "-p", f"{PORT}", "-A", "."])

utils.run_casenames([casename], [None], ["pip-multidev"])
utils.wait_until_ketchup_status(jobid=1, status="r", port=PORT, timeout=10000)
utils.wait_until_ketchup_status(jobid=1, status="c", port=PORT, timeout=10000)

ret = utils.job_status(1)
print(f"{ret=}")
status = utils.job_status(1)["data"][1]["status"]
assert status == "c"
files = os.listdir(os.path.join(".", "Jobs", "1"))
assert "jobdata.json" in files
assert "job-1.log" in files
assert os.path.exists("results.1.nc")
ds = xr.load_dataset("results.1.nc")
print(f"{ds=}")
assert ds["uts"].size == npoints

0 comments on commit 637b302

Please sign in to comment.