Skip to content

Commit

Permalink
remove redundant _prepare_regexp
Browse files Browse the repository at this point in the history
change is_regex implementation
logmatcher.match to read til EOF if there are more immediate lines regardless of timeout
  • Loading branch information
Pyifan committed Jan 9, 2025
1 parent 6b3d418 commit a71e558
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 3 additions & 1 deletion testplan/common/utils/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def is_regex(obj):
"""
Cannot do type check against SRE_Pattern, so we use duck typing.
"""
return hasattr(obj, "match") and hasattr(obj, "pattern")
import re

return isinstance(obj, re.Pattern)


def basic_compare(first, second, strict=False):
Expand Down
8 changes: 4 additions & 4 deletions testplan/common/utils/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def _prepare_regexp(self, regexp: Regex) -> Pattern[AnyStr]:

if isinstance(regexp, (str, bytes)):
regexp = re.compile(regexp)
elif isinstance(regexp, re.Pattern):
pass
else:
try:
import rpyc
Expand Down Expand Up @@ -278,7 +280,6 @@ def _match(
match = None
start_time = time.time()
end_time = start_time + timeout
regex = self._prepare_regexp(regex)

with closing(self.log_stream) as log:
log.seek(self.position)
Expand All @@ -301,13 +302,12 @@ def _match(
if match:
break
elif timeout > 0:
if time.time() > end_time:
break
time.sleep(LOG_MATCHER_INTERVAL)
else:
break

if timeout > 0 and time.time() > end_time:
break

self.position = self.log_stream.position
if self._debug_info_e is None:
self._debug_info_e = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@
"type": "LogfileMatch",
"description": None,
"passed": True,
"timeout": 1,
"timeout": 0.1,
"results": [
{
"matched": "lime juice",
Expand All @@ -1007,7 +1007,7 @@
"type": "LogfileMatch",
"description": None,
"passed": True,
"timeout": 1,
"timeout": 0.1,
"results": [
{
"matched": "ginger beer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ def test_logfile(self, env, result):
f.write("vodka\n")
f.write("lime juice\n")
f.flush()
result.logfile.match(lm, r"lime juice", timeout=1)
result.logfile.match(lm, r"lime juice", timeout=0.1)
result.logfile.seek_eof(lm)
with result.logfile.expect(lm, r"ginger beer", timeout=1):
with result.logfile.expect(lm, r"ginger beer", timeout=0.1):
f.write("ginger beer\n")
f.flush()
finally:
Expand Down

0 comments on commit a71e558

Please sign in to comment.