def testCode6Case2(self): """unsubscribe the topic before subscribe""" print('Code6Case2') print(microgear.gearkey) try: print("run helper...") code = str(51) args = ['python', 'helper.py', code] p = subprocess.Popen(args, cwd=(helper_dir)) time.sleep(connect_worst_timeout) print(microgear.gearkey) client.create(self.gearkey, self.gearsecret, self.appid) client.on_connect = MagicMock() client.on_message = MagicMock() client.connect() time.sleep(connect_timeout) self.assertTrue(client.on_connect.called) self.connected = True self.assertEqual(client.on_connect.call_count, 1) self.assertFalse(client.on_message.called) client.unsubscribe(self.topic) self.assertFalse(client.on_message.called) client.subscribe(self.topic) time.sleep(message_timeout) self.assertTrue(client.on_message.called) client.on_message.assert_called_with(self.expectedTopic, self.expectedMessage) p.kill() except Exception as e: p.kill() raise Exception(e.args)
def testCode6Case1(self): """unsubscribe the subscribed topic""" print('Code6Case1') try: print("run helper...") code = str(51) args = ['python', 'helper.py', code] p = subprocess.Popen(args, cwd=(helper_dir)) time.sleep(connect_worst_timeout) client.create(self.gearkey, self.gearsecret, self.appid) client.on_connect = MagicMock() client.on_message = MagicMock() client.connect() time.sleep(connect_timeout) self.assertTrue(client.on_connect.called) self.connected = True self.assertEqual(client.on_connect.call_count, 1) client.subscribe(self.topic) time.sleep(message_timeout) self.assertTrue(client.on_message.called) client.unsubscribe(self.topic) client.on_message.reset_mock() self.assertFalse(client.on_message.called) time.sleep(message_timeout) self.assertFalse(client.on_message.called) p.kill() #if fails due to assertion error except Exception as e: p.kill() raise Exception(e.args)
def testCode5Case3(self): """subscribe same topic twice""" try: print('Code5Case3') print("run helper...") code = str(51) args = ['python', 'helper.py', code] p = subprocess.Popen(args, cwd=(helper_dir)) time.sleep(connect_worst_timeout) client.create(self.gearkey, self.gearsecret, self.appid) client.subscribe(self.topic) client.on_connect = MagicMock() client.on_message = MagicMock() client.connect() time.sleep(connect_timeout) self.assertTrue(client.on_connect.called) connected = True self.assertEqual(client.on_connect.call_count, 1) time.sleep(message_timeout) self.assertTrue(client.on_message.called) client.on_message.assert_called_with(self.expectedTopic, self.expectedMessage) #unsubscribe after receive message client.unsubscribe(self.topic) client.on_message.reset_mock() #reset count recorded by mock self.assertFalse(client.on_message.called) time.sleep(message_timeout) self.assertFalse(client.on_message.called) #should not receive message now client.subscribe(self.topic) #subscribe again time.sleep(message_timeout) self.assertTrue(client.on_message.called) #should receive message p.kill() #if fails due to assertion error except Exception as e: p.kill() raise Exception(e.args)
def testCode6Case5x1(self): """unsubscribe the invalid topic - no slash""" try: print("run helper...") code = str(51) args = ['python', 'helper.py', code] p = subprocess.Popen(args, cwd=(helper_dir)) time.sleep(connect_worst_timeout) self.invalidStr = "firstTopic" client.create(self.gearkey, self.gearsecret, self.appid) client.on_connect = MagicMock() client.on_message = MagicMock() client.connect() time.sleep(connect_timeout) self.assertTrue(client.on_connect.called) self.connected = True self.assertEqual(client.on_connect.call_count, 1) self.assertFalse(client.on_message.called) client.subscribe(self.topic) time.sleep(message_timeout) self.assertTrue(client.on_message.called) client.on_message.assert_called_with(self.expectedTopic, self.expectedMessage) client.on_message.reset_mock() self.assertFalse(client.on_message.called) client.unsubscribe(self.invalidStr) time.sleep(connect_timeout) self.assertTrue(client.on_message.called) client.on_message.assert_called_with(self.expectedTopic, self.expectedMessage) p.kill() except Exception as e: p.kill() raise Exception(e.args)
def subscription(topic,message): print(topic+" "+message) client.unsubscribe("/secondTopic")