Skip to content

Commit

Permalink
tests: add query/queryable attachment test
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 14, 2024
1 parent e71a74c commit a488558
Showing 1 changed file with 111 additions and 29 deletions.
140 changes: 111 additions & 29 deletions tests/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ def pub_and_sub():
Putting Data ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!')...
Putting Data ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')...'''
Putting Data ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')...'''

# Expected z_sub output & status
z_sub_expected_status = 0
Expand All @@ -53,31 +48,11 @@ def pub_and_sub():
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')
with attachment:
0: source, C
1: index, 4
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!')
with attachment:
0: source, C
1: index, 5
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!')
with attachment:
0: source, C
1: index, 6
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!')
with attachment:
0: source, C
1: index, 7
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!')
with attachment:
0: source, C
1: index, 8
>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')
with attachment:
0: source, C
1: index, 9'''
1: index, 4'''

print("Start subscriber")
# Start z_sub in the background
z_sub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_sub_attachment -n 10"
z_sub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_sub_attachment -n 5"

z_sub_process = subprocess.Popen(
z_sub_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
Expand All @@ -88,7 +63,7 @@ def pub_and_sub():

print("Start publisher")
# Start z_pub
z_pub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_pub_attachment -n 10"
z_pub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_pub_attachment -n 5"
z_pub_process = subprocess.Popen(
z_pub_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
Expand Down Expand Up @@ -147,11 +122,118 @@ def pub_and_sub():
return test_status


def query_and_queryable():
print("*** Query & queryable test ***")
test_status = 0

# Expected z_query output & status

z_query_expected_status = 0
z_query_expected_output = """Opening session...
Sending Query 'demo/example/**'...
>> Received ('demo/example/**': 'Queryable from Pico!')
with attachment:
0: reply_key, reply_value
>> Received query final notification"""

# Expected z_queryable output & status
z_queryable_expected_status = 0
z_queryable_expected_output = """Opening session...
Creating Queryable on 'demo/example/zenoh-pico-queryable'...
Press CTRL-C to quit...
>> [Queryable handler] Received Query 'demo/example/**'
with attachment:
0: test_key, test_value"""

print("Start queryable")
# Start z_queryable in the background
z_queryable_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_queryable_attachment -n 1"
z_queryable_process = subprocess.Popen(
z_queryable_command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

# Introduce a delay to ensure z_queryable starts
time.sleep(2)

print("Start query")
# Start z_query
z_query_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_get_attachment"
z_query_process = subprocess.Popen(
z_query_command,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

# Wait for z_query to finish
z_query_process.wait()

print("Stop queryable")
time.sleep(2)
if z_queryable_process.poll() is None:
# send SIGINT to group
z_quaryable_process_gid = os.getpgid(z_queryable_process.pid)
os.killpg(z_quaryable_process_gid, SIGINT)

# Wait for z_queryable to finish
z_queryable_process.wait()

print("Check query status & output")
# Check the exit status of z_query
z_query_status = z_query_process.returncode
if z_query_status == z_query_expected_status:
print("z_query status valid")
else:
print(f"z_query status invalid, expected: {z_query_expected_status}," f" received: {z_query_status}")
test_status = 1

# Check output of z_query
z_query_output = z_query_process.stdout.read()
if z_query_expected_output in z_query_output:
print("z_query output valid")
else:
print("z_query output invalid:")
print(f'Expected: "{z_query_expected_output}"')
print(f'Received: "{z_query_output}"')
test_status = 1

print("Check queryable status & output")
# Check the exit status of z_queryable
z_queryable_status = z_queryable_process.returncode
if z_queryable_status == z_queryable_expected_status:
print("z_queryable status valid")
else:
print(f"z_queryable status invalid, expected: {z_queryable_expected_status}," f" received: {z_queryable_status}")
test_status = 1

# Check output of z_queryable
z_queryable_output = z_queryable_process.stdout.read()
if z_queryable_expected_output in z_queryable_output:
print("z_queryable output valid")
else:
print("z_queryable output invalid:")
print(f'Expected: "{z_queryable_expected_output}"')
print(f'Received: "{z_queryable_output}"')
test_status = 1
# Return status
return test_status


if __name__ == "__main__":
EXIT_STATUS = 0

# Test pub and sub examples
if pub_and_sub() == 1:
EXIT_STATUS = 1
# Test query and queryable examples
if query_and_queryable() == 1:
EXIT_STATUS = 1
# Exit
sys.exit(EXIT_STATUS)

0 comments on commit a488558

Please sign in to comment.