Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align examples and remove reading from stdin #150

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/z_pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def main():
print(f"Declaring Publisher on '{key}'...")
pub = session.declare_publisher(key)

print("Press CTRL-C to quit...")
for idx in itertools.count() if args.iter is None else range(args.iter):
time.sleep(1)
buf = f"[{idx:4d}] {value}"
Expand Down
1 change: 1 addition & 0 deletions examples/z_pub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def main():
session = zenoh.open(conf)
pub = session.declare_publisher('test/thr', congestion_control=congestion_control)

print("Press CTRL-C to quit...")
while True:
pub.put(data)

Expand Down
15 changes: 6 additions & 9 deletions examples/z_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# ZettaScale Zenoh Team, <[email protected]>
#

import itertools
import sys
import time
from datetime import datetime
Expand Down Expand Up @@ -73,17 +74,13 @@ def main():
session = zenoh.open(conf)

print("Declaring Subscriber on '{}'...".format(key))

sub = session.declare_pull_subscriber(key, listen, reliability=Reliability.RELIABLE())

print("Press <enter> to pull data...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
else:
sub.pull()
print("Press CTRL-C to quit...")
for idx in itertools.count():
time.sleep(1)
print(f"[{idx:4d}] Pulling...")
sub.pull()

sub.undeclare()
session.close()
Expand Down
11 changes: 3 additions & 8 deletions examples/z_queryable.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ def main():
print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, queryable_callback, complete)

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c != 'q':
print("getting")
session.get(key, print, consolidation=zenoh.QueryConsolidation.NONE())
time.sleep(1)
print("Press CTRL-C to quit...")
while True:
time.sleep(1)

queryable.undeclare()
session.close()
Expand Down
9 changes: 3 additions & 6 deletions examples/z_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ def main():
print("Declaring Queryable on '{}'...".format(key))
queryable = session.declare_queryable(key, query_handler, complete)

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
print("Press CTRL-C to quit...")
while True:
time.sleep(1)

sub.undeclare()
queryable.undeclare()
Expand Down
9 changes: 3 additions & 6 deletions examples/z_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,9 @@ def listener(sample: Sample):
# will be immediately undeclared.
sub = session.declare_subscriber(key, listener, reliability=Reliability.RELIABLE())

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
print("Press CTRL-C to quit...")
while True:
time.sleep(1)

# Cleanup: note that even if you forget it, cleanup will happen automatically when
# the reference counter reaches 0
Expand Down
9 changes: 3 additions & 6 deletions examples/z_sub_queued.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ def consumer():

t = Thread(target=consumer)
t.start()
print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
print("Press CTRL-C to quit...")
while True:
time.sleep(1)

# Cleanup: note that even if you forget it, cleanup will happen automatically when
# the reference counter reaches 0
Expand Down
9 changes: 3 additions & 6 deletions examples/z_sub_thr.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,9 @@ def main():
# Only do this if your callback runs faster than the minimum expected delay between two samples.
sub = session.declare_subscriber("test/thr", zenoh.Closure((listener, report)), reliability=Reliability.RELIABLE())

print("Enter 'q' to quit...")
c = '\0'
while c != 'q':
c = sys.stdin.read(1)
if c == '':
time.sleep(1)
print("Press CTRL-C to quit...")
while True:
time.sleep(1)

sub.undeclare()
session.close()
Expand Down
64 changes: 23 additions & 41 deletions tests/examples_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
# Contributors:
# ZettaScale Zenoh team, <[email protected]>
#
from os import path
from os import path, getpgid, killpg
from signal import SIGINT
from subprocess import Popen, PIPE
import time

examples = path.dirname(path.realpath(__file__)).replace('tests', 'examples')
examples = path.realpath(__file__).split("/tests")[0]+"/examples/"
tab = "\t"
ret = "\r\n"
KILL = -9
Expand All @@ -25,7 +26,7 @@ def __init__(self, p, args=None) -> None:
args = []
self.name = p
print(f"starting {self.name}")
self.process: Popen = Popen(["python3", path.join(examples, p), *args], stdin=PIPE, stdout=PIPE, stderr=PIPE)
self.process: Popen = Popen(["python3", path.join(examples, p), *args], stdin=PIPE, stdout=PIPE, stderr=PIPE, start_new_session=True)
self.start = time.time()
self.end = None
self._stdouts = []
Expand All @@ -51,6 +52,10 @@ def wait(self):
if self.end is None:
self.end = time.time()
return code
def interrupt(self):
# send SIGINT to process group
pgid = getpgid(self.process.pid)
killpg(pgid, SIGINT)
@property
def stdout(self):
self._stdouts.extend(line.decode('utf8') for line in self.process.stdout.readlines())
Expand Down Expand Up @@ -85,22 +90,14 @@ def time(self):
time.sleep(1)
pub = Pyrun("z_pub.py", ["--iter=2"])
time.sleep(4)
try:
pull.process.stdin.write(b"\n")
pull.process.stdin.flush()
time.sleep(1)
pull.process.stdin.write(b"q\n")
pull.process.stdin.flush()
pull.process.stdin.close()
except Exception as e:
pull.dbg()
errors.append(f"pull stdin sequence failed: {e}")

pull.interrupt()
if pub.status():
pub.dbg()
errors.append(pub.status())
if pull.status():
if pull.status(KILL):
pull.dbg()
errors.append(pull.status())
errors.append(pull.status(KILL))
subout = "".join(pull.stdout)
if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in subout):
errors.append("z_pull didn't catch put")
Expand All @@ -120,15 +117,10 @@ def time(self):
queryable.dbg()
errors.append("z_get didn't get a response from z_queryable")

try:
queryable.process.stdin.write(b"q\n")
queryable.process.stdin.flush()
queryable.process.stdin.close()
except Exception as e:
errors.append(f"queryable stdin sequence failed: {e}")
if queryable.status():
queryable.interrupt()
if queryable.status(KILL):
queryable.dbg()
errors.append(queryable.status())
errors.append(queryable.status(KILL))
queryableout = "".join(queryable.stdout)
if not ("Received Query 'demo/example/zenoh-python-queryable'" in queryableout):
errors.append("z_queryable didn't catch query")
Expand Down Expand Up @@ -163,15 +155,10 @@ def time(self):
if any(("z_get" in error) for error in errors):
get.dbg()

try:
sub.process.stdin.write(b"q\n")
sub.process.stdin.flush()
sub.process.stdin.close()
except Exception as e:
errors.append(f"pub stdin sequence failed: {e}")
if sub.status():
sub.interrupt()
if sub.status(KILL):
sub.dbg()
errors.append(sub.status())
errors.append(sub.status(KILL))
subout = "".join(sub.stdout)
if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in subout):
errors.append("z_sub didn't catch put")
Expand All @@ -182,15 +169,10 @@ def time(self):
if any(("z_sub" in error) for error in errors):
sub.dbg()

try:
storage.process.stdin.write(b"q\n")
storage.process.stdin.flush()
storage.process.stdin.close()
except Exception as e:
errors.append(f"storage stdin sequence failed: {e}")
if storage.status():
storage.interrupt()
if storage.status(KILL):
storage.dbg()
errors.append(storage.status())
errors.append(storage.status(KILL))
storageout = "".join(storage.stdout)
if not ("Received PUT ('demo/example/zenoh-python-put': 'Put from Python!')" in storageout):
errors.append("z_storage didn't catch put")
Expand All @@ -210,10 +192,10 @@ def time(self):
pub_thr.process.kill()
if sub_thr.status(KILL):
sub_thr.dbg()
errors.append(sub_thr.status())
errors.append(sub_thr.status(KILL))
if pub_thr.status(KILL):
pub_thr.dbg()
errors.append(pub_thr.status())
errors.append(pub_thr.status(KILL))


if len(errors):
Expand Down