def test_in_backgroud(self):
        probe = Probe(timeout=1, pause=0.5, fnc=snoozer, seconds=0.1)

        probe.run()
        assert not probe.is_alive()

        probe.run_in_background()
        assert probe.is_alive()
        probe.terminate()
        probe.join()
        assert not probe.is_alive()
    def test_reach_timeout(self):
        # probe should reach timeout with long-running function calls
        # probe and caller in one thread
        start = time.time()
        probe = Probe(timeout=1, pause=0.2, fnc=snoozer, seconds=10)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() - start) < 3, "Time elapsed is way too high"

        # in background
        start = time.time()
        probe = Probe(timeout=1, pause=0.2, fnc=snoozer, seconds=10)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() - start) < 3, "Time elapsed is way too high"
Example #3
0
    def test_reach_timeout(self):
        # probe should reach timeout with long-running function calls
        # probe and caller in one thread
        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=snoozer, seconds=10)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # in background
        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=snoozer, seconds=10)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"
Example #4
0
    def test_exception(self):

        # probe and caller in one thread
        # probe should ignore expected_exceptions
        start = time.time()
        probe = Probe(timeout=3,
                      pause=0.5,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # probe should not ignore exceptions other than expected exceptions
        start = time.time()
        probe = Probe(timeout=5,
                      pause=0.5,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()
        assert (time.time() - start) < 1, "Timeout exceeded"

        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()
        assert (time.time() - start) < 1, "Timeout exceeded"

        # run in background
        # probe should ignore expected_exceptions
        start = time.time()
        probe = Probe(timeout=3,
                      pause=0.5,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # probe should not ignore exceptions other than expected exceptions
        start = time.time()
        probe = Probe(timeout=5,
                      pause=0.5,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()
        assert (time.time() - start) < 1, "Timeout exceeded"

        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()
        assert (time.time() - start) < 1, "Timeout exceeded"
Example #5
0
    def test_exception(self):

        # probe and caller in one thread
        # probe should ignore expected_exceptions
        probe = Probe(timeout=1, pause=0.2, expected_exceptions=ValueError, fnc=value_err_raise)
        with pytest.raises(ProbeTimeout):
            probe.run()

        # probe should not ignore exceptions other than expected exceptions
        probe = Probe(timeout=1, pause=0.2, expected_exceptions=ImportError, fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()

        probe = Probe(timeout=1, pause=0.2, fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()

        # run in background
        # probe should ignore expected_exceptions
        probe = Probe(timeout=1, pause=0.2, expected_exceptions=ValueError, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()

        # probe should not ignore exceptions other than expected exceptions
        probe = Probe(timeout=1, pause=0.2, expected_exceptions=ImportError, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()

        probe = Probe(timeout=1, pause=0.2, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()