Added test on ScheduledService Fixed issues in the ScheduledService Added abstract class to implement a task for the scheduledservice
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/4446d247 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/4446d247 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/4446d247 Branch: refs/heads/master Commit: 4446d24735ff8ff8808ac483927c5cb812ff2314 Parents: b5b6fb3 Author: Chamila de Alwis <[email protected]> Authored: Mon Oct 20 15:39:50 2014 +0530 Committer: Chamila de Alwis <[email protected]> Committed: Mon Oct 20 15:39:50 2014 +0530 ---------------------------------------------------------------------- .../modules/util/asyncscheduledtask.py | 29 +++++++++++++-- tools/python-cartridge-agent/test/__init__.py | 16 ++++++++ tools/python-cartridge-agent/test/asynctest.txt | 2 +- tools/python-cartridge-agent/test/test_util.py | 39 +++++++++++++++----- 4 files changed, 71 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/4446d247/tools/python-cartridge-agent/cartridgeagent/modules/util/asyncscheduledtask.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridgeagent/modules/util/asyncscheduledtask.py b/tools/python-cartridge-agent/cartridgeagent/modules/util/asyncscheduledtask.py index 9fdde1e..4ff0416 100644 --- a/tools/python-cartridge-agent/cartridgeagent/modules/util/asyncscheduledtask.py +++ b/tools/python-cartridge-agent/cartridgeagent/modules/util/asyncscheduledtask.py @@ -18,29 +18,50 @@ import time from threading import Thread +class AbstractAsyncScheduledTask: + """ + Exposes the contract to follow to implement a scheduled task to be executed by the ScheduledExecutor + """ + + def execute_task(self): + """ + Override this method and implement the task to be executed by the ScheduledExecutor with a specified + interval. + """ + raise NotImplementedError -class AsyncScheduledTask(Thread): + +class ScheduledExecutor(Thread): """ Executes a given task with a given interval until being terminated """ def __init__(self, delay, task): + """ + Creates a ScheduledExecutor thread to handle interval based repeated execution of a given task of type + AbstractAsyncScheduledTask + :param int delay: The interval to keep between executions + :param AbstractAsyncScheduledTask task: The task to be implemented + :return: + """ + Thread.__init__(self) self.delay = delay """ :type : int """ self.task = task - """ :type : Thread """ + """ :type : AbstractAsyncScheduledTask """ self.terminated = False """ :type : bool """ def run(self): """ - Start the scheuled task with a sleep time of delay in between + Start the scheduled task with a sleep time of delay in between :return: """ while not self.terminated: time.sleep(self.delay) - self.task.start() + task_thread = Thread(target=self.task.execute_task) + task_thread.start() def terminate(self): """ http://git-wip-us.apache.org/repos/asf/stratos/blob/4446d247/tools/python-cartridge-agent/test/__init__.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/test/__init__.py b/tools/python-cartridge-agent/test/__init__.py index e69de29..13a8339 100644 --- a/tools/python-cartridge-agent/test/__init__.py +++ b/tools/python-cartridge-agent/test/__init__.py @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. http://git-wip-us.apache.org/repos/asf/stratos/blob/4446d247/tools/python-cartridge-agent/test/asynctest.txt ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/test/asynctest.txt b/tools/python-cartridge-agent/test/asynctest.txt index ffae2e3..623c418 100644 --- a/tools/python-cartridge-agent/test/asynctest.txt +++ b/tools/python-cartridge-agent/test/asynctest.txt @@ -1 +1 @@ -1413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793 552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.293 61413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.2936141379 3552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29 361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.293614137 93552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.2 9361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413 793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552. 29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.2936141 3793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552 .29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.293614 13793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.2936141379355 2.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361 413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.293614137935 52.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.29361413793552.2936 1413793552.29361413793552.29361413793552.2936 \ No newline at end of file +1413799652508.8130 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/4446d247/tools/python-cartridge-agent/test/test_util.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/test/test_util.py b/tools/python-cartridge-agent/test/test_util.py index c7ee884..f62b2e8 100644 --- a/tools/python-cartridge-agent/test/test_util.py +++ b/tools/python-cartridge-agent/test/test_util.py @@ -1,20 +1,39 @@ -from cartridgeagent.modules.util.asyncscheduledtask import AsyncScheduledTask -from threading import Thread -import time +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. -def thread_worker(): - f = open("asynctest.txt", "w") - f.write("%1.4f" % time.time() * 1000) - f.close() +from cartridgeagent.modules.util.asyncscheduledtask import * +import time def test_async_task(): - astask = AsyncScheduledTask(2, Thread(thread_worker())) + test_task = TestTask() + astask = ScheduledExecutor(2, test_task) start_time = time.time() * 1000 - astask.run() + astask.start() time.sleep(3) astask.terminate() - f = open("asynctest.txt") + f = open("asynctest.txt", "r") end_time = float(f.read()) assert (end_time - start_time) >= 2 * 1000, "Task was executed before specified delay" +class TestTask(AbstractAsyncScheduledTask): + + def execute_task(self): + with open("asynctest.txt", "w") as f: + f.seek(0) + f.truncate() + f.write("%1.4f" % (time.time()*1000))
