Skip to content

Commit 81701c5

Browse files
authored
Cleanly disconnect client before JWT expiration. (GoogleCloudPlatform#2622)
1 parent 78574a5 commit 81701c5

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

iot/api-client/manager/manager_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ def test_send_command(test_topic, capsys):
391391

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

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

402401
# Process commands
403402
for i in range(1, 5):
404-
client.loop()
405403
time.sleep(1)
406404

407405
# Clean up
406+
client.loop_stop()
407+
client.disconnect()
408408
manager.delete_device(
409409
service_account_json, project_id, cloud_region, registry_id,
410410
device_id)

iot/api-client/mqtt_example/cloudiot_mqtt_example.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ def listen_for_messages(
242242
if seconds_since_issue > 60 * jwt_exp_mins:
243243
print('Refreshing token after {}s'.format(seconds_since_issue))
244244
jwt_iat = datetime.datetime.utcnow()
245+
client.loop()
246+
client.disconnect()
245247
client = get_client(
246248
project_id, cloud_region, registry_id, gateway_id,
247249
private_key_file, algorithm, ca_certs, mqtt_bridge_hostname,
@@ -457,6 +459,8 @@ def mqtt_device_demo(args):
457459
if seconds_since_issue > 60 * jwt_exp_mins:
458460
print('Refreshing token after {}s'.format(seconds_since_issue))
459461
jwt_iat = datetime.datetime.utcnow()
462+
client.loop()
463+
client.disconnect()
460464
client = get_client(
461465
args.project_id, args.cloud_region,
462466
args.registry_id, args.device_id, args.private_key_file,
@@ -469,7 +473,9 @@ def mqtt_device_demo(args):
469473
client.publish(mqtt_topic, payload, qos=1)
470474

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

475481

0 commit comments

Comments
 (0)