Skip to content

Commit 885b976

Browse files
authored
Fix shadow tests (#543)
1 parent 9223c8a commit 885b976

File tree

2 files changed

+28
-59
lines changed

2 files changed

+28
-59
lines changed

servicetests/test_cases/test_shadow_update.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,33 @@ def main():
8080
if test_result == 0:
8181
print("Verifying that shadow was updated")
8282
shadow_value = None
83-
try:
84-
if shadow_name:
85-
thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name, shadowName=shadow_name)
86-
else:
87-
thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name)
88-
89-
payload = thing_shadow['payload'].read()
90-
data = json.loads(payload)
91-
shadow_value = data.get('state', {}).get('reported', {}).get(shadow_property, None)
92-
if shadow_value != shadow_desired_value:
93-
print(f"ERROR: Could not verify thing shadow: {shadow_property} is not set to desired value "
94-
f"'{shadow_desired_value}'; shadow actual state: {data}")
83+
i = 0
84+
while i < 10:
85+
try:
86+
if shadow_name:
87+
thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name, shadowName=shadow_name)
88+
else:
89+
thing_shadow = iot_data_client.get_thing_shadow(thingName=thing_name)
90+
91+
payload = thing_shadow['payload'].read()
92+
data = json.loads(payload)
93+
shadow_value = data.get('state', {}).get('reported', {}).get(shadow_property, None)
94+
if shadow_value == shadow_desired_value:
95+
test_result = 0
96+
break
97+
else:
98+
print(f"ERROR: Could not verify thing shadow: {shadow_property} is not set to desired value "
99+
f"'{shadow_desired_value}'; shadow actual state: {data}")
100+
test_result = -1
101+
except KeyError as e:
102+
print(f"ERROR: Could not verify thing shadow: key {e} does not exist in shadow response: {thing_shadow}")
95103
test_result = -1
96-
except KeyError as e:
97-
print(f"ERROR: Could not verify thing shadow: key {e} does not exist in shadow response: {thing_shadow}")
98-
test_result = -1
99-
except Exception as e:
100-
print(f"ERROR: Could not verify thing shadow: {e}")
101-
test_result = -1
104+
except Exception as e:
105+
print(f"ERROR: Could not verify thing shadow: {e}")
106+
test_result = -1
107+
i = i + 1
108+
time.sleep(3);
109+
102110

103111
if test_result == 0:
104112
print("Test succeeded")

servicetests/tests/ShadowUpdate/shadow_update.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,13 @@ def on_get_shadow_accepted(response):
100100
value = response.state.delta.get(shadow_property)
101101
if value:
102102
print(" Shadow contains delta value '{}'.".format(value))
103-
change_shadow_value(value)
104103
return
105104

106105
if response.state.reported:
107106
value = response.state.reported.get(shadow_property)
108107
if value:
109108
print(" Shadow contains reported value '{}'.".format(value))
110109
return
111-
112-
print(" Shadow document lacks '{}' property. Setting defaults...".format(shadow_property))
113-
change_shadow_value(SHADOW_VALUE_DEFAULT)
114110
return
115111

116112
except Exception as e:
@@ -128,12 +124,8 @@ def on_get_shadow_rejected(error):
128124
print("Ignoring get_shadow_rejected message due to unexpected token.")
129125
return
130126

131-
if error.code == 404:
132-
print("Thing has no shadow document. Creating with defaults...")
133-
change_shadow_value(SHADOW_VALUE_DEFAULT)
134-
else:
135-
exit("Get request was rejected. code:{} message:'{}'".format(
136-
error.code, error.message))
127+
if error.code != 404:
128+
exit("Get request was rejected. code:{} message:'{}'".format(error.code, error.message))
137129
except Exception as e:
138130
exit(e)
139131

@@ -193,32 +185,6 @@ def on_update_shadow_rejected(error):
193185
exit(e)
194186

195187

196-
def change_shadow_value(value):
197-
with locked_data.lock:
198-
199-
print("Changed local shadow value to '{}'.".format(value))
200-
locked_data.shadow_value = value
201-
202-
print("Updating reported shadow value to '{}'...".format(value))
203-
204-
# use a unique token so we can correlate this "request" message to
205-
# any "response" messages received on the /accepted and /rejected topics
206-
token = str(uuid4())
207-
208-
# if the value is "none" then set it to a Python none object to
209-
request = iotshadow.UpdateShadowRequest(
210-
thing_name=shadow_thing_name,
211-
state=iotshadow.ShadowState(
212-
reported={shadow_property: value},
213-
desired={shadow_property: value},
214-
),
215-
client_token=token,
216-
)
217-
future = shadow_client.publish_update_shadow(request, mqtt_qos)
218-
locked_data.request_tokens.add(token)
219-
future.add_done_callback(on_publish_update_shadow)
220-
221-
222188
def update_event_received(response):
223189
print("Update Event Received\n")
224190
print("Current response", response.current)
@@ -387,8 +353,6 @@ def update_shadow():
387353

388354
update_thing_update_future = shadow_client.publish_update_shadow(request = iotshadow.UpdateShadowRequest
389355
(thing_name = shadow_thing_name, state=state), qos=mqtt_qos)
390-
391-
change_shadow_value(cmdData.input_shadow_value)
392356
update_thing_update_future.result()
393357

394358
except Exception as e:
@@ -470,6 +434,3 @@ def update_shadow():
470434
exit(0)
471435
# Wait for the sample to finish
472436
is_sample_done.wait()
473-
474-
475-

0 commit comments

Comments
 (0)