Skip to content

Commit 45b8666

Browse files
Vasil ChimevVasil Chimev
Vasil Chimev
authored and
Vasil Chimev
committed
Add watch_base_class.py
1 parent ff42fb5 commit 45b8666

File tree

2 files changed

+19
-79
lines changed

2 files changed

+19
-79
lines changed

helpers/watch_abc.py renamed to helpers/watch_base_class.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
'''
2-
This class implements Watch Abstract Base Class.
2+
This class implements Watch Base Class.
33
'''
44

55
# C0111 - Missing docstring
66
# R0201 - Method could be a function
77
# pylint: disable=C0111, R0201
88

9-
from abc import ABCMeta, abstractmethod
9+
import psutil, subprocess, time, unittest
1010
from multiprocessing import Process
11-
import psutil, subprocess, time
1211

1312

14-
class WatchABC:
15-
__metaclass__ = ABCMeta
13+
class WatchBaseClass(unittest.TestCase):
1614

1715
SECONDS_TO_WAIT = 120
1816

19-
@abstractmethod
20-
def __init__(self, command):
21-
self.process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
17+
@classmethod
18+
def start_watcher(cls, command):
19+
cls.process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
2220

23-
@abstractmethod
2421
def wait_for_text_in_output(self, text):
2522
def read_loop():
2623
count = 0
@@ -49,7 +46,6 @@ def read_loop():
4946

5047
self.run_with_timeout(self.SECONDS_TO_WAIT, read_loop)
5148

52-
@abstractmethod
5349
def run_with_timeout(self, timeout, func):
5450
if not timeout:
5551
func()
@@ -66,12 +62,12 @@ def run_with_timeout(self, timeout, func):
6662
raise Exception("Timeout while waiting for livesync.")
6763
time.sleep(0.5)
6864

69-
@abstractmethod
70-
def terminate(self):
65+
@classmethod
66+
def terminate_watcher(cls):
7167
print "~~~ Killing subprocess ..."
72-
self.process.terminate()
68+
cls.process.terminate()
7369

7470
time.sleep(2)
75-
if psutil.pid_exists(self.process.pid):
71+
if psutil.pid_exists(cls.process.pid):
7672
print "~~~ Forced killing subprocess ..."
77-
self.process.kill()
73+
cls.process.kill()

tests/livesync_simulator.py

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
# R0201 - Method could be a function
88
# pylint: disable=C0103, C0111, R0201
99

10-
import psutil, subprocess, shutil, time, unittest
11-
from multiprocessing import Process
10+
import shutil, time
1211

1312
from helpers._os_lib import cleanup_folder, replace
1413
from helpers._tns_lib import IOS_RUNTIME_SYMLINK_PATH, \
@@ -17,9 +16,10 @@
1716
from helpers.simulator import create_simulator, delete_simulator, \
1817
cat_app_file_on_simulator, start_simulator, stop_simulators, \
1918
SIMULATOR_NAME
19+
from helpers.watch_base_class import WatchBaseClass
2020

2121

22-
class LiveSyncSimulator(unittest.TestCase):
22+
class LiveSyncSimulator(WatchBaseClass):
2323

2424
SECONDS_TO_WAIT = 120
2525

@@ -56,7 +56,7 @@ def setUpClass(cls):
5656
# livesync
5757
command = TNS_PATH + " livesync ios --emulator --watch --path TNS_App --log trace"
5858
print command
59-
cls.process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
59+
cls.start_watcher(command)
6060

6161
def setUp(self):
6262

@@ -73,62 +73,12 @@ def tearDown(self):
7373

7474
@classmethod
7575
def tearDownClass(cls):
76-
print "~~~ Killing subprocess ..."
77-
cls.process.terminate()
78-
79-
time.sleep(2)
80-
if psutil.pid_exists(cls.process.pid):
81-
print "~~~ Forced killing subprocess ..."
82-
cls.process.kill()
83-
76+
cls.terminate_watcher()
8477
stop_simulators()
78+
8579
cleanup_folder('TNS_App')
8680
cleanup_folder('appTest')
8781

88-
def wait_for_text_in_output(self, text):
89-
def read_loop():
90-
count = 0
91-
found = False
92-
print "~~~ Waiting for: " + text
93-
94-
while not found:
95-
if count == 0:
96-
line = self.process.stdout.readline()
97-
if text in line:
98-
print " + Text \"{0}\" found in: ".format(text) + line.rstrip(),
99-
print '\n'
100-
count = 1
101-
continue
102-
else:
103-
print (" - " + line),
104-
if count == 1:
105-
line = self.process.stdout.readline()
106-
if text in line:
107-
print " + Text \"{0}\" found in: ".format(text) + line.rstrip(),
108-
raise Exception("The console.log() message duplicates.")
109-
else:
110-
found = True
111-
print (" - " + line),
112-
break
113-
114-
self.run_with_timeout(self.SECONDS_TO_WAIT, read_loop)
115-
116-
def run_with_timeout(self, timeout, func):
117-
if not timeout:
118-
func()
119-
return
120-
121-
p = Process(target=func)
122-
p.start()
123-
124-
start_time = time.time()
125-
end_time = start_time + timeout
126-
while p.is_alive():
127-
if time.time() > end_time:
128-
p.terminate()
129-
raise Exception("Timeout while waiting for livesync.")
130-
time.sleep(0.5)
131-
13282
def test_001_full_livesync_ios_simulator_xml_js_css_tns_files(self):
13383

13484
# TODO: Update with console.log() when supported on simulators ...
@@ -216,15 +166,9 @@ def test_113_livesync_ios_simulator_watch_change_css_file(self):
216166
# assert "No such file or directory" in output
217167

218168
def test_301_livesync_ios_simulator_before_run(self):
219-
print "~~~ Killing subprocess ..."
220-
self.process.terminate()
221-
222-
time.sleep(2)
223-
if psutil.pid_exists(self.process.pid):
224-
print "~~~ Forced killing subprocess ..."
225-
self.process.kill()
226-
169+
self.terminate_watcher()
227170
cleanup_folder('TNS_App')
171+
228172
create_project(proj_name="TNS_App")
229173
platform_add(platform="ios", framework_path=IOS_RUNTIME_SYMLINK_PATH, \
230174
path="TNS_App", symlink=True)

0 commit comments

Comments
 (0)