Skip to content

Commit eda8e1c

Browse files
committed
refactor: logging and killing processes
1 parent 209d48f commit eda8e1c

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

core/base_test/tns_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ def tearDown(self):
5050
Tns.kill()
5151

5252
for process in TestContext.STARTED_PROCESSES:
53-
Log.info("Kill Process: " + os.linesep + process.commandline)
54-
Process.kill_pid(process.pid)
53+
if Process.is_running(process.pid):
54+
Log.info("Kill Process: " + os.linesep + process.commandline)
55+
Process.kill_pid(process.pid)
5556

5657
# Analise test result
5758
result = self._resultForDoCleanups

core/utils/device/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def is_text_visible(self, text):
5757

5858
# Retry find with ORC if macOS automation fails
5959
if not is_visible:
60-
actual_text = self.get_text()
60+
actual_text = self.get_text().encode('utf-8').strip()
6161
if text in actual_text:
6262
is_visible = True
6363
else:

core/utils/device/simctl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from core.log.log import Log
66
from core.utils.file_utils import File
7-
from core.utils.process import Run
7+
from core.utils.process import Run, Process
88
from core.utils.wait import Wait
99

1010

@@ -19,8 +19,10 @@ def __run_simctl_command(command, wait=True, timeout=30):
1919
# noinspection PyBroadException
2020
@staticmethod
2121
def __get_simulators():
22-
logs = Simctl.__run_simctl_command(command='list --json devices', wait=False).log_file
22+
result = Simctl.__run_simctl_command(command='list --json devices', wait=False)
23+
logs = result.log_file
2324
found = Wait.until(lambda: 'iPhone' in File.read(logs), timeout=30)
25+
Process.kill_pid(result.pid)
2426
if found:
2527
json_content = '{' + File.read(logs).split('{', 1)[-1]
2628
try:

core/utils/file_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Folder(object):
1313
@staticmethod
1414
def clean(folder):
1515
if Folder.exists(folder=folder):
16-
Log.info("Clean folder: " + folder)
16+
Log.debug("Clean folder: " + folder)
1717
try:
1818
shutil.rmtree(folder)
1919
except OSError as e:
@@ -33,7 +33,7 @@ def exists(folder):
3333

3434
@staticmethod
3535
def create(folder):
36-
Log.info("Create folder: " + folder)
36+
Log.debug("Create folder: " + folder)
3737
if not os.path.exists(folder):
3838
try:
3939
os.makedirs(folder)

core/utils/process.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ def command(cmd, cwd=Settings.TEST_RUN_HOME, wait=True, fail_safe=False, registe
8282
# noinspection PyBroadException,PyUnusedLocal
8383
class Process(object):
8484
@staticmethod
85-
def is_running(proc_name):
85+
def is_running(pid):
86+
try:
87+
os.kill(pid, 0)
88+
return True
89+
except OSError:
90+
return False
91+
92+
@staticmethod
93+
def is_running_by_name(proc_name):
8694
"""
8795
Check if process is running.
8896
"""
@@ -135,7 +143,7 @@ def wait_until_running(proc_name, timeout=60):
135143
end_time = time.time() + timeout
136144
while not running:
137145
time.sleep(5)
138-
running = Process.is_running(proc_name)
146+
running = Process.is_running_by_name(proc_name)
139147
if running:
140148
running = True
141149
break
@@ -189,6 +197,7 @@ def kill_pid(pid):
189197
try:
190198
p = psutil.Process(pid)
191199
p.terminate()
200+
Log.log(level=logging.DEBUG, message="Process has been killed: {0}{1}".format(os.linesep, p.cmdline()))
192201
except Exception:
193202
pass
194203

core/utils/process_info.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from psutil import Process
2-
3-
41
class ProcessInfo(object):
52
def __init__(self, cmd=None, pid=None, exit_code=None, output='', log_file=None, complete=True, duration=None):
63
self.commandline = cmd
@@ -10,8 +7,3 @@ def __init__(self, cmd=None, pid=None, exit_code=None, output='', log_file=None,
107
self.log_file = log_file
118
self.complete = complete
129
self.duration = duration
13-
14-
@classmethod
15-
def kill(cls, pid):
16-
p = Process(pid)
17-
p.terminate()

products/angular/ng.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def serve(project=Settings.AppName.DEFAULT, verify=True):
8484
result = NG.exec_command(command='serve', cwd=project_path, wait=False)
8585
if verify:
8686
compiled = Wait.until(lambda: 'Compiled successfully' in File.read(result.log_file))
87+
Process.kill_pid(result.pid)
8788
assert compiled, 'Failed to compile NG app at {0}'.format(project)
8889
return result
8990

0 commit comments

Comments
 (0)