-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New dfms.apps.simple module and tests
These are the applications that we usually take from test.graphsRepository. That was not a proper location for them, given that we use them so much. Also, the "copy" from "SleepAndCopy" has been factored out, so now we have a sleeping app, a copying app, and a sleep *and* copy app (while in the version under the test.graphsRepository module we lacked the copy-only app). Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
- Loading branch information
Showing
3 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# | ||
# ICRAR - International Centre for Radio Astronomy Research | ||
# (c) UWA - The University of Western Australia, 2017 | ||
# Copyright by UWA (in the framework of the ICRAR) | ||
# All rights reserved | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307 USA | ||
# | ||
"""Applications used as examples, for testing, or in simple situations""" | ||
|
||
import time | ||
|
||
from .. import droputils | ||
from ..drop import BarrierAppDROP, ContainerDROP | ||
|
||
|
||
class NullBarrierApp(BarrierAppDROP): | ||
"""A BarrierAppDrop that doesn't perform any work""" | ||
def run(self): | ||
pass | ||
|
||
class SleepApp(BarrierAppDROP): | ||
"""A BarrierAppDrop that sleeps the specified amount of time (0 by default)""" | ||
|
||
def initialize(self, **kwargs): | ||
super(SleepApp, self).initialize(**kwargs) | ||
self._sleepTime = self._getArg(kwargs, 'sleepTime', 0) | ||
|
||
def run(self): | ||
time.sleep(self._sleepTime) | ||
|
||
class CopyApp(BarrierAppDROP): | ||
""" | ||
A BarrierAppDrop that copies its inputs into its outputs. | ||
All inputs are copied into all outputs in the order they were declared in | ||
the graph. | ||
""" | ||
def run(self): | ||
self.copyAll() | ||
|
||
def copyAll(self): | ||
for inputDrop in self.inputs: | ||
self.copyRecursive(inputDrop) | ||
|
||
def copyRecursive(self, inputDrop): | ||
if isinstance(inputDrop, ContainerDROP): | ||
for child in inputDrop.children: | ||
self.copyRecursive(child) | ||
else: | ||
for outputDrop in self.outputs: | ||
droputils.copyDropContents(inputDrop, outputDrop) | ||
|
||
class SleepAndCopyApp(SleepApp, CopyApp): | ||
"""A combination of the SleepApp and the CopyApp. It sleeps, then copies""" | ||
|
||
def run(self): | ||
SleepApp.run(self) | ||
CopyApp.run(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# | ||
# ICRAR - International Centre for Radio Astronomy Research | ||
# (c) UWA - The University of Western Australia, 2017 | ||
# Copyright by UWA (in the framework of the ICRAR) | ||
# All rights reserved | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, | ||
# MA 02111-1307 USA | ||
# | ||
import os | ||
import unittest | ||
|
||
from dfms import droputils | ||
from dfms.apps.simple import SleepApp, CopyApp, SleepAndCopyApp | ||
from dfms.ddap_protocol import DROPStates | ||
from dfms.drop import NullDROP, InMemoryDROP | ||
|
||
|
||
class TestSimpleApps(unittest.TestCase): | ||
|
||
def _test_graph_runs(self, drops, first, last): | ||
first = droputils.listify(first) | ||
with droputils.DROPWaiterCtx(self, last, 1): | ||
for f in first: | ||
f.setCompleted() | ||
|
||
for x in drops: | ||
self.assertEqual(DROPStates.COMPLETED, x.status) | ||
|
||
def test_sleepapp(self): | ||
|
||
# Nothing fancy, just run it and be done with it | ||
a = NullDROP('a', 'a') | ||
b = SleepApp('b', 'b') | ||
c = NullDROP('c', 'c') | ||
b.addInput(a) | ||
b.addOutput(c) | ||
|
||
self._test_graph_runs((a, b, c), a, c) | ||
|
||
def _test_copyapp_simple(self, app): | ||
|
||
# Again, not foo fancy, simple apps require simple tests | ||
a, c = (InMemoryDROP(x, x) for x in ('a', 'c')) | ||
b = app('b', 'b') | ||
b.addInput(a) | ||
b.addOutput(c) | ||
|
||
data = os.urandom(32) | ||
a.write(data) | ||
|
||
self._test_graph_runs((a, b, c), a, c) | ||
self.assertEqual(data, droputils.allDropContents(c)) | ||
|
||
def _test_copyapp_order_preserved(self, app): | ||
|
||
# Inputs are copied in the order they are added | ||
a, b, d = (InMemoryDROP(x, x) for x in ('a', 'b', 'd')) | ||
c = app('c', 'c') | ||
for x in a, b: | ||
c.addInput(x) | ||
c.addOutput(d) | ||
|
||
data1 = os.urandom(32) | ||
data2 = os.urandom(32) | ||
a.write(data1) | ||
b.write(data2) | ||
|
||
self._test_graph_runs((a, b, c, d), (a, b), d) | ||
self.assertEqual(data1 + data2, droputils.allDropContents(d)) | ||
|
||
def _test_copyapp(self, app): | ||
self._test_copyapp_simple(app) | ||
self._test_copyapp_order_preserved(app) | ||
|
||
def test_copyapp(self): | ||
self._test_copyapp(CopyApp) | ||
|
||
def test_sleepandcopyapp(self): | ||
self._test_copyapp(SleepAndCopyApp) |