Ejemplo n.º 1
0
    def test_sampling(self):
        """Test getting and setting the sampling."""
        s = self.sensor

        sampling.SampleNone(None, s)
        sampling.SampleAuto(None, s)
        sampling.SamplePeriod(None, s, 10)
        sampling.SampleEvent(None, s)
        sampling.SampleDifferential(None, s, 2)
        self.assertRaises(ValueError, sampling.SampleNone, None, s, "foo")
        self.assertRaises(ValueError, sampling.SampleAuto, None, s, "bar")
        self.assertRaises(ValueError, sampling.SamplePeriod, None, s)
        self.assertRaises(ValueError, sampling.SamplePeriod, None, s, "1.5")
        self.assertRaises(ValueError, sampling.SamplePeriod, None, s, "-1")
        self.assertRaises(ValueError, sampling.SampleEvent, None, s, "foo")
        self.assertRaises(ValueError, sampling.SampleDifferential, None, s)
        self.assertRaises(ValueError, sampling.SampleDifferential, None, s,
                          "-1")
        self.assertRaises(ValueError, sampling.SampleDifferential, None, s,
                          "1.5")

        sampling.SampleStrategy.get_strategy("none", None, s)
        sampling.SampleStrategy.get_strategy("auto", None, s)
        sampling.SampleStrategy.get_strategy("period", None, s, "15")
        sampling.SampleStrategy.get_strategy("event", None, s)
        sampling.SampleStrategy.get_strategy("differential", None, s, "2")
        self.assertRaises(ValueError, sampling.SampleStrategy.get_strategy,
                          "random", None, s)
        self.assertRaises(ValueError, sampling.SampleStrategy.get_strategy,
                          "period", None, s, "foo")
        self.assertRaises(ValueError, sampling.SampleStrategy.get_strategy,
                          "differential", None, s, "bar")
Ejemplo n.º 2
0
    def test_auto(self):
        t0 = self.ioloop_time
        DUT = sampling.SampleAuto(self.inform, self.sensor)
        self.assertEqual(DUT.get_sampling_formatted(), (b'auto', []))
        self.assertEqual(self.calls, [])
        t, status, value = self.sensor.read()
        DUT.start()
        yield self.wake_ioloop()
        # Check that it is attached
        self.assertTrue(DUT in self.sensor._observers)
        # The initial update
        self.assertEqual(self.calls, [(self.sensor, (t, status, value))])
        self.calls = []
        # Move along in time, don't expect any updates
        yield self.set_ioloop_time(t0 + 20)
        self.assertEqual(self.calls, [])
        # Now update the sensor a couple of times
        t1, status1, value1 = t0 + 21, Sensor.ERROR, 2
        t2, status2, value2 = t0 + 22, Sensor.NOMINAL, -1
        self.sensor.set(t1, status1, value1)
        self.sensor.set(t2, status2, value2)
        self.assertEqual(self.calls, [(self.sensor, (t1, status1, value1)),
                                      (self.sensor, (t2, status2, value2))])
        self.calls = []

        self._thread_update_check(t, status, value)
        yield self._check_cancel(DUT)