Skip to content

Commit e566592

Browse files
Measuring time
1 parent 7674e43 commit e566592

File tree

1 file changed

+77
-27
lines changed

1 file changed

+77
-27
lines changed

src/python_testing/TC_IDM_4_3.py

+77-27
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@
4545

4646
class TC_IDM_4_3(MatterBaseTest):
4747

48+
# ANSI escape codes for background colors
49+
BACKGROUND_COLORS = {
50+
'black': '\033[40m',
51+
'red': '\033[41m',
52+
'green': '\033[42m',
53+
'yellow': '\033[43m',
54+
'blue': '\033[44m',
55+
'magenta': '\033[45m',
56+
'cyan': '\033[46m',
57+
'white': '\033[47m',
58+
'reset': '\033[0m'
59+
}
60+
61+
# Function to print text with a specific background color
62+
def fprint(self, text: str, background_color: str):
63+
print(f"{self.BACKGROUND_COLORS.get(background_color, self.BACKGROUND_COLORS['reset'])}{text}{self.BACKGROUND_COLORS['reset']}")
64+
4865
def steps_TC_IDM_4_3(self):
4966
return [TestStep("1a", "DUT and TH activate the subscription.",
5067
"Verify on the TH, a report data message is received. Verify on the TH the Subscribe Response has the following fields: SubscriptionId and MaxInterval In the following Steps 2, 3, 5-10, 13, and 15, the MaxInterval time reference in each step is the MaxInterval presented in the Subscribe Response of the subscription."),
@@ -93,11 +110,45 @@ def steps_TC_IDM_4_3(self):
93110
]
94111

95112
def on_notify_subscription_still_active(self):
96-
print("NotifyLogic")
113+
114+
self.previous_report_data_time = self.current_report_data_time
115+
self.current_report_data_time = time.time()
116+
117+
if self.previous_report_data_time > 0:
118+
diff = self.current_report_data_time - self.previous_report_data_time
119+
else:
120+
diff = 0
121+
122+
self.fprint(f"NotifyLogic {time.time()} >>> t: {diff}", "red")
123+
124+
def wait_for_attribute_update_report(self, expected_attribute, output):
125+
try:
126+
path, transaction = output.get(block=True, timeout=10)
127+
except queue.Empty:
128+
asserts.fail(
129+
f"[AttributeChangeCallback | Local] Failed to receive a report for the {expected_attribute} attribute change")
130+
131+
asserts.assert_equal(path.AttributeType, expected_attribute,
132+
f"[AttributeChangeCallback | Local] Received incorrect report. Expected: {expected_attribute}, received: {path.AttributeType}")
133+
try:
134+
attribute_value = transaction.GetAttribute(path)
135+
136+
self.attr_update_report_data_time = time.time()
137+
138+
logging.info(
139+
f"[AttributeChangeCallback | Local] Got attribute subscription report. Attribute {path.AttributeType}. Updated value: {attribute_value}. SubscriptionId: {transaction.subscriptionId}")
140+
except KeyError:
141+
asserts.fail("[AttributeChangeCallback | Local] Attribute {expected_attribute} not found in returned report")
142+
143+
current_report_data_time: time = 0
144+
previous_report_data_time: time = 0
145+
attr_update_report_data_time: time = 0
146+
147+
min_interval_floor_sec: int = 2
148+
max_interval_ceiling_sec: int = 3
97149

98150
@async_test_body
99151
async def test_TC_IDM_4_3(self):
100-
print("Hey there")
101152

102153
# Test setup
103154
node_label_attr = Clusters.BasicInformation.Attributes.NodeLabel
@@ -109,44 +160,30 @@ async def test_TC_IDM_4_3(self):
109160
self.step("1a")
110161

111162
# Subscribe to attribute
112-
sub_th_step1a = await TH.ReadAttribute(
163+
sub_th_step1ab = await TH.ReadAttribute(
113164
nodeid=self.dut_node_id,
114165
attributes=node_label_attr_path,
115-
reportInterval=(3, 5),
166+
reportInterval=(self.min_interval_floor_sec, self.max_interval_ceiling_sec),
116167
keepSubscriptions=False
117168
)
118169

170+
sub_th_step1ab.SetNotifySubscriptionStillActiveCallback(self.on_notify_subscription_still_active)
119171

120-
121-
122-
123-
124-
125-
sub_th_step1a.SetNotifySubscriptionStillActiveCallback(self.on_notify_subscription_still_active)
126-
127-
secs = 60
172+
secs = 45
128173
print(f"\n\n\n\n\nTime to sleep {secs} second(s)")
129174
time.sleep(secs)
130175
print(f"Rise and shine after {secs} second(s)\n\n\n\n\n")
131176

132-
133-
134-
135-
136-
137-
138177
# Verify that the subscription is activated between TH and DUT
139178
# Verify on the TH, a report data message is received.
140-
asserts.assert_true(sub_th_step1a.subscriptionId, "Subscription not activated")
179+
asserts.assert_true(sub_th_step1ab.subscriptionId, "Subscription not activated")
141180

142181
# Verify subscriptionId field is present
143-
asserts.assert_is_not_none(sub_th_step1a.subscriptionId, "SubscriptionId field not present")
182+
asserts.assert_is_not_none(sub_th_step1ab.subscriptionId, "SubscriptionId field not present")
144183

145184
# Verify MaxInterval field is present
146-
sub_th_step1a_min_interval_sec, sub_th_step1a_max_interval_sec = sub_th_step1a.GetReportingIntervalsSeconds()
147-
asserts.assert_is_not_none(sub_th_step1a_max_interval_sec, "MaxInterval field not present")
148-
149-
# sub_th_step1a.Shutdown()
185+
sub_th_step1ab_min_interval_sec, sub_th_step1ab_max_interval_sec = sub_th_step1ab.GetReportingIntervalsSeconds()
186+
asserts.assert_is_not_none(sub_th_step1ab_max_interval_sec, "MaxInterval field not present")
150187

151188
# *** Step 1b ***
152189
# Change the value of the attribute which has been subscribed on the DUT by manually changing some
@@ -156,16 +193,29 @@ async def test_TC_IDM_4_3(self):
156193

157194
# Set Attribute Update Callback
158195
node_label_update_cb = AttributeChangeCallback(node_label_attr)
159-
sub_th_step1a.SetAttributeUpdateCallback(node_label_update_cb)
196+
sub_th_step1ab.SetAttributeUpdateCallback(node_label_update_cb)
160197

161-
# Modify attribute value
198+
# Update attribute value
162199
new_node_label_write = "NewNodeLabel_11001100"
163200
await TH.WriteAttribute(
164201
self.dut_node_id,
165202
[(0, node_label_attr(value=new_node_label_write))]
166203
)
167204

168-
node_label_update_cb.wait_for_report()
205+
self.wait_for_attribute_update_report(node_label_attr, node_label_update_cb._output)
206+
207+
# Number of seconds elapsed between the last report data event
208+
# and the arrival of the attribute update report data
209+
elapsed_time_since_report = self.attr_update_report_data_time - self.previous_report_data_time
210+
211+
# Verify that the attribute update report data is sent
212+
# after MinInterval time and before MaxInterval time
213+
asserts.assert_greater(elapsed_time_since_report, self.min_interval_floor_sec,
214+
f"Attribute update report data must be sent after the MinInterval")
215+
asserts.assert_less(elapsed_time_since_report, self.max_interval_ceiling_sec,
216+
f"Attribute update report data must be sent before the MaxInterval")
217+
218+
sub_th_step1ab.Shutdown()
169219

170220

171221
if __name__ == "__main__":

0 commit comments

Comments
 (0)