def test_wait_cond_with_timeout(self):
        """ Test wait_cond using a timeout value """

        t_ref = time.time()
        self.assertTrue(common.wait_cond(10., True, lambda: True))
        self.assertGreater(10, time.time() - t_ref)

        t_ref = time.time()
        self.assertFalse(common.wait_cond(0.5, True, lambda: False))
        self.assertLessEqual(0.5, time.time() - t_ref)
    def test_wait_cond_with_timeout(self):
        """ Test wait_cond using a timeout value """

        t_ref = time.time()
        self.assertTrue(common.wait_cond(10., True, lambda: True))
        self.assertGreater(10, time.time() - t_ref)

        t_ref = time.time()
        self.assertFalse(common.wait_cond(0.5, True, lambda: False))
        self.assertLessEqual(0.5, time.time() - t_ref)
    def test_experiment_with_timeout(self):
        """ Start an experiment with a timeout. """
        extra = query_string('timeout=5')
        ret = self.server.post(EXP_START, extra_environ=extra)
        self.assertEqual(0, ret.json['ret'])

        # Wait max 10 seconds for experiment to have been stopped
        self.assertTrue(wait_cond(10, False, self._safe_exp_is_running))
        self.assertTrue(self.timeout_mock.called)
    def test_experiment_with_timeout(self):
        """ Start an experiment with a timeout. """
        extra = query_string('timeout=5')
        ret = self.server.post(EXP_START, extra_environ=extra)
        self.assertEquals(0, ret.json['ret'])

        # Wait max 10 seconds for experiment to have been stopped
        self.assertTrue(wait_cond(10, False, self._safe_exp_is_running))
        self.assertTrue(self.timeout_mock.called)
    def test_m3_exp_with_measures(self):  # pylint:disable=too-many-locals
        """ Run an experiment with measures and profile update """

        if self.board_cfg.cn_type != 'iotlab':
            pytest.skip("Not an iotlab control node (requires consumption and "
                        "radio features)")

        if self.board_cfg.board_class.TYPE != 'm3':
            pytest.skip("Not an M3")

        t_start = time.time()

        files = [
            file_tuple('firmware', self.board_cfg.board_class.FW_AUTOTEST),
        ]
        ret = self.server.post(EXP_START, upload_files=files)
        self.assertEqual(0, ret.json['ret'])

        # Copy files for further checking
        exp_files = self.g_m.exp_files.copy()

        # Set a profile for 10s, remove it and wait remaining measures
        self._update_profile('profile.json')
        time.sleep(10)
        self._update_profile(None)
        time.sleep(2)

        measures = autotest.extract_measures(self.cn_measures)
        self.cn_measures = []

        # # # # # # # # # #
        # Check measures  #
        # # # # # # # # # #

        # Got consumption and radio
        self.assertNotEqual([], measures['consumption']['values'])
        self.assertNotEqual([], measures['radio']['values'])

        # Validate values
        for values in measures['consumption']['values']:
            # no power, voltage in 3.3V, current not null
            self.assertTrue(math.isnan(values[0]))
            self.assertTrue(2.8 <= values[1] <= 3.5)
            self.assertNotEqual(0.0, values[2])
        for values in measures['radio']['values']:
            self.assertIn(values[0], [15, 26])
            self.assertLessEqual(-91, values[1])

        # check timestamps are sorted in correct order
        for values in measures.values():
            timestamps = [t_start] + values['timestamps'] + [time.time()]
            _sorted = all([a < b for a, b in izip(timestamps, timestamps[1:])])
            self.assertTrue(_sorted)

        # there should be no new measures since profile update
        # wait 5 more seconds that no measures arrive
        # wait_cond is used as 'check still false'
        wait_cond(5, False, lambda: [] == self.cn_measures)
        self.assertEqual([], self.cn_measures)

        # Stop experiment
        self.assertEqual(0, self.server.delete('/exp/stop').json['ret'])
        # Got no error during tests (use assertEquals for printing result)
        self.log_error.check()

        # # # # # # # # # # # # # # # # # # # # # #
        # Test OML Files still exists after stop  #
        #    * radio and conso file exist         #
        # # # # # # # # # # # # # # # # # # # # # #
        for meas_type in ('radio', 'consumption'):
            assert os.path.isfile(exp_files[meas_type])
            os.remove(exp_files[meas_type])
    def test_wait_cond_with_fct_param(self):
        """ Test wait_cond using a function with params """

        self.assertTrue(common.wait_cond(0, True, lambda x: x, True))
        self.assertTrue(common.wait_cond(0, True, lambda x: x, x=True))
    def test_wait_cond_no_timeout(self):
        """ Test wait_cond """

        self.assertTrue(common.wait_cond(0, True, lambda: True))
        self.assertFalse(common.wait_cond(0, True, lambda: False))
    def test_wait_cond_with_fct_param(self):
        """ Test wait_cond using a function with params """

        self.assertTrue(common.wait_cond(0, True, lambda x: x, True))
        self.assertTrue(common.wait_cond(0, True, lambda x: x, x=True))
    def test_wait_cond_no_timeout(self):
        """ Test wait_cond """

        self.assertTrue(common.wait_cond(0, True, lambda: True))
        self.assertFalse(common.wait_cond(0, True, lambda: False))
    def test_m3_exp_with_measures(self):
        """ Run an experiment with measures and profile update """

        if self.board_cfg.board_class.TYPE != 'm3':
            return
        t_start = time.time()

        files = [
            file_tuple('firmware', self.board_cfg.board_class.FW_AUTOTEST),
        ]
        ret = self.server.post(EXP_START, upload_files=files)
        self.assertEquals(0, ret.json['ret'])

        # Copy files for further checking
        exp_files = self.g_m.exp_files.copy()

        # Set a profile for 10s, remove it and wait remaining measures
        self._update_profile('profile.json')
        time.sleep(10)
        self._update_profile(None)
        time.sleep(2)

        measures = autotest.extract_measures(self.cn_measures)
        self.cn_measures = []

        # # # # # # # # # #
        # Check measures  #
        # # # # # # # # # #

        # Got consumption and radio
        self.assertNotEquals([], measures['consumption']['values'])
        self.assertNotEquals([], measures['radio']['values'])

        # Validate values
        for values in measures['consumption']['values']:
            # no power, voltage in 3.3V, current not null
            self.assertTrue(math.isnan(values[0]))
            self.assertTrue(2.8 <= values[1] <= 3.5)
            self.assertNotEquals(0.0, values[2])
        for values in measures['radio']['values']:
            self.assertIn(values[0], [15, 26])
            self.assertLessEqual(-91, values[1])

        # check timestamps are sorted in correct order
        for values in measures.values():
            timestamps = [t_start] + values['timestamps'] + [time.time()]
            _sorted = all([a < b for a, b in izip(timestamps, timestamps[1:])])
            self.assertTrue(_sorted)

        # there should be no new measures since profile update
        # wait 5 more seconds that no measures arrive
        # wait_cond is used as 'check still false'
        wait_cond(5, False, lambda: [] == self.cn_measures)
        self.assertEquals([], self.cn_measures)

        # Stop experiment
        self.assertEquals(0, self.server.delete('/exp/stop').json['ret'])
        # Got no error during tests (use assertEquals for printing result)
        self.log_error.check()

        # # # # # # # # # # # # # # # # # # # # # #
        # Test OML Files still exists after stop  #
        #    * radio and conso file exist         #
        # # # # # # # # # # # # # # # # # # # # # #
        for meas_type in ('radio', 'consumption'):
            try:
                os.remove(exp_files[meas_type])
            except IOError:
                self.fail('File should exist %r' % exp_files[meas_type])