Skip to content

Commit

Permalink
python/iot3: add simple tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yann E. MORIN <yann.morin@orange.com>
  • Loading branch information
ymorin-orange committed Aug 6, 2024
1 parent 606a009 commit bcf8e1d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python/iot3/tests/test-iot3-core
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3

import iot3.core
import time

def recv(data, topic, payload):
print(f"{topic[:16]}: {payload[:16]}")

iot3.core.start(
config=iot3.core.sample_config,
message_callback=recv,
)

iot3.core.subscribe("#")

iot3.core.publish("foo/bar/dropped", "dropped")
time.sleep(1)

iot3.core.publish("foo/bar/passed", "passed")
time.sleep(1)

iot3.core.stop()
35 changes: 35 additions & 0 deletions python/iot3/tests/test-iot3-core-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

import iot3.core.mqtt
import iot3.core.otel
import time

def recv(data, topic, payload):
print(f"{topic[:16]}: {payload[:16]}")

o = iot3.core.otel.Otel(
service_name="test-service",
endpoint="http://localhost:4318",
batch_period=1,
)
o.start()

c = iot3.core.mqtt.MqttClient(
client_id="test",
host="127.0.0.1",
port=1883,
span_ctxmgr_cb=o.span,
msg_cb=recv,
)
c.start()

c.subscribe(topics=["#"])

c.publish(topic='foo/bar/dropped', payload="dropped")
time.sleep(1)

c.publish(topic='foo/bar/passed', payload="passed")
time.sleep(1)

c.stop()
o.stop()
23 changes: 23 additions & 0 deletions python/iot3/tests/test-iot3-core-mqtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

import iot3.core.mqtt
import time

def recv(data, topic, payload):
print(f"{topic[:16]}: {payload[:16]}")

c = iot3.core.mqtt.MqttClient(
client_id="test",
host="127.0.0.1",
port=1883,
msg_cb=recv,
)
c.start()

c.subscribe(topics=["#"])
c.wait_for_ready()
c.publish(topic='foo/bar', payload="pouet")

time.sleep(1)

c.stop()
38 changes: 38 additions & 0 deletions python/iot3/tests/test-iot3-core-otel
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

import iot3.core.otel
import os
import sys
import time


o = iot3.core.otel.Otel(
service_name="test-service",
endpoint="http://localhost:4318",
batch_period=15,
max_backlog=15,
)
o.start()

with o.span(name="Root span") as s0:
time.sleep(0.1)
with o.span(name="Explicit child span level 1", parent_span=s0) as s1:
time.sleep(0.1)
with o.span(name="Automatic child span level 2") as s2:
time.sleep(0.1)
with o.span(name="Explicit child span level 2 missed", parent_span=s0) as s3:
time.sleep(0.1)
with o.span(name="Automatic child span level 1") as s4:
time.sleep(0.1)
with o.span(name="Automatic child span level 1 - failed") as s5:
s5.set_status(
status_code=iot3.core.otel.SpanStatus.ERROR,
status_message="Some failure"
)
time.sleep(0.1)
time.sleep(0.1)

with o.span(name="Root span with link", span_links=[s0]) as s10:
time.sleep(0.1)

o.stop()

0 comments on commit bcf8e1d

Please sign in to comment.