diff --git a/examples/layer_2/threaded/network_down_detectors_no_runner.py b/examples/layer_2/threaded/network_down_detectors_no_runner.py index 0d197fc65..1d84b076c 100644 --- a/examples/layer_2/threaded/network_down_detectors_no_runner.py +++ b/examples/layer_2/threaded/network_down_detectors_no_runner.py @@ -63,8 +63,8 @@ def ping_observing_task(ext_io_connection, ping_ip): with ext_io_connection.open(): observing_timeout = 10 - start_time = time.time() - while time.time() < start_time + observing_timeout: + start_time = time.monotonic() + while time.monotonic() < start_time + observing_timeout: # anytime new data comes it may change status of observer if not net_drop_found and net_down_detector.done(): net_drop_found = True diff --git a/examples/network_toggle_observers.py b/examples/network_toggle_observers.py index 2b01e02d8..87eaf6028 100644 --- a/examples/network_toggle_observers.py +++ b/examples/network_toggle_observers.py @@ -31,7 +31,7 @@ def data_received(self, data, recv_time): """Awaiting ping output change""" if not self.done(): if self.detect_pattern in data: - when_detected = time.time() + when_detected = time.monotonic() self.logger.debug("Network {} {}!".format(self.net_ip, self.detected_status)) self.set_result(result=when_detected) diff --git a/moler/asyncio_runner.py b/moler/asyncio_runner.py index 9fc7cd2a5..b6121a556 100644 --- a/moler/asyncio_runner.py +++ b/moler/asyncio_runner.py @@ -344,7 +344,7 @@ def wait_for(self, connection_observer, connection_observer_future, timeout=None max_timeout = timeout observer_timeout = connection_observer.timeout # we count timeout from now if timeout is given; else we use .life_status.start_time and .timeout of observer - start_time = time.time() if max_timeout else connection_observer.life_status.start_time + start_time = time.monotonic() if max_timeout else connection_observer.life_status.start_time await_timeout = max_timeout if max_timeout else observer_timeout if max_timeout: remain_time, msg = his_remaining_time("await max.", timeout=max_timeout, from_start_time=start_time) @@ -393,7 +393,7 @@ def _cancel_submitted_future(self, connection_observer, connection_observer_futu del self._submitted_futures[id(future)] def _wait_for_time_out(self, connection_observer, connection_observer_future, timeout): - passed = time.time() - connection_observer.life_status.start_time + passed = time.monotonic() - connection_observer.life_status.start_time future = connection_observer_future or connection_observer._future if future: with future.observer_lock: @@ -593,7 +593,7 @@ async def feed(self, connection_observer, subscribed_data_receiver, observer_loc if connection_observer.done(): self.logger.debug("done {}".format(connection_observer)) break - run_duration = time.time() - start_time + run_duration = time.monotonic() - start_time # we need to check connection_observer.timeout at each round since timeout may change # during lifetime of connection_observer if (connection_observer.timeout is not None) and (run_duration >= connection_observer.timeout): @@ -742,7 +742,7 @@ def wait_for(self, connection_observer, connection_observer_future, timeout=None self.logger.debug("go foreground: {!r} - await max. {} [sec]".format(connection_observer, timeout)) if connection_observer.done(): # may happen when failed to start observer feeder return None - start_time = time.time() + start_time = time.monotonic() async def wait_for_connection_observer_done(): result_of_future = await connection_observer_future # feed() always returns None @@ -784,7 +784,7 @@ async def conn_observer_fut_cancel(): thread4async.start_async_coroutine(conn_observer_fut_cancel()) # handle timeout - passed = time.time() - start_time + passed = time.monotonic() - start_time fired_timeout = timeout if timeout else connection_observer.timeout time_out_observer(connection_observer=connection_observer, timeout=fired_timeout, passed_time=passed, @@ -916,7 +916,7 @@ async def _await_stop_loop(self, loop_started, stop_event): def run_async_coroutine(self, coroutine_to_run, timeout): """Start coroutine in dedicated thread and await its result with timeout""" - start_time = time.time() + start_time = time.monotonic() coro_future = self.start_async_coroutine(coroutine_to_run) # run_coroutine_threadsafe returns future as concurrent.futures.Future() and not asyncio.Future # so, we can await it with timeout inside current thread @@ -925,7 +925,7 @@ def run_async_coroutine(self, coroutine_to_run, timeout): self.logger.debug("scheduled {} returned {}".format(coroutine_to_run, coro_result)) return coro_result except concurrent.futures.TimeoutError: - passed = time.time() - start_time + passed = time.monotonic() - start_time raise MolerTimeout(timeout=timeout, kind="run_async_coroutine({})".format(coroutine_to_run), passed_time=passed) diff --git a/moler/cmd/unix/tail_latest_file.py b/moler/cmd/unix/tail_latest_file.py index e43dfcfd3..520ccc028 100644 --- a/moler/cmd/unix/tail_latest_file.py +++ b/moler/cmd/unix/tail_latest_file.py @@ -86,7 +86,7 @@ def on_new_line(self, line, is_full_line): """ if is_full_line: if not self._first_line_time: - self._first_line_time = time.time() + self._first_line_time = time.monotonic() super(TailLatestFile, self).on_new_line(line=line, is_full_line=is_full_line) def is_failure_indication(self, line, is_full_line): @@ -98,7 +98,7 @@ def is_failure_indication(self, line, is_full_line): :return: False if line does not match regex with failure, True if line matches the failure regex. """ if self._check_failure_indication: - if time.time() - self._first_line_time < self.time_for_failure: + if time.monotonic() - self._first_line_time < self.time_for_failure: return self._regex_helper.search_compiled(self.re_fail, line) is not None else: self._check_failure_indication = False # do not check time for future output. It's too late already. diff --git a/moler/command_scheduler.py b/moler/command_scheduler.py index 447f0c3ec..451a455c3 100644 --- a/moler/command_scheduler.py +++ b/moler/command_scheduler.py @@ -106,7 +106,7 @@ def _add_command_to_connection(self, cmd, wait_for_slot=True): cmd.set_exception(CommandTimeout(cmd, timeout=cmd.timeout, kind="scheduler.await_done", - passed_time=time.time() - start_time)) + passed_time=time.monotonic() - start_time)) cmd.set_end_of_life() self._remove_command(cmd=cmd) return False @@ -144,9 +144,9 @@ def _wait_for_slot_for_command(self, cmd): """ connection = cmd.connection lock = self._lock_for_connection(connection) - start_time = time.time() + start_time = time.monotonic() conn_atr = self._locks[connection] - while cmd.timeout >= (time.time() - start_time): + while cmd.timeout >= (time.monotonic() - start_time): time.sleep(0.005) with lock: if conn_atr['current_cmd'] is None and len(conn_atr['queue']) >= 1 and cmd == conn_atr['queue'][0]: diff --git a/moler/connection_observer.py b/moler/connection_observer.py index f88a632ce..7887447cd 100644 --- a/moler/connection_observer.py +++ b/moler/connection_observer.py @@ -159,7 +159,7 @@ def start(self, timeout: Optional[float] = None, *args, **kwargs): # That is so, since observer lifetime starts with it's timeout-clock # and timeout is counted from calling observer.start() self.life_status._is_running = True - self.life_status.start_time = time.time() + self.life_status.start_time = time.monotonic() # Besides not started parallelism machinery causing start-delay # we can have start-delay caused by commands queue on connection # (can't submit command to background-run till previous stops running) diff --git a/moler/device/proxy_pc2.py b/moler/device/proxy_pc2.py index 5d9a8ed95..aa50bc5d1 100644 --- a/moler/device/proxy_pc2.py +++ b/moler/device/proxy_pc2.py @@ -461,7 +461,7 @@ def _detect_prompt_get_cmd(self): for try_nr in range(1, 10, 1): if self._after_open_prompt_detector is None or self._after_open_prompt_detector.running() is False: self._detect_after_open_prompt(self._set_after_open_prompt) - while time.time() - self._after_open_prompt_detector.life_status.start_time < self._prompt_detector_timeout: + while time.monotonic() - self._after_open_prompt_detector.life_status.start_time < self._prompt_detector_timeout: time.sleep(0.1) if self._prompt_detected is True: break diff --git a/moler/device/textualdevice.py b/moler/device/textualdevice.py index e115b3184..03c8792cc 100644 --- a/moler/device/textualdevice.py +++ b/moler/device/textualdevice.py @@ -449,13 +449,13 @@ def await_goto_state(self, timeout=10): :return: None :raise DeviceChangeStateFailure: if the goto_state chain is not empty and timeout occurs. """ - start_time = time.time() - while time.time() - start_time <= timeout: + start_time = time.monotonic() + while time.monotonic() - start_time <= timeout: if self._queue_states.empty() and self._thread_for_goto_state is None: return raise DeviceChangeStateFailure(device=self.__class__.__name__, exception="After {} seconds there are still states to go: '{}' and/or thread to" - " change state".format(time.time() - start_time, self._queue_states, + " change state".format(time.monotonic() - start_time, self._queue_states, self._thread_for_goto_state), device_name=self.name) @@ -519,7 +519,7 @@ def _goto_state_to_run_in_try(self, dest_state, keep_state, timeout, rerun, send is_dest_state = False is_timeout = False - start_time = time.time() + start_time = time.monotonic() final_timeout = timeout_multiply * timeout next_stage_timeout = final_timeout @@ -534,7 +534,7 @@ def _goto_state_to_run_in_try(self, dest_state, keep_state, timeout, rerun, send is_dest_state = True if timeout > 0: - next_stage_timeout = final_timeout - (time.time() - start_time) + next_stage_timeout = final_timeout - (time.monotonic() - start_time) if next_stage_timeout <= 0: is_timeout = True if keep_state: diff --git a/moler/event_awaiter.py b/moler/event_awaiter.py index 4fb11912b..ba3d09916 100644 --- a/moler/event_awaiter.py +++ b/moler/event_awaiter.py @@ -29,7 +29,7 @@ def wait_for_all(cls, timeout, events, interval=0.001): :return: True if all events are done, False otherwise. """ grand_timeout = timeout - start_time = time.time() + start_time = time.monotonic() all_done = True while timeout >= 0: time.sleep(interval) @@ -40,7 +40,7 @@ def wait_for_all(cls, timeout, events, interval=0.001): break if all_done: break - timeout = grand_timeout - (time.time() - start_time) + timeout = grand_timeout - (time.monotonic() - start_time) return all_done @classmethod @@ -54,7 +54,7 @@ def wait_for_any(cls, timeout, events, interval=0.001): :return: True if any event is done, False otherwise. """ grand_timeout = timeout - start_time = time.time() + start_time = time.monotonic() any_done = False while timeout >= 0 and not any_done: time.sleep(interval) @@ -62,7 +62,7 @@ def wait_for_any(cls, timeout, events, interval=0.001): if event.done(): any_done = True break - timeout = grand_timeout - (time.time() - start_time) + timeout = grand_timeout - (time.monotonic() - start_time) return any_done @classmethod diff --git a/moler/io/raw/sshshell.py b/moler/io/raw/sshshell.py index 2b2a31e76..29e2007c5 100644 --- a/moler/io/raw/sshshell.py +++ b/moler/io/raw/sshshell.py @@ -233,7 +233,7 @@ def send(self, data, timeout=1): raise # let any other error be visible def _send(self, data, timeout=1.0): - start_time = time.time() + start_time = time.monotonic() nb_bytes_to_send = len(data) nb_bytes_sent = 0 data2send = data @@ -246,7 +246,7 @@ def _send(self, data, timeout=1.0): nb_bytes_sent = 0 if nb_bytes_sent >= nb_bytes_to_send: break - if time.time() - start_time >= timeout: + if time.monotonic() - start_time >= timeout: send_status = '[{} of {} bytes] {}'.format(nb_bytes_sent, nb_bytes_to_send, data) # don't want to show class name - just ssh address # want same output from any implementation of SshShell-connection diff --git a/moler/moler_connection_for_single_thread_runner.py b/moler/moler_connection_for_single_thread_runner.py index b2e184dc5..757ccc194 100644 --- a/moler/moler_connection_for_single_thread_runner.py +++ b/moler/moler_connection_for_single_thread_runner.py @@ -109,7 +109,7 @@ def notify_observers(self, data, recv_time): """ super(MolerConnectionForSingleThreadRunner, self).notify_observers(data=data, recv_time=recv_time) for connection_observer in self._connection_observers: - connection_observer.life_status.last_feed_time = time.time() + connection_observer.life_status.last_feed_time = time.monotonic() def _create_observer_wrapper(self, observer_reference, self_for_observer): """ diff --git a/moler/runner.py b/moler/runner.py index 3d69af763..866730e1a 100644 --- a/moler/runner.py +++ b/moler/runner.py @@ -315,7 +315,7 @@ def submit(self, connection_observer): c_future = CancellableFuture(connection_observer_future, observer_lock, stop_feeding, feed_done) - connection_observer.life_status.last_feed_time = time.time() + connection_observer.life_status.last_feed_time = time.monotonic() return c_future def wait_for(self, connection_observer, connection_observer_future, timeout=None): @@ -349,7 +349,7 @@ def wait_for(self, connection_observer, connection_observer_future, timeout=None max_timeout = timeout observer_timeout = connection_observer.timeout # we count timeout from now if timeout is given; else we use .life.status.start_time and .timeout of observer - start_time = time.time() if max_timeout else connection_observer.life_status.start_time + start_time = time.monotonic() if max_timeout else connection_observer.life_status.start_time await_timeout = max_timeout if max_timeout else observer_timeout if max_timeout: remain_time, msg = his_remaining_time("await max.", timeout=max_timeout, from_start_time=start_time) @@ -397,7 +397,7 @@ def _execute_till_eol(self, connection_observer, connection_observer_future, max if (future in done) or connection_observer.done(): self._cancel_submitted_future(connection_observer, future) return True - already_passed = time.time() - connection_observer.life_status.start_time + already_passed = time.monotonic() - connection_observer.life_status.start_time eol_timeout = connection_observer.timeout + connection_observer.life_status.terminating_timeout eol_remain_time = eol_timeout - already_passed timeout = connection_observer.timeout @@ -414,10 +414,10 @@ def _execute_till_eol(self, connection_observer, connection_observer_future, max def _wait_for_not_started_connection_observer_is_done(self, connection_observer): # Have to wait till connection_observer is done with terminaing timeout. eol_remain_time = connection_observer.life_status.terminating_timeout - start_time = time.time() + start_time = time.monotonic() while not connection_observer.done() and eol_remain_time > 0.0: time.sleep(self._tick) - eol_remain_time = start_time + connection_observer.life_status.terminating_timeout - time.time() + eol_remain_time = start_time + connection_observer.life_status.terminating_timeout - time.monotonic() def _end_of_life_of_future_and_connection_observer(self, connection_observer, connection_observer_future): future = connection_observer_future or connection_observer._future @@ -432,7 +432,7 @@ def _cancel_submitted_future(connection_observer, connection_observer_future): future.cancel(no_wait=True) def _wait_for_time_out(self, connection_observer, connection_observer_future, timeout): - passed = time.time() - connection_observer.life_status.start_time + passed = time.monotonic() - connection_observer.life_status.start_time future = connection_observer_future or connection_observer._future if future: self.logger.debug(">>> Entering {}. conn-obs '{}' runner '{}' future '{}'".format(future.observer_lock, connection_observer, self, future)) @@ -478,7 +478,7 @@ def secure_data_received(data, timestamp): with observer_lock: self.logger.debug(u">>> Entered {}. conn-obs '{}' runner '{}' data '{}'".format(observer_lock, connection_observer, self, data)) connection_observer.data_received(data, timestamp) - connection_observer.life_status.last_feed_time = time.time() + connection_observer.life_status.last_feed_time = time.monotonic() self.logger.debug(u">>> Exited {}. conn-obs '{}' runner '{}' data '{}'".format(observer_lock, connection_observer, self, data)) except Exception as exc: # TODO: handling stacktrace @@ -583,7 +583,7 @@ def _feed_loop(self, connection_observer, stop_feeding, observer_lock): if connection_observer.done(): self.logger.debug("done {}".format(connection_observer)) break - current_time = time.time() + current_time = time.monotonic() run_duration = current_time - start_time # we need to check connection_observer.timeout at each round since timeout may change # during lifetime of connection_observer @@ -605,7 +605,7 @@ def _feed_loop(self, connection_observer, stop_feeding, observer_lock): passed_time=run_duration, runner_logger=self.logger) if connection_observer.life_status.terminating_timeout >= 0.0: - start_time = time.time() + start_time = time.monotonic() connection_observer.life_status.in_terminating = True else: break @@ -649,7 +649,7 @@ def his_remaining_time(prefix, timeout, from_start_time): :param from_start_time: start of lifetime for the object :return: remaining time as float and related description message """ - already_passed = time.time() - from_start_time + already_passed = time.monotonic() - from_start_time remain_time = timeout - already_passed if remain_time < 0.0: remain_time = 0.0 @@ -668,7 +668,7 @@ def await_future_or_eol(connection_observer, remain_time, start_time, timeout, l logger.debug("{} is done before creating future".format(connection_observer)) end_of_life = True break - now = time.time() + now = time.monotonic() already_passed = now - start_time remain_time = timeout - already_passed observer_lifetime_passed = now - connection_observer.life_status.start_time diff --git a/moler/runner_single_thread.py b/moler/runner_single_thread.py index 193305f59..c77197da4 100644 --- a/moler/runner_single_thread.py +++ b/moler/runner_single_thread.py @@ -76,7 +76,7 @@ def wait_for(self, connection_observer, connection_observer_future, timeout=10.0 observer_timeout = connection_observer.timeout # we count timeout from now if timeout is given; else we use .life.status.start_time and .timeout of # observer - current_time = time.time() + current_time = time.monotonic() start_time = current_time if max_timeout else connection_observer.life_status.start_time await_timeout = max_timeout if max_timeout else observer_timeout if max_timeout: @@ -162,7 +162,7 @@ def _add_connection_observer(self, connection_observer): connection_observer._log(logging.INFO, "{} started, {}".format(connection_observer.get_long_desc(), msg)) self._start_command(connection_observer=connection_observer) - connection_observer.life_status.last_feed_time = time.time() + connection_observer.life_status.last_feed_time = time.monotonic() @classmethod def _its_remaining_time(cls, prefix, timeout, from_start_time): @@ -174,7 +174,7 @@ def _its_remaining_time(cls, prefix, timeout, from_start_time): :param from_start_time: start of lifetime for the object :return: remaining time as float and related description message """ - already_passed = time.time() - from_start_time + already_passed = time.monotonic() - from_start_time remain_time = timeout - already_passed if remain_time < 0.0: remain_time = 0.0 @@ -231,7 +231,7 @@ def _wait_till_done(self, connection_observer, timeout, check_timeout): timeout_add = 0.1 term_timeout = 0 if connection_observer.life_status.terminating_timeout is None else \ connection_observer.life_status.terminating_timeout - remain_time = timeout - (time.time() - connection_observer.life_status.start_time) + term_timeout + timeout_add + remain_time = timeout - (time.monotonic() - connection_observer.life_status.start_time) + term_timeout + timeout_add while remain_time >= 0: if connection_observer.done(): return True @@ -240,7 +240,7 @@ def _wait_till_done(self, connection_observer, timeout, check_timeout): timeout = connection_observer.timeout term_timeout = 0 if connection_observer.life_status.terminating_timeout is None else \ connection_observer.life_status.terminating_timeout - remain_time = timeout - (time.time() - connection_observer.life_status.start_time) + term_timeout + \ + remain_time = timeout - (time.monotonic() - connection_observer.life_status.start_time) + term_timeout + \ timeout_add return False @@ -252,10 +252,10 @@ def _wait_for_not_started_connection_observer_is_done(self, connection_observer) """ # Have to wait till connection_observer is done with terminaing timeout. eol_remain_time = connection_observer.life_status.terminating_timeout - start_time = time.time() + start_time = time.monotonic() while not connection_observer.done() and eol_remain_time > 0.0: time.sleep(self._tick) - eol_remain_time = start_time + connection_observer.life_status.terminating_timeout - time.time() + eol_remain_time = start_time + connection_observer.life_status.terminating_timeout - time.monotonic() def _runner_loop(self): """ @@ -277,7 +277,7 @@ def _check_last_feed_connection_observers(self): Call on_inactivity on connection_observer if needed. :return: None """ - current_time = time.time() + current_time = time.monotonic() for connection_observer in self._copy_of_connections_observers: life_status = connection_observer.life_status if (life_status.inactivity_timeout > 0.0) and (life_status.last_feed_time is not None): @@ -299,7 +299,7 @@ def _check_timeout_connection_observers(self): """ for connection_observer in self._copy_of_connections_observers: start_time = connection_observer.life_status.start_time - current_time = time.time() + current_time = time.monotonic() run_duration = current_time - start_time timeout = connection_observer.timeout if connection_observer.life_status.in_terminating: @@ -315,7 +315,7 @@ def _check_timeout_connection_observers(self): timeout=connection_observer.timeout, passed_time=run_duration, runner_logger=self.logger) if connection_observer.life_status.terminating_timeout > 0.0: - connection_observer.life_status.start_time = time.time() + connection_observer.life_status.start_time = time.monotonic() connection_observer.life_status.in_terminating = True else: connection_observer.set_end_of_life() @@ -327,7 +327,7 @@ def _prepare_for_time_out(self, connection_observer, timeout): :param timeout: timeout in seconds. :return: None """ - passed = time.time() - connection_observer.life_status.start_time + passed = time.monotonic() - connection_observer.life_status.start_time self._timeout_observer(connection_observer=connection_observer, timeout=timeout, passed_time=passed, runner_logger=self.logger, kind="await_done") diff --git a/moler/util/cmds_events_doc.py b/moler/util/cmds_events_doc.py index 2042670cd..2ebb64a14 100755 --- a/moler/util/cmds_events_doc.py +++ b/moler/util/cmds_events_doc.py @@ -259,7 +259,7 @@ def check_if_documentation_exists(path2cmds): number_of_command_found = 0 for moler_module, moler_class in _walk_moler_nonabstract_commands(path=path2cmds, base_class=base_class): number_of_command_found += 1 - start_time = time.time() + start_time = time.monotonic() print("processing: '{}'...".format(moler_class), end="") test_data = _retrieve_command_documentation(moler_module, observer_type) @@ -298,7 +298,7 @@ def check_if_documentation_exists(path2cmds): if error_msg: wrong_commands[moler_class.__name__] = 1 errors_found.append(error_msg) - print(" - {:.3f} s.".format(time.time() - start_time)) + print(" - {:.3f} s.".format(time.monotonic() - start_time)) print("") if errors_found: diff --git a/moler/util/devices_SM.py b/moler/util/devices_SM.py index 579faa761..5809006c3 100644 --- a/moler/util/devices_SM.py +++ b/moler/util/devices_SM.py @@ -111,7 +111,7 @@ def _start_device_tests(source_device, tested, states_to_test, max_time, new_dev def _perform_device_tests(device, tested, states_to_test, max_time, rerun, timeout_multiply): device.set_all_prompts_on_line(True) - start_time = time.time() + start_time = time.monotonic() test_nr = 0 while 0 < states_to_test.qsize(): source_state, target_state = states_to_test.get(0) @@ -132,7 +132,7 @@ def _perform_device_tests(device, tested, states_to_test, max_time, rerun, timeo "Cannot trigger change state: '{}' -> '{}'. Successful tests: {}\n{}\nAlready tested '{}'.".format( source_state, target_state, test_nr, exc, tested)) test_nr += 1 - if max_time is not None and time.time() - start_time > max_time: + if max_time is not None and time.monotonic() - start_time > max_time: return diff --git a/moler/util/tracked_thread.py b/moler/util/tracked_thread.py index 4229a3cdb..341096904 100644 --- a/moler/util/tracked_thread.py +++ b/moler/util/tracked_thread.py @@ -35,11 +35,11 @@ def thread_exceptions_catcher(*args, **kwargs): def report_alive(report_tick=5.0): - last_report_time = time.time() + last_report_time = time.monotonic() do_report = True while True: yield do_report # TODO log long loop tick, allowed_loop_tick as param - now = time.time() + now = time.monotonic() delay = now - last_report_time if delay >= report_tick: last_report_time = now diff --git a/test/cmd/at/test_cmd_at_attach.py b/test/cmd/at/test_cmd_at_attach.py index 7a409882f..6ab8acbc2 100644 --- a/test/cmd/at/test_cmd_at_attach.py +++ b/test/cmd/at/test_cmd_at_attach.py @@ -25,10 +25,10 @@ def test_calling_at_cmd_attach_timeouts_after_500ms(buffer_connection): **attach.COMMAND_KWARGS_ver_execute) at_cmd_attach.timeout = 0.5 buffer_connection.remote_inject_response(["AT+CGATT=1\n"]) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): at_cmd_attach() - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration > 0.5 assert duration < 0.7 @@ -40,10 +40,10 @@ def test_calling_at_cmd_attach_timeouts_on_no_output(buffer_connection): at_cmd_attach = attach.Attach(connection=buffer_connection.moler_connection, **attach.COMMAND_KWARGS_ver_execute) at_cmd_attach.timeout = 0.5 - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): at_cmd_attach() - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration > 0.5 assert duration < 0.7 diff --git a/test/cmd/at/test_cmd_plink_serial.py b/test/cmd/at/test_cmd_plink_serial.py index 18823ed8e..f098e68eb 100644 --- a/test/cmd/at/test_cmd_plink_serial.py +++ b/test/cmd/at/test_cmd_plink_serial.py @@ -32,10 +32,10 @@ def test_command_quickly_fails_on_error(buffer_connection, command_output_from_f run = plink_serial.PlinkSerial(connection=buffer_connection.moler_connection, prompt="user@host", serial_devname="COM5") buffer_connection.remote_inject_response([command_output_from_failed_command]) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandFailure): run() - assert (time.time() - start_time) < 0.8 + assert (time.monotonic() - start_time) < 0.8 no_plink = """ diff --git a/test/cmd/unix/test_cmd_run_serial_proxy.py b/test/cmd/unix/test_cmd_run_serial_proxy.py index fda88a498..60cc369db 100644 --- a/test/cmd/unix/test_cmd_run_serial_proxy.py +++ b/test/cmd/unix/test_cmd_run_serial_proxy.py @@ -33,10 +33,10 @@ def test_command_quickly_fails_on_error_in_proxy(buffer_connection, command_outp run = run_serial_proxy.RunSerialProxy(connection=buffer_connection.moler_connection, prompt="image9|>>>", serial_devname="COM5") buffer_connection.remote_inject_response([command_output_from_failed_python_code]) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandFailure): run() - assert (time.time() - start_time) < 0.8 + assert (time.monotonic() - start_time) < 0.8 def test_command_exits_python_interactive_shell(buffer_connection): diff --git a/test/cmd/unix/test_cmd_sudo.py b/test/cmd/unix/test_cmd_sudo.py index a1e2c7cdf..92d65079b 100644 --- a/test/cmd/unix/test_cmd_sudo.py +++ b/test/cmd/unix/test_cmd_sudo.py @@ -113,10 +113,10 @@ def test_can_use_timeout_of_embedded_command(buffer_connection, command_output_a cmd_pwd.timeout = 0.2 cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="pass", cmd_object=cmd_pwd) cmd_sudo.terminating_timeout = 0 # no additional timeout for Ctrl-C..till..prompt (shutdown after cmd timeout) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): cmd_sudo() - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration >= 0.2 assert duration < 0.5 @@ -129,10 +129,10 @@ def test_can_ignore_timeout_of_embedded_command_if_direct_timeout_provided(buffe cmd_pwd.timeout = 0.8 cmd_sudo = Sudo(connection=buffer_connection.moler_connection, password="pass", cmd_object=cmd_pwd) cmd_sudo.terminating_timeout = 0 # no additional sudo timeout for Ctrl-C..till..prompt (shutdown after cmd timeout) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): cmd_sudo(timeout=0.5) - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration >= 0.5 assert duration < 0.8 diff --git a/test/cmd/unix/test_cmd_uptime.py b/test/cmd/unix/test_cmd_uptime.py index bcdd8112f..247dd8b8d 100644 --- a/test/cmd/unix/test_cmd_uptime.py +++ b/test/cmd/unix/test_cmd_uptime.py @@ -37,10 +37,10 @@ def test_calling_uptime_timeout_with_long_timeout(buffer_connection): uptime_cmd.terminating_timeout = 0.2 uptime_cmd.start(timeout=long_timeout) uptime_cmd.timeout = long_timeout - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): uptime_cmd.await_done(timeout=1) - end_time = time.time() + end_time = time.monotonic() duration = end_time - start_time assert duration < long_timeout/10 @@ -50,10 +50,10 @@ def test_calling_uptime_timeout_with_short_timeout(buffer_connection): short_timeout = 1 uptime_cmd.terminating_timeout = 0.2 uptime_cmd.start(timeout=short_timeout) - start_time = time.time() + start_time = time.monotonic() with pytest.raises(CommandTimeout): uptime_cmd.await_done() - end_time = time.time() + end_time = time.monotonic() duration = end_time - start_time assert duration < short_timeout + 1 assert duration >= short_timeout diff --git a/test/device/test_SM_unix_remote.py b/test/device/test_SM_unix_remote.py index 5a405bbbf..b1c8a6efd 100644 --- a/test/device/test_SM_unix_remote.py +++ b/test/device/test_SM_unix_remote.py @@ -58,19 +58,19 @@ def test_unix_remote_proxy_pc_device_goto_state_bg(device_connection, unix_remot src_state = "UNIX_LOCAL" unix_remote_proxy_pc.goto_state(state=src_state) assert unix_remote_proxy_pc.current_state == src_state - start_time = time.time() + start_time = time.monotonic() unix_remote_proxy_pc.goto_state_bg(state=dst_state) assert unix_remote_proxy_pc.current_state != dst_state - while dst_state != unix_remote_proxy_pc.current_state and (time.time() - start_time) < 10: + while dst_state != unix_remote_proxy_pc.current_state and (time.monotonic() - start_time) < 10: MolerTest.sleep(0.01) - execution_time_bg = time.time() - start_time + execution_time_bg = time.monotonic() - start_time assert unix_remote_proxy_pc.current_state == dst_state unix_remote_proxy_pc.goto_state(state=src_state) assert unix_remote_proxy_pc.current_state == src_state - start_time = time.time() + start_time = time.monotonic() unix_remote_proxy_pc.goto_state(state=dst_state) - execution_time_fg = time.time() - start_time + execution_time_fg = time.monotonic() - start_time assert unix_remote_proxy_pc.current_state == dst_state time_diff = abs(execution_time_bg - execution_time_fg) assert time_diff < min(execution_time_fg, execution_time_bg) / 2 diff --git a/test/events/unix/test_event_ping_no_response.py b/test/events/unix/test_event_ping_no_response.py index 7c73ed051..8bfc12564 100644 --- a/test/events/unix/test_event_ping_no_response.py +++ b/test/events/unix/test_event_ping_no_response.py @@ -26,8 +26,8 @@ def callback_fun(param): assert 0 == counter['nr'] event.start() buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now()) - start_time = time.time() - while time.time() - start_time <= max_timeout: + start_time = time.monotonic() + while time.monotonic() - start_time <= max_timeout: if 1 == counter['nr']: break MolerTest.sleep(sleep_time) @@ -39,8 +39,8 @@ def callback_fun(param): event.resume() buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now()) event.await_done() - start_time = time.time() - while time.time() - start_time <= max_timeout: + start_time = time.monotonic() + while time.monotonic() - start_time <= max_timeout: if 2 == counter['nr']: break MolerTest.sleep(sleep_time) @@ -76,9 +76,9 @@ def feed_in_separate_thread(): tf = threading.Thread(target=feed_in_separate_thread) tf.start() - start_time = time.time() + start_time = time.monotonic() - while (time.time() - start_time < 4) or (processed['process'] < 300): + while (time.monotonic() - start_time < 4) or (processed['process'] < 300): event.pause() MolerTest.sleep(sleep_time) event.resume() diff --git a/test/integration/py3test_runners.py b/test/integration/py3test_runners.py index a9ed5da4f..8b89eec06 100644 --- a/test/integration/py3test_runners.py +++ b/test/integration/py3test_runners.py @@ -52,9 +52,9 @@ async def test_observer_gets_all_data_of_connection_after_it_is_submitted_to_bac moler_conn = ThreadedMolerConnection() net_down_detector = NetworkDownDetector(connection=moler_conn, runner=observer_runner) connection = net_down_detector.connection - start_time = net_down_detector.life_status.start_time = time.time() + start_time = net_down_detector.life_status.start_time = time.monotonic() observer_runner.submit(net_down_detector) - durations.append(time.time() - start_time) + durations.append(time.monotonic() - start_time) connection.data_received("61 bytes", datetime.datetime.now()) connection.data_received("62 bytes", datetime.datetime.now()) @@ -81,9 +81,9 @@ def test_runner_secures_observer_against_additional_data_after_observer_is_done( for n in range(20): # need to test multiple times to ensure there are no thread races moler_conn = ThreadedMolerConnection() net_down_detector = NetworkDownDetector(connection=moler_conn, runner=observer_runner) - net_down_detector.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + net_down_detector.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() connection = net_down_detector.connection - net_down_detector.life_status.start_time = time.time() + net_down_detector.life_status.start_time = time.monotonic() observer_runner.submit(net_down_detector) connection.data_received("61 bytes", datetime.datetime.now()) @@ -105,8 +105,8 @@ def test_runner_secures_observer_against_additional_data_after_runner_shutdown(o # check if shutdown stops all observers running inside given runner net_down_detector1 = NetworkDownDetector(connection=moler_conn, runner=observer_runner) net_down_detector2 = NetworkDownDetector(connection=moler_conn, runner=observer_runner) - net_down_detector1.life_status.start_time = time.time() # must start observer lifetime before runner.submit() - net_down_detector2.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + net_down_detector1.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() + net_down_detector2.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() connection = moler_conn observer_runner.submit(net_down_detector1) observer_runner.submit(net_down_detector2) @@ -129,8 +129,8 @@ async def test_runner_unsubscribes_from_connection_after_runner_shutdown(observe # check if shutdown unsubscribes all observers running inside given runner net_down_detector1 = NetworkDownDetector(connection=moler_conn, runner=observer_runner) net_down_detector2 = NetworkDownDetector(connection=moler_conn, runner=observer_runner) - net_down_detector1.life_status.start_time = time.time() # must start observer lifetime before runner.submit() - net_down_detector2.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + net_down_detector1.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() + net_down_detector2.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() assert len(moler_conn._observers) == 0 observer_runner.submit(net_down_detector1) observer_runner.submit(net_down_detector2) @@ -152,7 +152,7 @@ async def test_runner_doesnt_break_on_exception_raised_inside_observer(observer_ fail_by_raising=Exception("unknown format"), runner=observer_runner) as conn_observer: connection = conn_observer.connection - conn_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + conn_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(conn_observer) connection.data_received("61 bytes", datetime.datetime.now()) @@ -181,7 +181,7 @@ async def test_runner_sets_observer_exception_result_for_exception_raised_inside fail_by_raising=unknown_format_exception, runner=observer_runner) as conn_observer: connection = conn_observer.connection - conn_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + conn_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(conn_observer) connection.data_received("61 bytes", datetime.datetime.now()) @@ -203,7 +203,7 @@ async def test_future_is_not_exception_broken_when_observer_is_exception_broken( fail_by_raising=Exception("unknown format"), runner=observer_runner) as conn_observer: connection = conn_observer.connection - conn_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + conn_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(conn_observer) connection.data_received("61 bytes", datetime.datetime.now()) @@ -221,7 +221,7 @@ async def test_future_doesnt_return_result_of_observer(net_down_detector): observer_runner = net_down_detector.runner connection = net_down_detector.connection - net_down_detector.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + net_down_detector.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(net_down_detector) connection.data_received("61 bytes", datetime.datetime.now()) @@ -250,7 +250,7 @@ async def test_future_timeouts_after_timeout_of_observer(connection_observer): observer_runner = connection_observer.runner connection_observer.timeout = 0.1 - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) with pytest.raises(ResultNotAvailableYet): connection_observer.result() @@ -273,7 +273,7 @@ async def test_future_accommodates_to_extending_timeout_of_observer(connection_o logger = logging.getLogger('moler.runner') observer_runner = connection_observer.runner connection_observer.timeout = 0.2 - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(connection_observer) with pytest.raises(ResultNotAvailableYet): # not timed out yet connection_observer.result() @@ -303,7 +303,7 @@ async def test_future_accommodates_to_shortening_timeout_of_observer(connection_ observer_runner = connection_observer.runner connection_observer.timeout = 0.2 - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(connection_observer) with pytest.raises(ResultNotAvailableYet): # not timed out yet connection_observer.result() @@ -331,13 +331,13 @@ def test_wait_for__times_out_on_constructor_timeout(connection_observer): observer_runner = connection_observer.runner connection_observer.timeout = 0.2 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) with pytest.raises(MolerTimeout): observer_runner.wait_for(connection_observer, future, timeout=None) # means: use .timeout of observer connection_observer.result() # should raise Timeout - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration >= 0.2 assert duration < 0.25 time.sleep(0.1) # future may be 'not done yet' (just after timeout) - it should be "in exiting of feed" @@ -353,15 +353,15 @@ def test_wait_for__times_out_on_specified_timeout(connection_observer): observer_runner = connection_observer.runner connection_observer.timeout = 1.5 connection_observer.terminating_timeout = 0.0 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) time.sleep(0.1) with pytest.raises(MolerTimeout): - wait4_start_time = time.time() # wait_for() timeout is counted from wait_for() line in code + wait4_start_time = time.monotonic() # wait_for() timeout is counted from wait_for() line in code observer_runner.wait_for(connection_observer, future, timeout=0.2) # means: use timeout of wait_for (shorter then initial one) connection_observer.result() # should raise Timeout - now = time.time() + now = time.monotonic() observer_life_duration = now - start_time wait4_duration = now - wait4_start_time assert wait4_duration >= 0.2 @@ -376,14 +376,14 @@ def test_wait_for__times_out_on_earlier_timeout(connection_observer): observer_runner = connection_observer.runner connection_observer.timeout = 0.3 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) with pytest.raises(MolerTimeout): - wait4_start_time = time.time() # wait_for() timeout is counted from wait_for() line in code + wait4_start_time = time.monotonic() # wait_for() timeout is counted from wait_for() line in code observer_runner.wait_for(connection_observer, future, timeout=0.5) # means: timeout of wait_for longer then initial one connection_observer.result() # should raise Timeout - now = time.time() + now = time.monotonic() observer_life_duration = now - start_time wait4_duration = now - wait4_start_time assert observer_life_duration >= 0.3 @@ -397,7 +397,7 @@ def test_wait_for__tracks_changes_of_observer_timeout__extension(connection_obse observer_runner = connection_observer.runner connection_observer.timeout = 0.2 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) def modify_observer_timeout(): @@ -409,7 +409,7 @@ def modify_observer_timeout(): observer_runner.wait_for(connection_observer, future, timeout=None) connection_observer.result() # should raise Timeout - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration >= 0.35 assert duration < 0.4 @@ -420,7 +420,7 @@ def test_wait_for__tracks_changes_of_observer_timeout__shortening(connection_obs observer_runner = connection_observer.runner connection_observer.timeout = 0.35 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) def modify_observer_timeout(): @@ -432,7 +432,7 @@ def modify_observer_timeout(): observer_runner.wait_for(connection_observer, future, timeout=None) connection_observer.result() # should raise Timeout - duration = time.time() - start_time + duration = time.monotonic() - start_time assert duration >= 0.2 assert duration < 0.25 @@ -445,7 +445,7 @@ def test_wait_for__direct_timeout_takes_precedence_over_extended_observer_timeou observer_runner = connection_observer.runner connection_observer.timeout = 0.2 connection_observer.terminating_timeout = 0.0 - start_time = connection_observer.life_status.start_time = time.time() + start_time = connection_observer.life_status.start_time = time.monotonic() future = observer_runner.submit(connection_observer) def modify_observer_timeout(): @@ -454,12 +454,12 @@ def modify_observer_timeout(): threading.Thread(target=modify_observer_timeout).start() with pytest.raises(MolerTimeout): - wait4_start_time = time.time() # wait_for() timeout is counted from wait_for() line in code + wait4_start_time = time.monotonic() # wait_for() timeout is counted from wait_for() line in code observer_runner.wait_for(connection_observer, future, timeout=0.25) # should take precedence, means: 0.25 sec from now connection_observer.result() # should raise Timeout - now = time.time() + now = time.monotonic() observer_life_duration = now - start_time wait4_duration = now - wait4_start_time @@ -481,7 +481,7 @@ def test_observer__on_timeout__is_called_once_at_timeout(connection_observer): observer_runner = connection_observer.runner connection_observer.timeout = 0.33 - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) with mock.patch.object(connection_observer, "on_timeout") as timeout_callback: with pytest.raises(MolerTimeout): @@ -497,7 +497,7 @@ def test_runner_shutdown_cancels_remaining_active_feeders_inside_main_thread(asy connection_observer = NetworkDownDetector(connection=ThreadedMolerConnection(), runner=async_runner) - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = async_runner.submit(connection_observer) future._loop.run_until_complete(asyncio.sleep(1.0)) # feeder will start processing inside loop @@ -512,7 +512,7 @@ def test_runner_shutdown_cancels_remaining_inactive_feeders_inside_main_thread(o connection_observer = NetworkDownDetector(connection=ThreadedMolerConnection(), runner=observer_runner) - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) time.sleep(0.2) # won't enter event loop of future - feeder won't start processing @@ -530,7 +530,7 @@ def test_runner_shutdown_cancels_remaining_feeders_inside_threads(observer_runne observers_pool.append(connection_observer) def submit_feeder(connection_observer): - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) while not future.done(): time.sleep(0.1) @@ -561,7 +561,7 @@ def submit_feeder(connection_observer): # observers_pool.append(connection_observer) # # def await_on_timeout(connection_observer): -# connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() +# connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() # future = observer_runner.submit(connection_observer) # with pytest.raises(MolerTimeout): # observer_runner.wait_for(connection_observer, future, timeout=0.33) @@ -589,7 +589,7 @@ def submit_feeder(connection_observer): def test_can_await_connection_observer_to_complete(observer_and_awaited_data): connection_observer, awaited_data = observer_and_awaited_data observer_runner = connection_observer.runner - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) def inject_data(): @@ -619,7 +619,7 @@ def inject_data(): async def test_can_async_await_connection_observer_to_complete(observer_and_awaited_data): connection_observer, awaited_data = observer_and_awaited_data observer_runner = connection_observer.runner - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = observer_runner.submit(connection_observer) connection_observer.timeout = 0.3 @@ -667,7 +667,7 @@ async def test_wait_for__is_prohibited_inside_async_def(async_runner): # Any way to treat wait_for() as awaitable? # connection_observer = NetworkDownDetector(connection=ThreadedMolerConnection(), runner=async_runner) - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() future = async_runner.submit(connection_observer) with pytest.raises(WrongUsage) as err: async_runner.wait_for(connection_observer, future) @@ -773,7 +773,7 @@ def data_received(self, data, recv_time): self.all_data_received.append(data) if not self.done(): if "Network is unreachable" in data: - when_detected = time.time() + when_detected = time.monotonic() self.set_result(result=when_detected) diff --git a/test/integration/test_connection_observer_with_external-io.py b/test/integration/test_connection_observer_with_external-io.py index da185fe83..673186fcf 100644 --- a/test/integration/test_connection_observer_with_external-io.py +++ b/test/integration/test_connection_observer_with_external-io.py @@ -75,7 +75,7 @@ def data_received(self, data, recv_time): """ if not self.done(): if "Network is unreachable" in data: - when_detected = time.time() + when_detected = time.monotonic() self.set_result(result=when_detected) diff --git a/test/integration/test_connection_observer_with_runner.py b/test/integration/test_connection_observer_with_runner.py index 056f7ec9a..923ab6fcc 100644 --- a/test/integration/test_connection_observer_with_runner.py +++ b/test/integration/test_connection_observer_with_runner.py @@ -87,7 +87,7 @@ def data_received(self, data, recv_time): """ if not self.done(): if "Network is unreachable" in data: - when_detected = time.time() + when_detected = time.monotonic() self.set_result(result=when_detected) diff --git a/test/integration/test_io_raw_memory.py b/test/integration/test_io_raw_memory.py index a51c35836..00b04c30b 100644 --- a/test/integration/test_io_raw_memory.py +++ b/test/integration/test_io_raw_memory.py @@ -185,12 +185,12 @@ def test_can_inject_data_with_specified_delay(memory_connection_without_decoder) connection = memory_connection_without_decoder moler_conn = connection.moler_connection received_data = bytearray() - start_time = time.time() + start_time = time.monotonic() def receiver(data, time_recv): received_data.extend(data) if b"msg3" in received_data: - duration = time.time() - start_time + duration = time.monotonic() - start_time assert b'msg1\nmsg2\nmsg3\n' == received_data assert (duration > 0.7) and (duration < 0.8) diff --git a/test/integration/test_io_raw_ssh.py b/test/integration/test_io_raw_ssh.py index ba5591c4d..700fd79bc 100644 --- a/test/integration/test_io_raw_ssh.py +++ b/test/integration/test_io_raw_ssh.py @@ -288,12 +288,12 @@ def test_opening_connection_created_from_existing_one_is_quicker(sshshell_connec full_open_durations = [] reused_conn_open_durations = [] for cnt in range(10): - start1 = time.time() + start1 = time.monotonic() with connection.open(): - end1 = time.time() - start2 = time.time() + end1 = time.monotonic() + start2 = time.monotonic() with new_connection.open(): - end2 = time.time() + end2 = time.monotonic() full_open_durations.append(end1 - start1) reused_conn_open_durations.append(end2 - start2) @@ -560,21 +560,21 @@ def test_active_connection_can_notify_on_establishing_and_closing_connection(act notifications = [] def on_connection_made(connection): - notifications.append(("made", time.time(), connection)) + notifications.append(("made", time.monotonic(), connection)) def on_connection_lost(connection): - notifications.append(("lost", time.time(), connection)) + notifications.append(("lost", time.monotonic(), connection)) connection = active_sshshell_connection connection.notify(callback=on_connection_made, when="connection_made") connection.notify(callback=on_connection_lost, when="connection_lost") - before_open_time = time.time() + before_open_time = time.monotonic() with connection.open(): - after_open_time = time.time() + after_open_time = time.monotonic() time.sleep(0.1) - before_close_time = time.time() + before_close_time = time.monotonic() time.sleep(0.1) - after_close_time = time.time() + after_close_time = time.monotonic() assert len(notifications) == 2 assert "made" == notifications[0][0] @@ -592,7 +592,7 @@ def test_active_connection_can_notify_on_losing_connection(active_sshshell_conne lost_time = [] def on_connection_lost(connection): - lost_time.append(time.time()) + lost_time.append(time.monotonic()) connection = active_sshshell_connection moler_conn = connection.moler_connection @@ -600,9 +600,9 @@ def on_connection_lost(connection): with connection.open(): time.sleep(0.1) moler_conn.send(data="exit\n") - before_exit_from_remote_time = time.time() + before_exit_from_remote_time = time.monotonic() time.sleep(0.5) - before_close_time = time.time() + before_close_time = time.monotonic() assert len(lost_time) == 1 connection_lost_time = lost_time[0] diff --git a/test/integration/test_io_tcp.py b/test/integration/test_io_tcp.py index 62944b9b1..c3d61ad98 100644 --- a/test/integration/test_io_tcp.py +++ b/test/integration/test_io_tcp.py @@ -113,7 +113,7 @@ def connection_closed_handler(): def _wait_for_last_message(tcp_server_pipe, last_message="Client disconnected", timeout=5): - start_time = time.time() + start_time = time.monotonic() dialog_with_server = [] while last_message not in dialog_with_server: @@ -121,7 +121,7 @@ def _wait_for_last_message(tcp_server_pipe, last_message="Client disconnected", time.sleep(0.01) dialog_with_server = tcp_server_pipe.recv() - if time.time() - start_time > timeout: + if time.monotonic() - start_time > timeout: break return dialog_with_server diff --git a/test/integration/test_thread_based_runner.py b/test/integration/test_thread_based_runner.py index 2f6de94e2..1c82691fe 100644 --- a/test/integration/test_thread_based_runner.py +++ b/test/integration/test_thread_based_runner.py @@ -19,7 +19,7 @@ def test_can_submit_connection_observer_into_background(connection_observer, observer_runner): - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() connection_observer_future = observer_runner.submit(connection_observer) # see API of concurrent.futures.Future try: @@ -90,7 +90,7 @@ def data_received(self, data, recv_time): self.all_data_received.append(data) if not self.done(): if "Network is unreachable" in data: - when_detected = time.time() + when_detected = time.monotonic() self.set_result(result=when_detected) diff --git a/test/test_connection_observer.py b/test/test_connection_observer.py index 27d7fcda4..eeeae35ab 100644 --- a/test/test_connection_observer.py +++ b/test/test_connection_observer.py @@ -36,9 +36,9 @@ def test_can_retrieve_connection_observer_start_time(do_nothing_connection_obser connection_to_remote): connection_observer = do_nothing_connection_observer__for_major_base_class connection_observer.connection = connection_to_remote.moler_connection - time1 = time.time() + time1 = time.monotonic() connection_observer.start() - time2 = time.time() + time2 = time.monotonic() assert connection_observer.start_time >= time1 assert connection_observer.start_time <= time2 @@ -355,9 +355,9 @@ def test_awaiting_done_on_already_done_connection_observer_immediately_returns_r # till it decides "ok, I've found what I was looking for" # and it internally sets the result connection_observer.set_result(14361) - result_set_time = time.time() + result_set_time = time.monotonic() assert connection_observer.await_done() == 14361 - assert time.time() - result_set_time < 0.01 # our immediately ;-) + assert time.monotonic() - result_set_time < 0.01 # our immediately ;-) def test_awaiting_done_on_not_running_connection_observer_raises_ConnectionObserverNotStarted( diff --git a/test/test_loggers.py b/test/test_loggers.py index 3036f1fad..5ede2a494 100644 --- a/test/test_loggers.py +++ b/test/test_loggers.py @@ -117,7 +117,7 @@ def test_RawTraceFormatter_produces_yaml_record(): import mock trace_formatter = RawTraceFormatter() binary_msg = b"127.0.0.1 \xe2\x86\x92 ttl" - now = time.time() + now = time.monotonic() with mock.patch("time.time", return_value=now): record = logging.Logger(name='moler').makeRecord(name=None, level=RAW_DATA, fn="", lno=0, msg=binary_msg, diff --git a/test/test_moler_sleep.py b/test/test_moler_sleep.py index d719d5d80..c00e667c4 100644 --- a/test/test_moler_sleep.py +++ b/test/test_moler_sleep.py @@ -14,11 +14,11 @@ def test_sleep_for_threaded_variant(): sleep_time = 1 - start_time = time.time() + start_time = time.monotonic() MolerTest.sleep(sleep_time) - stop_time = time.time() + stop_time = time.monotonic() elapsed = stop_time - start_time assert round(elapsed) == sleep_time diff --git a/test/test_runner.py b/test/test_runner.py index d998212fa..fcac4740c 100644 --- a/test/test_runner.py +++ b/test/test_runner.py @@ -28,7 +28,7 @@ def proxy_shutdown(self): with mock.patch.object(observer_runner.__class__, "shutdown", proxy_shutdown): with observer_runner: - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(connection_observer) assert observer_runner in shutdown_call_params @@ -46,7 +46,7 @@ def test_time_out_observer_can_set_proper_exception_inside_observer(conn_observe expected_timeout_class = ConnectionObserverTimeout with observer_runner: - conn_observer.life_status.start_time = time.time() + conn_observer.life_status.start_time = time.monotonic() observer_runner.submit(conn_observer) time_out_observer(conn_observer, timeout=2.3, passed_time=2.32, runner_logger=mock.MagicMock()) @@ -66,7 +66,7 @@ def on_timeout_handler(self): with mock.patch.object(conn_observer.__class__, "on_timeout", on_timeout_handler): with observer_runner: - conn_observer.life_status.start_time = time.time() + conn_observer.life_status.start_time = time.monotonic() observer_runner.submit(conn_observer) time_out_observer(conn_observer, timeout=2.3, passed_time=2.32, runner_logger=mock.MagicMock()) @@ -77,7 +77,7 @@ def test_runner_doesnt_impact_unrised_observer_exception_while_taking_observer_r from moler.exceptions import ConnectionObserverTimeout with observer_runner: - connection_observer.life_status.start_time = time.time() # must start observer lifetime before runner.submit() + connection_observer.life_status.start_time = time.monotonic() # must start observer lifetime before runner.submit() observer_runner.submit(connection_observer) time_out_observer(connection_observer, timeout=2.3, passed_time=2.32, runner_logger=mock.MagicMock()) @@ -122,8 +122,8 @@ def test_CancellableFuture_can_force_cancel_of_embedded_future(): feed_done = threading.Event() def fun_with_future_result(delay, should_stop): - start_time = time.time() - while not should_stop.is_set() and ((time.time() - start_time) < delay): + start_time = time.monotonic() + while not should_stop.is_set() and ((time.monotonic() - start_time) < delay): time.sleep(0.01) return delay * 2 @@ -177,7 +177,7 @@ def data_received(self, data, recv_time): self.all_data_received.append(data) if not self.done(): if "Network is unreachable" in data: - when_detected = time.time() + when_detected = time.monotonic() self.set_result(result=when_detected) diff --git a/trainings/workshop1/step12/network_outage.py b/trainings/workshop1/step12/network_outage.py index 4f11fa446..aac02ea1a 100644 --- a/trainings/workshop1/step12/network_outage.py +++ b/trainings/workshop1/step12/network_outage.py @@ -7,14 +7,14 @@ def outage_callback(device_name, ping_times): MolerTest.info("Network outage on {}".format(device_name)) - ping_times["lost_connection_time"] = time.time() + ping_times["lost_connection_time"] = time.monotonic() def ping_is_on_callback(ping_times): MolerTest.info("Ping works") if ping_times["lost_connection_time"] > 0: # ping operable AFTER any net loss if ping_times["reconnection_time"] == 0: - ping_times["reconnection_time"] = time.time() + ping_times["reconnection_time"] = time.monotonic() outage_time = ping_times["reconnection_time"] - ping_times["lost_connection_time"] MolerTest.info("Network outage time is {}".format(outage_time)) diff --git a/trainings/workshop1/step13/network_outage.py b/trainings/workshop1/step13/network_outage.py index 790d59e63..0d2b5c402 100644 --- a/trainings/workshop1/step13/network_outage.py +++ b/trainings/workshop1/step13/network_outage.py @@ -7,14 +7,14 @@ def outage_callback(device_name, ping_times): MolerTest.info("Network outage on {}".format(device_name)) - ping_times["lost_connection_time"] = time.time() + ping_times["lost_connection_time"] = time.monotonic() def ping_is_on_callback(ping_times): MolerTest.info("Ping works") if ping_times["lost_connection_time"] > 0: # ping operable AFTER any net loss if ping_times["reconnection_time"] == 0: - ping_times["reconnection_time"] = time.time() + ping_times["reconnection_time"] = time.monotonic() outage_time = ping_times["reconnection_time"] - ping_times["lost_connection_time"] MolerTest.info("Network outage time is {}".format(outage_time)) if outage_time > 3: diff --git a/trainings/workshop1/step14/network_outage.py b/trainings/workshop1/step14/network_outage.py index d4b24edad..234331674 100644 --- a/trainings/workshop1/step14/network_outage.py +++ b/trainings/workshop1/step14/network_outage.py @@ -7,14 +7,14 @@ def outage_callback(device_name, ping_times): MolerTest.info("Network outage on {}".format(device_name)) - ping_times["lost_connection_time"] = time.time() + ping_times["lost_connection_time"] = time.monotonic() def ping_is_on_callback(ping_times): MolerTest.info("Ping works") if ping_times["lost_connection_time"] > 0: # ping operable AFTER any net loss if ping_times["reconnection_time"] == 0: - ping_times["reconnection_time"] = time.time() + ping_times["reconnection_time"] = time.monotonic() outage_time = ping_times["reconnection_time"] - ping_times["lost_connection_time"] MolerTest.info("Network outage time is {}".format(outage_time)) if outage_time > 3: diff --git a/trainings/workshop1/step15/network_outage.py b/trainings/workshop1/step15/network_outage.py index 25ef7f482..4a13eb705 100644 --- a/trainings/workshop1/step15/network_outage.py +++ b/trainings/workshop1/step15/network_outage.py @@ -7,14 +7,14 @@ def outage_callback(device_name, ping_times): MolerTest.info("Network outage on {}".format(device_name)) - ping_times["lost_connection_time"] = time.time() + ping_times["lost_connection_time"] = time.monotonic() def ping_back_detector_callback(ping_times): if ping_times["lost_connection_time"] > 0: # ping operable AFTER any net loss if ping_times["reconnection_time"] == 0: MolerTest.info("Ping is back") - ping_times["reconnection_time"] = time.time() + ping_times["reconnection_time"] = time.monotonic() outage_time = ping_times["reconnection_time"] - ping_times["lost_connection_time"] MolerTest.info("Network outage time is {}".format(outage_time)) if outage_time > 3: