<s> import time <EOL> from Adafruit_IO import MQTTClient <EOL> import base <EOL> TIMEOUT_SEC = <NUM_LIT:5> <EOL> class TestMQTTClient ( base . IOTestCase ) : <EOL> def wait_until_connected ( self , client , connect_value = True , <EOL> timeout_sec = TIMEOUT_SEC ) : <EOL> start = time . time ( ) <EOL> while client . is_connected ( ) != connect_value and ( time . time ( ) - start ) < timeout_sec : <EOL> client . loop ( ) <EOL> time . sleep ( <NUM_LIT:0> ) <EOL> def test_create_client ( self ) : <EOL> client = MQTTClient ( self . get_test_username ( ) , self . get_test_key ( ) ) <EOL> self . assertFalse ( client . is_connected ( ) ) <EOL> def test_connect ( self ) : <EOL> client = MQTTClient ( self . get_test_username ( ) , self . get_test_key ( ) ) <EOL> def on_connect ( mqtt_client ) : <EOL> self . assertEqual ( mqtt_client , client ) <EOL> client . on_connect = on_connect <EOL> client . connect ( ) <EOL> self . wait_until_connected ( client ) <EOL> self . assertTrue ( client . is_connected ( ) ) <EOL> def test_disconnect ( self ) : <EOL> client = MQTTClient ( self . get_test_username ( ) , self . get_test_key ( ) ) <EOL> def on_disconnect ( mqtt_client ) : <EOL> self . assertEqual ( mqtt_client , client ) <EOL> client . on_disconnect = on_disconnect <EOL> client . connect ( ) <EOL> self . wait_until_connected ( client ) <EOL> client . disconnect ( ) <EOL> self . wait_until_connected ( client , connect_value = False ) <EOL> self . assertFalse ( client . is_connected ( ) ) <EOL> def test_subscribe_and_publish ( self ) : <EOL> client = MQTTClient ( self . get_test_username ( ) , self . get_test_key ( ) ) <EOL> messages = [ ] <EOL> def on_message ( mqtt_client , feed , payload ) : <EOL> self . assertEqual ( mqtt_client , client ) <EOL> messages . append ( ( feed , payload ) ) <EOL> client . on_message = on_message <EOL> client . connect ( ) <EOL> self . wait_until_connected ( client ) <EOL> client . subscribe ( '<STR_LIT>' ) <EOL> client . publish ( '<STR_LIT>' , <NUM_LIT> ) <EOL> start = time . time ( ) <EOL> while len ( messages ) == <NUM_LIT:0> and ( time . time ( ) - start ) < TIMEOUT_SEC : <EOL> client . loop ( ) <EOL> time . sleep ( <NUM_LIT:0> ) <EOL> self . assertListEqual ( messages , [ ( '<STR_LIT>' , '<STR_LIT>' ) ] ) </s>
