Beispiel #1
0
 def test_broadcast_methods_should_raise_RuntimeError_if_not_connected_to_broadcast(self):
     client = Client()
     client.connect(api=API_ADDRESS)
     with self.assertRaises(RuntimeError):
         client.broadcast_subscribe('42')
     with self.assertRaises(RuntimeError):
         client.broadcast_unsubscribe('42')
     with self.assertRaises(RuntimeError):
         client.broadcast_poll(timeout=1) # milliseconds
     with self.assertRaises(RuntimeError):
         client.broadcast_receive()
     with self.assertRaises(RuntimeError):
         client.disconnect_broadcast()
Beispiel #2
0
    def test_disconnect(self):
        client = Client()
        client.connect(api=API_ADDRESS, broadcast=BROADCAST_ADDRESS)

        #connected we can communicate...
        client.send_api_request({'command': 'get configuration'})
        self.assertTrue(self.api.poll(TIMEOUT))
        self.api.recv_json()
        self.api.send_json({'command': 'ok'})
        self.assertTrue(client.api_poll(TIMEOUT))
        self.assertEqual(client.get_api_reply(), {'command': 'ok'})
        client.broadcast_subscribe('spam')
        time.sleep(TIMEOUT / 1000.0) # wait for subscribe to take effect
        self.broadcast.send('spam eggs ham')
        self.assertTrue(client.broadcast_poll(TIMEOUT))
        self.assertEqual(client.broadcast_receive(), 'spam eggs ham')

        #disconnected not!
        client.disconnect_api()
        with self.assertRaises(RuntimeError):
            client.send_api_request({'command': 'get configuration'})
        with self.assertRaises(RuntimeError):
            client.api_poll(TIMEOUT)
        with self.assertRaises(RuntimeError):
            client.get_api_reply()
        client.broadcast_subscribe('spam')
        time.sleep(TIMEOUT / 1000.0) # wait for subscribe to take effect
        self.broadcast.send('spam eggs ham')
        self.assertTrue(client.broadcast_poll(TIMEOUT))
        self.assertEqual(client.broadcast_receive(), 'spam eggs ham')
        client.disconnect_broadcast()
        with self.assertRaises(RuntimeError):
            client.broadcast_subscribe('spam')
        with self.assertRaises(RuntimeError):
            client.broadcast_poll(TIMEOUT)
        with self.assertRaises(RuntimeError):
            client.broadcast_receive()

        #connect again...
        client.connect(api=API_ADDRESS, broadcast=BROADCAST_ADDRESS)
        client.send_api_request({'command': 'get configuration'})
        self.assertTrue(self.api.poll(TIMEOUT))
        self.api.recv_json()
        self.api.send_json({'command': 'ok'})
        self.assertTrue(client.api_poll(TIMEOUT))
        self.assertEqual(client.get_api_reply(), {'command': 'ok'})
        client.broadcast_subscribe('spam')
        time.sleep(TIMEOUT / 1000.0) # wait for subscribe to take effect
        self.broadcast.send('spam eggs ham')
        self.assertTrue(client.broadcast_poll(TIMEOUT))
        self.assertEqual(client.broadcast_receive(), 'spam eggs ham')

        #disconnected everything
        client.disconnect()
        with self.assertRaises(RuntimeError):
            client.send_api_request({'command': 'get configuration'})
        with self.assertRaises(RuntimeError):
            client.api_poll(TIMEOUT)
        with self.assertRaises(RuntimeError):
            client.get_api_reply()
        with self.assertRaises(RuntimeError):
            client.broadcast_subscribe('spam')
        with self.assertRaises(RuntimeError):
            client.broadcast_poll(TIMEOUT)
        with self.assertRaises(RuntimeError):
            client.broadcast_receive()
        with self.assertRaises(RuntimeError):
            client.disconnect()
        # Should not raises:
        client.disconnect(silent=True)
        client.disconnect_api(silent=True)
        client.disconnect_broadcast(silent=True)