Skip to content

Commit

Permalink
New dfms.apps.simple module and tests
Browse files Browse the repository at this point in the history
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
rtobar committed Aug 9, 2017
1 parent 5d3f316 commit 5f7686b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
71 changes: 71 additions & 0 deletions dfms/apps/simple.py
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)
2 changes: 1 addition & 1 deletion dfms/dropmake/web/lg_editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@
{ category: "Start", text: "Start" },
{ category: "End", text: "End" },
{ category: "Component", text: "PythonApp", execution_time: 5,
num_cpus: 1, group_start: 0, appclass: "test.graphsRepository.SleepApp",
num_cpus: 1, group_start: 0, appclass: "dfms.apps.simple.SleepApp",
Arg01: "", Arg02: "", Arg03: "", Arg04: "", Arg05: "",
Arg06: "", Arg07: "", Arg08: "", Arg09: "", Arg10: ""},
{ category: "BashShellApp", text: "ShellApp", execution_time: 5,
Expand Down
92 changes: 92 additions & 0 deletions test/apps/test_simple.py
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)

0 comments on commit 5f7686b

Please sign in to comment.