Skip to content

Commit

Permalink
Cleanly disconnect client before JWT expiration. (#2622)
Browse files Browse the repository at this point in the history
  • Loading branch information
gguuss committed Dec 17, 2019
1 parent 78574a5 commit 81701c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions iot/api-client/manager/manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ def test_send_command(test_topic, capsys):

# Pre-process commands
for i in range(1, 5):
client.loop()
time.sleep(1)

manager.send_command(
Expand All @@ -401,10 +400,11 @@ def test_send_command(test_topic, capsys):

# Process commands
for i in range(1, 5):
client.loop()
time.sleep(1)

# Clean up
client.loop_stop()
client.disconnect()
manager.delete_device(
service_account_json, project_id, cloud_region, registry_id,
device_id)
Expand Down
8 changes: 7 additions & 1 deletion iot/api-client/mqtt_example/cloudiot_mqtt_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def listen_for_messages(
if seconds_since_issue > 60 * jwt_exp_mins:
print('Refreshing token after {}s'.format(seconds_since_issue))
jwt_iat = datetime.datetime.utcnow()
client.loop()
client.disconnect()
client = get_client(
project_id, cloud_region, registry_id, gateway_id,
private_key_file, algorithm, ca_certs, mqtt_bridge_hostname,
Expand Down Expand Up @@ -457,6 +459,8 @@ def mqtt_device_demo(args):
if seconds_since_issue > 60 * jwt_exp_mins:
print('Refreshing token after {}s'.format(seconds_since_issue))
jwt_iat = datetime.datetime.utcnow()
client.loop()
client.disconnect()
client = get_client(
args.project_id, args.cloud_region,
args.registry_id, args.device_id, args.private_key_file,
Expand All @@ -469,7 +473,9 @@ def mqtt_device_demo(args):
client.publish(mqtt_topic, payload, qos=1)

# Send events every second. State should not be updated as often
time.sleep(1 if args.message_type == 'event' else 5)
for i in range(0, 60):
time.sleep(1)
client.loop()
# [END iot_mqtt_run]


Expand Down

0 comments on commit 81701c5

Please sign in to comment.