def test_list_sensors(self):
        resource_spec = dict(
            name='testdev',
            address=('testhost', 12345))
        DUT = resource_client.KATCPClientResource(resource_spec)
        sens_manager = mock.create_autospec(
            resource_client.KATCPClientResourceSensorsManager(mock.Mock(), "test"))
        test_sensors_info = AttrDict(
            sens_one=AttrDict(name='sens-one', description='sensor one', value=1),
            sens_two=AttrDict(name='sens.two', description='sensor one', value=2),
            sens_three=AttrDict(name='sens_three', description='sensor three', value=3))
        sensor_strategies = dict(sens_one='event', sens_three='period 10')

        def make_test_sensors(sensors_info):
            test_sensors = AttrDict()
            for sens_pyname, info in sensors_info.items():
                info = dict(info)
                info['sensor_type'] = Sensor.INTEGER
                val = info.pop('value')
                timestamp = val*10
                received_timestamp = timestamp + 1
                sens = test_sensors[sens_pyname] =  resource.KATCPSensor(
                    info, sens_manager)
                sens._reading = resource.KATCPSensorReading(
                    received_timestamp, timestamp, Sensor.NOMINAL, val)
                test_sensors[sens_pyname] = sens
            return test_sensors

        test_sensors = make_test_sensors(test_sensors_info)

        sens_manager.get_sampling_strategy.side_effect = (
            lambda sens_name: resource.normalize_strategy_parameters(
                sensor_strategies.get(
                    resource.escape_name(sens_name), 'none')) )

        DUT.sensor.update(test_sensors)

        # Simple search based on python identifier
        result = yield DUT.list_sensors('sens_one')
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0], resource.SensorResultTuple(
            test_sensors.sens_one, test_sensors_info.sens_one.name,
            'sens_one', test_sensors_info.sens_one.description, 'integer', '',
            test_sensors.sens_one.reading))

        # Now get all the sensors
        result = yield DUT.list_sensors('')
        expected_result = sorted(resource.SensorResultTuple(
            test_sensors[s_id], test_sensors_info[s_id].name,
            s_id, test_sensors_info[s_id].description, 'integer', '',
            test_sensors[s_id].reading)
                                 for s_id in test_sensors_info)
        self.assertEqual(sorted(result), expected_result)

        # Test that all sensors are found using their Python identifiers
        result = yield DUT.list_sensors('sens_two')
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].object, test_sensors.sens_two)
        result = yield DUT.list_sensors('sens_three')
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].object, test_sensors.sens_three)

        # Test using actual sensor name
        result = yield DUT.list_sensors('sens_one', use_python_identifiers=False)
        self.assertEqual(len(result), 0)
        result = yield DUT.list_sensors('sens-one', use_python_identifiers=False)
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].name, 'sens-one')

        # Now test with strategy filter
        result = yield DUT.list_sensors('', strategy=True)
        self.assertEqual(len(result), len(sensor_strategies))
    def test_wait(self):
        sensor_manager = mock.Mock()
        sensor_manager.get_sampling_strategy.return_value = (
            resource.normalize_strategy_parameters('none'))
        DUT = resource.KATCPSensor(
            dict(sensor_type=Sensor.INTEGER,
                 name='test.int',
                 params=[-1000, 1000],
                 default=0,
                 initial_status=Sensor.NOMINAL), sensor_manager)
        # Test that an exception is raised if no strategy is set
        with self.assertRaises(resource.KATCPSensorError):
            yield DUT.wait(1)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Test that timeout works
        sensor_manager.get_sampling_strategy.return_value = (
            resource.normalize_strategy_parameters('event'))
        # Add an ioloop callback to timewarp so that we don't get stuck
        timeout = 0.25
        self.io_loop.add_callback(self.set_ioloop_time,
                                  self.io_loop.time() + timeout + 0.1)
        # Check that the timout error is raised
        with self.assertRaises(tornado.gen.TimeoutError):
            yield DUT.wait(1, timeout=timeout)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait for a value that changes within the timeout
        self.io_loop.add_callback(self.set_ioloop_time,
                                  self.io_loop.time() + timeout - 0.1)
        waiting_value = 2
        self.io_loop.add_callback(DUT.set_value, waiting_value)
        yield DUT.wait(waiting_value, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait using a callable, comparison times out
        self.io_loop.add_callback(self.set_ioloop_time,
                                  self.io_loop.time() + timeout + 0.1)
        waiting_value = 11
        waiting_condition = lambda reading: reading.value == waiting_value
        # Check that the timout error is raised
        with self.assertRaises(tornado.gen.TimeoutError):
            yield DUT.wait(waiting_value, timeout=timeout)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait for a callable condition that is reached within the timeout
        self.io_loop.add_callback(self.set_ioloop_time,
                                  self.io_loop.time() + timeout - 0.1)
        waiting_value = 12
        waiting_condition = lambda reading: reading.value == waiting_value
        self.io_loop.add_callback(DUT.set_value, waiting_value)
        yield DUT.wait(waiting_condition, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # wait for value and status
        waiting_value = 3
        waiting_status = 'warn'
        waiting_condition = lambda reading: reading.value == waiting_value and \
                                            reading.status == waiting_status
        self.io_loop.add_callback(DUT.set_value, 3, Sensor.WARN)
        yield DUT.wait(waiting_condition, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        self.assertEqual(DUT.status, waiting_status)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)
Exemple #3
0
    def test_wait(self):
        sensor_manager = mock.Mock()
        sensor_manager.get_sampling_strategy.return_value = (
            resource.normalize_strategy_parameters('none'))
        DUT = resource.KATCPSensor(dict(sensor_type=Sensor.INTEGER,
                                        name='test.int', params=[-1000, 1000],
                                        default=0, initial_status=Sensor.NOMINAL),
                                   sensor_manager)
        # Test that an exception is raised if no strategy is set
        with self.assertRaises(resource.KATCPSensorError):
            yield DUT.wait(1)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Test that timeout works
        sensor_manager.get_sampling_strategy.return_value = (
            resource.normalize_strategy_parameters('event'))
        # Add an ioloop callback to timewarp so that we don't get stuck
        timeout = 0.25
        self.io_loop.add_callback(
            self.set_ioloop_time, self.io_loop.time() + timeout + 0.1)
        # Check that the timout error is raised
        with self.assertRaises(tornado.gen.TimeoutError):
            yield DUT.wait(1, timeout=timeout)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait for a value that changes within the timeout
        self.io_loop.add_callback(
            self.set_ioloop_time, self.io_loop.time() + timeout - 0.1)
        waiting_value = 2
        self.io_loop.add_callback(DUT.set_value, waiting_value)
        yield DUT.wait(waiting_value, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait using a callable, comparison times out
        self.io_loop.add_callback(
            self.set_ioloop_time, self.io_loop.time() + timeout + 0.1)
        waiting_value = 11
        waiting_condition = lambda reading: reading.value == waiting_value
        # Check that the timout error is raised
        with self.assertRaises(tornado.gen.TimeoutError):
            yield DUT.wait(waiting_value, timeout=timeout)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # Wait for a callable condition that is reached within the timeout
        self.io_loop.add_callback(
            self.set_ioloop_time, self.io_loop.time() + timeout - 0.1)
        waiting_value = 12
        waiting_condition = lambda reading: reading.value == waiting_value
        self.io_loop.add_callback(DUT.set_value, waiting_value)
        yield DUT.wait(waiting_condition, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)

        # wait for value and status
        waiting_value = 3
        waiting_status = 'warn'
        waiting_condition = lambda reading: reading.value == waiting_value and \
                                            reading.status == waiting_status
        self.io_loop.add_callback(DUT.set_value, 3, Sensor.WARN)
        yield DUT.wait(waiting_condition, timeout=timeout)
        self.assertEqual(DUT.value, waiting_value)
        self.assertEqual(DUT.status, waiting_status)
        # Check that no stray listeners are left behind
        self.assertFalse(DUT._listeners)