-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wait4Prompts - reverse order of processing lines (#537)
* Wait4Prompts - reverse order of processing lines * unit test * test fix
- Loading branch information
1 parent
e947605
commit 32fcca7
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
__author__ = "Michal Ernst, Marcin Usielski" | ||
__copyright__ = "Copyright (C) 2019-2023, Nokia" | ||
__copyright__ = "Copyright (C) 2019-2024, Nokia" | ||
__email__ = "[email protected], [email protected]" | ||
|
||
import datetime | ||
|
@@ -34,6 +34,10 @@ def __init__(self, connection, prompts, till_occurs_times=-1, runner=None): | |
self.check_against_all_prompts = False | ||
self._ret_list_matched = [] | ||
|
||
# Change default order and behavior after matching the prompt | ||
self._reverse_order = True | ||
self._break_processing_when_found = True | ||
|
||
def on_new_line(self, line, is_full_line): | ||
try: | ||
self._parse_prompts(line) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
__author__ = 'Marcin Usielski' | ||
__copyright__ = 'Copyright (C) 2021-2023, Nokia' | ||
__copyright__ = 'Copyright (C) 2021-2024, Nokia' | ||
__email__ = '[email protected]' | ||
|
||
import time | ||
|
||
from moler.events.unix.wait4prompts import Wait4prompts | ||
import datetime | ||
|
@@ -63,3 +64,53 @@ def test_event_wait4prompts_wrong_1_prompt_from_1_line(buffer_connection): | |
assert 'list_matched' not in ret | ||
|
||
|
||
def test_event_wait4prompts_reverse_order(buffer_connection): | ||
matched_states = [] | ||
|
||
def callback(w4p_event): | ||
occurrence = w4p_event.get_last_occurrence() | ||
state = occurrence["state"] | ||
matched_states.append(state) | ||
|
||
prompts = {re.compile(r'host:.*#'): "UNIX_LOCAL", re.compile(r'user@server.*#'): "USER"} | ||
output = "user@host:/home/#\nBLABLA\nuser@server:/home/#\n" | ||
event = Wait4prompts(connection=buffer_connection.moler_connection, | ||
till_occurs_times=-1, prompts=prompts) | ||
event.add_event_occurred_callback(callback=callback, callback_params={"w4p_event": event}) | ||
event._reverse_order = True | ||
event.start() | ||
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now()) | ||
start_time = time.monotonic() | ||
while 1 != len(matched_states) and time.monotonic() - start_time < 10: | ||
time.sleep(0.1) | ||
time.sleep(0.1) | ||
event.cancel() | ||
assert 1 == len(matched_states) | ||
assert 'USER' in matched_states | ||
|
||
|
||
def test_event_wait4prompts_normal_order(buffer_connection): | ||
matched_states = [] | ||
|
||
def callback(w4p_event): | ||
occurrence = w4p_event.get_last_occurrence() | ||
state = occurrence["state"] | ||
matched_states.append(state) | ||
|
||
prompts = {re.compile(r'host:.*#'): "UNIX_LOCAL", re.compile(r'user@server.*#'): "USER"} | ||
output = "user@host:/home/#\nBLABLA\nuser@server:/home/#\n" | ||
event = Wait4prompts(connection=buffer_connection.moler_connection, | ||
till_occurs_times=-1, prompts=prompts) | ||
event._reverse_order = False | ||
event._break_processing_when_found = False | ||
event.add_event_occurred_callback(callback=callback, callback_params={"w4p_event": event}) | ||
event.start() | ||
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now()) | ||
start_time = time.monotonic() | ||
while 2 != len(matched_states) and time.monotonic() - start_time < 10: | ||
time.sleep(0.1) | ||
time.sleep(0.1) | ||
event.cancel() | ||
assert 2 == len(matched_states) | ||
assert matched_states == ['UNIX_LOCAL', 'USER'] | ||
|