Completed ports check tests Improved AsyncTask test
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/cd97e694 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/cd97e694 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/cd97e694 Branch: refs/heads/master Commit: cd97e69466c8cf7ae5dc58e7034eb385839ac1bb Parents: ebf9e0b Author: Chamila de Alwis <[email protected]> Authored: Tue Oct 21 15:35:02 2014 +0530 Committer: Chamila de Alwis <[email protected]> Committed: Tue Oct 21 15:35:02 2014 +0530 ---------------------------------------------------------------------- tools/python_cartridgeagent/test/asynctest.txt | 2 +- tools/python_cartridgeagent/test/test_util.py | 65 ++++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/cd97e694/tools/python_cartridgeagent/test/asynctest.txt ---------------------------------------------------------------------- diff --git a/tools/python_cartridgeagent/test/asynctest.txt b/tools/python_cartridgeagent/test/asynctest.txt index 4032a53..75eddf2 100644 --- a/tools/python_cartridgeagent/test/asynctest.txt +++ b/tools/python_cartridgeagent/test/asynctest.txt @@ -1 +1 @@ -1413834107419.9700 \ No newline at end of file +1413885854235.2659 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/cd97e694/tools/python_cartridgeagent/test/test_util.py ---------------------------------------------------------------------- diff --git a/tools/python_cartridgeagent/test/test_util.py b/tools/python_cartridgeagent/test/test_util.py index 2b4186e..e7c42ac 100644 --- a/tools/python_cartridgeagent/test/test_util.py +++ b/tools/python_cartridgeagent/test/test_util.py @@ -18,16 +18,34 @@ from ..cartridgeagent.modules.util.asyncscheduledtask import * from ..cartridgeagent.modules.util import cartridgeagentutils import time +import socket +from threading import Thread + +ASYNC_WRITE_FILE = "asynctest.txt" def test_async_task(): + with open(ASYNC_WRITE_FILE, "r") as f: + init_context = f.read() + test_task = TestTask() astask = ScheduledExecutor(1, test_task) start_time = time.time() * 1000 astask.start() - time.sleep(2) + contents_changed = False + timeout = 10 #seconds + + # wait till file content is written + while not contents_changed and (time.time() * 1000 - start_time) < (10 * 1000): + time.sleep(2) + with open(ASYNC_WRITE_FILE, "r") as f: + now_content = f.read() + + if init_context != now_content: + contents_changed = True + astask.terminate() - f = open("asynctest.txt", "r") + f = open(ASYNC_WRITE_FILE, "r") end_time = float(f.read()) assert (end_time - start_time) >= 1 * 1000, "Task was executed before specified delay" @@ -35,7 +53,7 @@ def test_async_task(): class TestTask(AbstractAsyncScheduledTask): def execute_task(self): - with open("asynctest.txt", "w") as f: + with open(ASYNC_WRITE_FILE, "w") as f: f.seek(0) f.truncate() f.write("%1.4f" % (time.time()*1000)) @@ -65,17 +83,48 @@ def test_decrypt_password_failure(): def test_create_dir_normal(): assert True + def test_create_dir_system_path(): assert True + def test_create_dir_existing_dir(): assert True + def test_wait_for_ports_activity_normal(): - assert True + portnumber = 12345 + listener = PortListener(portnumber) + listener.start() -def test_wait_for_ports_activity_non_existent(): - assert True + assert cartridgeagentutils.check_ports_active(socket.gethostbyname(socket.gethostname()), [str(portnumber)]) -def test_wait_for_ports_activity_timeout(): - assert True \ No newline at end of file + +class PortListener(Thread): + + def __init__(self, portnumber): + Thread.__init__(self) + self.portnumber = portnumber + self.terminated = False + + def run(self): + s = socket.socket() + host = socket.gethostname() + + s.bind((host, self.portnumber)) + s.listen(5) + + #while not self.terminated: + c, addr = s.accept() # Establish connection with client. + #print 'Got connection from', addr + c.send('Thank you for connecting') + c.close() + + s.close() + + def terminate(self): + self.terminated = True + + +def test_wait_for_ports_activity_non_existent(): + assert cartridgeagentutils.check_ports_active(socket.gethostbyname(socket.gethostname()), [str(34565)]) == False
