Example #1
0
 def test_total_time_slept_accumulates(self):
     with sleep.mocked() as sleep_counter:
         sleep(1)
         self.assertThat(sleep_counter.total_time_slept(), Equals(1.0))
         sleep(0.5)
         self.assertThat(sleep_counter.total_time_slept(), Equals(1.5))
         sleep(0.5)
         self.assertThat(sleep_counter.total_time_slept(), Equals(2.0))
Example #2
0
    def test_find_matching_connections_dedupes_results_on_pid(self):
        bus = ProxyObjectTests.FMCTest()

        with patch.object(_s, '_dedupe_connections_on_pid') as dedupe:
            with sleep.mocked():
                _s._find_matching_connections(bus, lambda *args: True)

                dedupe.assert_called_once_with(["conn1"], bus)
Example #3
0
    def test_find_matching_connections_calls_connection_matcher(self):
        bus = ProxyObjectTests.FMCTest()
        connection_matcher = Mock(return_value=False)

        with sleep.mocked():
            _s._find_matching_connections(bus, connection_matcher)

        connection_matcher.assert_called_with((bus, "conn1"))
Example #4
0
 def test_long_elapsed_time_increases(self):
     with sleep.mocked():
         last_elapsed = None
         for elapsed in Timeout.long():
             if last_elapsed is not None:
                 self.assertThat(elapsed, GreaterThan(last_elapsed))
             else:
                 self.assertEqual(elapsed, 0.0)
             last_elapsed = elapsed
Example #5
0
    def test_find_matching_connections_attempts_multiple_times(self):
        bus = ProxyObjectTests.FMCTest()
        connection_matcher = Mock(return_value=False)

        with sleep.mocked():
            _s._find_matching_connections(bus, connection_matcher)

        connection_matcher.assert_called_with((bus, "conn1"))
        self.assertEqual(connection_matcher.call_count, 11)
Example #6
0
    def test_snapshot_exits_after_first_success(self):
        get_snapshot = Mock()
        get_snapshot.side_effect = [['foo'], []]

        with sleep.mocked() as mock_sleep:
            _compare_system_with_process_snapshot(
                get_snapshot,
                []
            )
            self.assertEqual(1.0, mock_sleep.total_time_slept())
Example #7
0
 def test_snapshot_raises_AssertionError_with_new_apps_opened(self):
     with sleep.mocked():
         fn = lambda: _compare_system_with_process_snapshot(
             lambda: ['foo'],
             []
         )
         self.assertThat(fn, raises(AssertionError(
             "The following apps were started during the test and "
             "not closed: ['foo']"
         )))
Example #8
0
 def test_bad_snapshot_waits_10_seconds(self):
     with sleep.mocked() as mock_sleep:
         try:
             _compare_system_with_process_snapshot(
                 lambda: ['foo'],
                 []
             )
         except:
             pass
         finally:
             self.assertEqual(10.0, mock_sleep.total_time_slept())
    def test_kill_process_tries_again(self, patched_kill_pid):
        with sleep.mocked():
            mock_process = Mock()
            mock_process.pid = 123
            mock_process.communicate.return_value = (
                "",
                "",
            )

            with patch.object(_l, '_is_process_running',
                              return_value=True) as proc_running:
                _kill_process(mock_process)

                self.assertThat(proc_running.call_count, GreaterThan(1))
                self.assertThat(patched_kill_pid.call_count, Equals(2))
                patched_kill_pid.assert_called_with(123, signal.SIGKILL)
Example #10
0
 def test_no_delay_if_time_jumps_since_last_event(self):
     event_delayer = EventDelay()
     with sleep.mocked() as mocked_sleep:
         event_delayer.delay(duration=2, current_time=lambda: 100)
         event_delayer.delay(duration=2, current_time=lambda: 110)
         self.assertThat(mocked_sleep.total_time_slept(), Equals(0.0))
Example #11
0
 def test_long_sleeps_for_correct_time(self):
     with sleep.mocked() as mocked_sleep:
         for _ in Timeout.long():
             pass
         self.assertEqual(30.0, mocked_sleep.total_time_slept())
Example #12
0
 def test_medium_sleeps_for_correct_time(self):
     with sleep.mocked() as mocked_sleep:
         for _ in Timeout.default():
             pass
         self.assertEqual(10.0, mocked_sleep.total_time_slept())
Example #13
0
    def test_snapshot_returns_when_no_apps_running(self):
        with sleep.mocked() as mock_sleep:
            _compare_system_with_process_snapshot(lambda: [], [])

            self.assertEqual(0.0, mock_sleep.total_time_slept())
Example #14
0
 def test_total_time_slept_starts_at_zero(self):
     with sleep.mocked() as sleep_counter:
         self.assertThat(sleep_counter.total_time_slept(), Equals(0.0))
Example #15
0
 def test_mocked_sleep_contextmanager(self):
     with ElapsedTimeCounter() as time_counter:
         with sleep.mocked():
             sleep(10)
         self.assertThat(time_counter.elapsed_time, LessThan(2))
Example #16
0
 def test_sleep_delta_calculator_returns_non_zero_if_delta_not_zero(self):
     with sleep.mocked():
         result = _sleep_for_calculated_delta(101, 100, 2)
         self.assertThat(result, Equals(1.0))
Example #17
0
 def test_sleep_delta_calculator_doesnt_sleep_if_time_delta_zero(self):
     with sleep.mocked() as mocked_sleep:
         _sleep_for_calculated_delta(100, 98, 2)
         self.assertThat(mocked_sleep.total_time_slept(), Equals(0.0))
Example #18
0
 def test_no_delay_if_given_delay_time_negative(self):
     event_delayer = EventDelay()
     with sleep.mocked() as mocked_sleep:
         event_delayer.delay(duration=-2, current_time=lambda: 100)
         event_delayer.delay(duration=-2, current_time=lambda: 101)
         self.assertThat(mocked_sleep.total_time_slept(), Equals(0.0))
Example #19
0
 def test_second_call_to_delay_causes_sleep(self):
     event_delayer = EventDelay()
     with sleep.mocked() as mocked_sleep:
         event_delayer.delay(duration=0, current_time=lambda: 100)
         event_delayer.delay(duration=10, current_time=lambda: 105)
         self.assertThat(mocked_sleep.total_time_slept(), Equals(5.0))
Example #20
0
 def test_first_call_to_delay_causes_no_sleep(self):
     event_delayer = EventDelay()
     with sleep.mocked() as mocked_sleep:
         event_delayer.delay(duration=0.0)
         self.assertThat(mocked_sleep.total_time_slept(), Equals(0.0))