Exemple #1
0
class TestServer(AsyncTestCase):
    def setUp(self):
        super(TestServer, self).setUp()
        self.server = Server(io_loop=self.io_loop, address="127.0.0.1", announce=False)

    def tearDown(self):
        super(TestServer, self).tearDown()
        if self.server.is_running:
            self.server.stop()

    def test_adding_kinects_after_start(self):
        mock = MockKinect()
        with self.server:
            assert len(self.server.kinects) == 0
            self.server.add_kinect(mock)
            assert len(self.server.kinects) == 1
            assert self.server.kinects[0] is mock
            self.server.remove_kinect(mock)
            assert len(self.server.kinects) == 0

    def test_adding_kinects_before(self):
        mock = MockKinect()
        assert len(self.server.kinects) == 0
        self.server.add_kinect(mock)
        assert len(self.server.kinects) == 1
        assert self.server.kinects[0] is mock
        self.server.remove_kinect(mock)
        assert len(self.server.kinects) == 0

    # Use a ZMQ-compatible I/O loop so that we can use `ZMQStream`.
    def get_new_ioloop(self):
        return ZMQIOLoop()
Exemple #2
0
    def setUp(self):
        super(TestClientConnection, self).setUp()

        # Start a server for this client
        self.server = Server(address='127.0.0.1',
                start_immediately=True, io_loop=self.io_loop, announce=False)
        self.endpoint = self.server.endpoints[EndpointType.control]
Exemple #3
0
    def run(self):
        # Create the server
        log.info('Creating server')
        server = Server()

        # Add mock kinect device to server
        kinect = MockKinect()
        server.add_kinect(kinect)

        # With the server and kinect running...
        log.info('Running server...')
        with server, kinect:
            # Run the ioloop
            ioloop.IOLoop.instance().start()

        # The server has now stopped
        log.info('Stopped')
Exemple #4
0
    def setUp(self):
        super(TestBasicClient, self).setUp()

        # Start a server for this client
        self.server = Server(address='127.0.0.1',
            start_immediately=True, io_loop=self.io_loop, announce=False)

        # Start the client. Use a fast heartbeat to make testing quick
        control_endpoint = self.server.endpoints[EndpointType.control]
        log.info('Started server with control endpoint: {0}'.format(control_endpoint))
        self.client = Client(control_endpoint, io_loop=self.io_loop)

        # Increase heartbeat frequency to make tests quicker
        self.client.heartbeat_period = 100

        # Connect client
        self.client.connect()
Exemple #5
0
 def setUp(self):
     super(TestServer, self).setUp()
     self.server = Server(io_loop=self.io_loop, address="127.0.0.1", announce=False)
Exemple #6
0
def test_server_start():
    s = Server(start_immediately=True, address="127.0.0.1", announce=False)
    assert s.is_running
    s.stop()
    assert not s.is_running
Exemple #7
0
class TestClientConnection(AsyncTestCase):
    def setUp(self):
        super(TestClientConnection, self).setUp()

        # Start a server for this client
        self.server = Server(address='127.0.0.1',
                start_immediately=True, io_loop=self.io_loop, announce=False)
        self.endpoint = self.server.endpoints[EndpointType.control]

    def tearDown(self):
        super(TestClientConnection, self).tearDown()
        if self.server.is_running:
            self.server.stop()

    @raises(RuntimeError)
    def test_client_must_be_connected(self):
        client = Client(self.endpoint, io_loop=self.io_loop)
        client.ping()

    def test_client_with_statement(self):
        with Client(self.endpoint, io_loop=self.io_loop) as client:
            assert client.is_connected
        assert not client.is_connected

    def test_explicit_connect_disconnect(self):
        client = Client(self.endpoint, io_loop=self.io_loop)
        assert not client.is_connected
        client.connect()
        assert client.is_connected
        client.disconnect()
        assert not client.is_connected

    def test_immediate_connect(self):
        client = Client(self.endpoint, connect_immediately=True, io_loop=self.io_loop)
        assert client.is_connected
        client.disconnect()
        assert not client.is_connected

    def test_explicit_connect_sends_events(self):
        state = { 'on_connect': 0 }

        client = Client(self.endpoint, io_loop=self.io_loop)

        @client.on_connect.connect_via(client)
        def on_connect_called(sender):
            assert sender is client
            state['on_connect'] += 1

        assert state['on_connect'] == 0
        client.connect()

        self.keep_checking(lambda: state['on_connect'] != 0)
        self.wait()
        assert state['on_connect'] == 1

        client.disconnect()
        assert not client.is_connected

    def test_explicit_disconnect_sends_events(self):
        state = { 'on_disconnect': 0 }

        client = Client(self.endpoint, io_loop=self.io_loop)

        @client.on_disconnect.connect_via(client)
        def on_disconnect_called(sender):
            assert sender is client
            state['on_disconnect'] += 1

        assert state['on_disconnect'] == 0
        client.connect()
        self.keep_checking(lambda: client.is_connected)
        self.wait()

        client.disconnect()
        self.keep_checking(lambda: state['on_disconnect'] != 0)
        self.wait()
        assert state['on_disconnect'] == 1

    def test_client_disconnects_when_server_disappears(self):
        client = Client(self.endpoint, io_loop=self.io_loop)

        assert not client.is_connected
        client.heartbeat_period = 200
        client.response_timeout = 500
        client.connect()

        # Wait for server identity
        self.keep_checking(lambda: client.server_name is not None)
        self.wait()

        # Stop server
        self.server.stop()

        # Client should eventually disconnect
        self.keep_checking(lambda: not client.is_connected)
        self.wait()
Exemple #8
0
class TestBasicClient(AsyncTestCase):
    def setUp(self):
        super(TestBasicClient, self).setUp()

        # Start a server for this client
        self.server = Server(address='127.0.0.1',
            start_immediately=True, io_loop=self.io_loop, announce=False)

        # Start the client. Use a fast heartbeat to make testing quick
        control_endpoint = self.server.endpoints[EndpointType.control]
        log.info('Started server with control endpoint: {0}'.format(control_endpoint))
        self.client = Client(control_endpoint, io_loop=self.io_loop)

        # Increase heartbeat frequency to make tests quicker
        self.client.heartbeat_period = 100

        # Connect client
        self.client.connect()

    def tearDown(self):
        super(TestBasicClient, self).tearDown()

        if self.client.is_connected:
            self.client.disconnect()
        if self.server.is_running:
            self.server.stop()

    def test_control_endpoint(self):
        control_endpoint = self.server.endpoints[EndpointType.control]
        log.info('Testing client control endpoint {0} matches server endpoint {1}'.format(
            self.client.endpoints[EndpointType.control], control_endpoint))
        assert self.client.endpoints[EndpointType.control] == control_endpoint

    def test_device_before_connect(self):
        self.server.add_kinect(MockKinect())
        self.keep_checking(lambda: len(self.client.kinect_ids) == 1)
        self.wait()

    def test_device_after_connect_heartbeat(self):
        self.keep_checking(lambda: self.client.server_name is not None)
        self.wait()

        self.server.add_kinect(MockKinect())

        self.keep_checking(lambda: len(self.client.kinect_ids) == 1)
        self.wait()

    def test_add_signal_device_after_connect(self):
        self.keep_checking(lambda: self.client.server_name is not None)
        self.wait()

        k = MockKinect()

        state = { 'n_kinect_added': 0, 'n_kinect_removed': 0 }

        @self.client.on_add_kinect.connect_via(self.client)
        def on_add(client, kinect_id):
            log.info('Add kinect {0}'.format(kinect_id))
            assert kinect_id == k.unique_kinect_id
            state['n_kinect_added'] += 1

        @self.client.on_remove_kinect.connect_via(self.client)
        def on_add(client, kinect_id):
            log.info('Remove kinect {0}'.format(kinect_id))
            assert kinect_id == k.unique_kinect_id
            state['n_kinect_removed'] += 1

        self.server.add_kinect(k)
        self.keep_checking(lambda: state['n_kinect_added'] == 1)
        self.wait()

        self.server.remove_kinect(k)
        self.keep_checking(lambda: state['n_kinect_removed'] == 1)
        self.wait()

    def test_server_name(self):
        def condition():
            return self.client.server_name == self.server.name

        self.keep_checking(condition)
        self.wait()

    def test_ping(self):
        def pong():
            log.info('Got pong from server')
            self.stop(True)

        self.client.ping(pong)
        assert self.wait()

    def test_many_pings(self):
        state = { 'n_pings': 10, 'n_pongs': 0 }
        def pong():
            log.info('Got pong from server')
            state['n_pongs'] += 1

        for _ in range(state['n_pings']):
            self.client.ping(pong)

        self.keep_checking(lambda: state['n_pongs'] == state['n_pings'])
        self.wait()

    @raises(ValueError)
    def test_cannot_receive_depth_frames_from_bad_device(self):
        k = MockKinect()
        self.client.enable_depth_frames(k.unique_kinect_id)

    def test_receives_depth_frames(self):
        k = MockKinect()

        state = { 'n_depth_frames': 0 }
        @self.client.on_depth_frame.connect_via(self.client)
        def on_depth_frame(client, depth_frame, kinect_id):
            assert k.unique_kinect_id == kinect_id
            state['n_depth_frames'] += 1

        @self.client.on_add_kinect.connect_via(self.client)
        def on_add_kinect(client, kinect_id):
            assert client == self.client
            assert kinect_id == k.unique_kinect_id
            client.enable_depth_frames(kinect_id)

        with k:
            self.server.add_kinect(k)
            self.keep_checking(lambda: state['n_depth_frames'] > 1)
            self.wait()