Exemple #1
0
    def test_device_proxy_is_created_for_initial_observer(self):
        attr = Attribute('device', 'attribute')
        with call_via_mocks() as (mock_call, _):
            mgr = SubscriptionManager(proxy_factory=TangoDeviceProxyFactory())
            mgr.register_observer(attr, Mock())

        mock_call.assert_called_once_with("device")
Exemple #2
0
    def test_register_observer_adds_observer_to_appropriate_callback(self):
        attr = Attribute('device', 'attribute')
        observer = Mock()

        with call_via_mocks() as (mock_call, _):
            mgr = SubscriptionManager(proxy_factory=TangoDeviceProxyFactory())
            mgr.register_observer(attr, observer)
            assert observer in mgr._get_callback(attr)._observers
Exemple #3
0
    def test_existing_device_proxy_is_reused_for_subsequent_observers(self):
        with call_via_mocks() as (mock_call, _):
            mgr = SubscriptionManager(proxy_factory=TangoDeviceProxyFactory())
            for i in range(10):
                attr = Attribute('device', 'attribute')
                mgr.register_observer(attr, Mock())

        mock_call.assert_called_once_with("device")
Exemple #4
0
def test_tango_device_proxy_creates_device_proxy_to_named_device():
    """
    Confirm that the TangoDeviceProxyFactory creates a DeviceProxy using the
    device name given as an argument.
    """
    with patch("oet.command.tango") as mock_pytango:
        _ = TangoDeviceProxyFactory()("my device")
    mock_pytango.DeviceProxy.assert_called_once_with("my device")
Exemple #5
0
    def test_unregistering_does_not_create_a_new_subscription(self):
        attr = Attribute('device', 'attribute')
        observer = Mock()

        with call_via_mocks() as (mock_call, _):
            mgr = SubscriptionManager(proxy_factory=TangoDeviceProxyFactory())
            mgr.unregister_observer(attr, observer)

        assert not mock_call.called
Exemple #6
0
    def test_unsubscribe_all_releases_all_subscriptions(self):
        with call_via_mocks() as (mock_call, mock_proxy):
            mgr = SubscriptionManager(proxy_factory=TangoDeviceProxyFactory())
            for i in range(10):
                attr = Attribute('device', f'attr{i}')
                mgr.register_observer(attr, Mock())
            mgr._unsubscribe_all()

        for i in range(10):
            mock_proxy.unsubscribe_event.assert_any_call(i)
Exemple #7
0
 def test_unsubscribe_keeps_tango_subscription(self):
     """
     Check that the TangoExecutor.unsubscribe does not unsubscribe from Tango events.
     """
     attr = Attribute("device", "name")
     with call_via_mocks() as (_, mock_proxy):
         executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
         pid = executor.subscribe_event(attr)
         executor.unsubscribe_event(attr, pid)
     assert not mock_proxy.unsubscribe_event.called
Exemple #8
0
    def test_read_uses_cached_deviceproxy(self):
        """
        Check that the TangoExecutor uses cached DeviceProxy.
        """
        attr = Attribute("device", "name")

        with call_via_mocks() as (mock_call, _):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.read(attr)
            executor.read(attr)

        mock_call.assert_called_once_with("device")
Exemple #9
0
    def test_read_creates_proxy_to_specified_device(self):
        """
        Check that the TangoExecutor creates a proxy to the device specified in
        the Attribute.
        """
        attr = Attribute("device", "name")

        with call_via_mocks() as (mock_call, mock_proxy):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.read(attr)

        mock_call.assert_called_once_with("device")
Exemple #10
0
    def test_tango_executor_calls_subscribe_event_correctly(self):
        """
        Check that the TangoExecutor correctly invokes subscribe event.
        :return:
        """
        attr = Attribute("device", "name")

        with call_via_mocks() as (_, mock_proxy):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            response = executor.subscribe_event(attr)
        mock_proxy.subscribe_event.assert_called_once()
        assert response == -1
Exemple #11
0
    def test_only_one_device_proxy_per_device(self):
        """
        Check that the TangoExecutor uses cached DeviceProxy.
        """
        cmd = Command("device", "command")

        with call_via_mocks() as (mock_call, _):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.execute(cmd)
            executor.execute(cmd)

        mock_call.assert_called_once_with("device")
Exemple #12
0
    def test_executor_creates_proxy_to_correct_device(self):
        """
        Check that the TangoExecutor creates a proxy to the device specified in
        the Command.
        """
        cmd = Command("device", "command")

        with call_via_mocks() as (mock_call, _):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.execute(cmd)

        mock_call.assert_called_once_with("device")
Exemple #13
0
    def test_multiple_arg_commands_called_correctly(self):
        """
        Check that the TangoExecutor correctly invokes device server commands that
        accept multiple arguments.
        :return:
        """
        cmd = Command("device", "command", 1, 2, 3)

        with call_via_mocks() as (_, mock_proxy):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.execute(cmd)

        mock_proxy.command_inout.assert_called_once_with("command",
                                                         cmd_param=(1, 2, 3))
Exemple #14
0
    def test_single_arg_commands_called_correctly(self):
        """
        Check that the TangoExecutor correctly invokes device server commands that
        require a scalar argument.
        """
        cmd = Command("device", "command", 1)

        with call_via_mocks() as (_, mock_proxy):
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())
            executor.execute(cmd)

        # single-valued arg should be unpacked
        mock_proxy.command_inout.assert_called_once_with("command",
                                                         cmd_param=1)
Exemple #15
0
    def test_read_event_returns_event_from_callback(self):
        attr = Attribute("device", "name")
        expected_evt = Mock(spec=tango.EventData)

        # Tango calls the supplied callback on a new thread, supplying an
        # EventData as argument
        def fake_subscribe(_, __, cb):
            t = threading.Thread(target=cb, args=(expected_evt, ))
            t.start()
            t.join()
            # fake event subscription ID
            return 1

        with call_via_mocks() as (_, mock_proxy):
            mock_proxy.subscribe_event.side_effect = fake_subscribe
            executor = TangoExecutor(proxy_factory=TangoDeviceProxyFactory())

            with patch("oet.command.FEATURES") as mock_features:
                mock_features.discard_first_event = False
                _ = executor.subscribe_event(attr)
                received_evt = executor.read_event(attr)

        assert received_evt == expected_evt