added support for multi-type waiting plus some cleanup
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
|
||||
import unittest
|
||||
|
||||
from multiprocessing import Pipe, Queue
|
||||
from time import sleep
|
||||
|
||||
from core.correctness.vars import CHAR_LOWERCASE, CHAR_UPPERCASE, \
|
||||
BAREBONES_NOTEBOOK
|
||||
from core.functionality import create_rules, generate_id
|
||||
from core.functionality import create_rules, generate_id, wait, \
|
||||
check_pattern_dict, check_recipe_dict
|
||||
from core.meow import BaseRule
|
||||
from patterns.file_event_pattern import FileEventPattern
|
||||
from recipes.jupyter_notebook_recipe import JupyterNotebookRecipe
|
||||
@ -87,3 +91,199 @@ class CorrectnessTests(unittest.TestCase):
|
||||
}
|
||||
with self.assertRaises(KeyError):
|
||||
create_rules({}, recipes)
|
||||
|
||||
def testCheckPatternDictValid(self)->None:
|
||||
fep1 = FileEventPattern("name_one", "path", "recipe", "file")
|
||||
fep2 = FileEventPattern("name_two", "path", "recipe", "file")
|
||||
|
||||
patterns = {
|
||||
fep1.name: fep1,
|
||||
fep2.name: fep2
|
||||
}
|
||||
|
||||
check_pattern_dict(patterns=patterns)
|
||||
|
||||
def testCheckPatternDictNoEntries(self)->None:
|
||||
with self.assertRaises(ValueError):
|
||||
check_pattern_dict(patterns={})
|
||||
|
||||
check_pattern_dict(patterns={}, min_length=0)
|
||||
|
||||
def testCheckPatternDictMissmatchedName(self)->None:
|
||||
fep1 = FileEventPattern("name_one", "path", "recipe", "file")
|
||||
fep2 = FileEventPattern("name_two", "path", "recipe", "file")
|
||||
|
||||
patterns = {
|
||||
fep2.name: fep1,
|
||||
fep1.name: fep2
|
||||
}
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
check_pattern_dict(patterns=patterns)
|
||||
|
||||
def testCheckRecipeDictValid(self)->None:
|
||||
jnr1 = JupyterNotebookRecipe("recipe_one", BAREBONES_NOTEBOOK)
|
||||
jnr2 = JupyterNotebookRecipe("recipe_two", BAREBONES_NOTEBOOK)
|
||||
|
||||
recipes = {
|
||||
jnr1.name: jnr1,
|
||||
jnr2.name: jnr2
|
||||
}
|
||||
|
||||
check_recipe_dict(recipes=recipes)
|
||||
|
||||
def testCheckRecipeDictNoEntires(self)->None:
|
||||
with self.assertRaises(ValueError):
|
||||
check_recipe_dict(recipes={})
|
||||
|
||||
check_recipe_dict(recipes={}, min_length=0)
|
||||
|
||||
def testCheckRecipeDictMismatchedName(self)->None:
|
||||
jnr1 = JupyterNotebookRecipe("recipe_one", BAREBONES_NOTEBOOK)
|
||||
jnr2 = JupyterNotebookRecipe("recipe_two", BAREBONES_NOTEBOOK)
|
||||
|
||||
recipes = {
|
||||
jnr2.name: jnr1,
|
||||
jnr1.name: jnr2
|
||||
}
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
check_recipe_dict(recipes=recipes)
|
||||
|
||||
def testWaitPipes(self)->None:
|
||||
pipe_one_reader, pipe_one_writer = Pipe()
|
||||
pipe_two_reader, pipe_two_writer = Pipe()
|
||||
|
||||
inputs = [
|
||||
pipe_one_reader, pipe_two_reader
|
||||
]
|
||||
|
||||
pipe_one_writer.send(1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(pipe_one_reader, readables)
|
||||
self.assertEqual(len(readables), 1)
|
||||
msg = readables[0].recv()
|
||||
self.assertEqual(msg, 1)
|
||||
|
||||
pipe_one_writer.send(1)
|
||||
pipe_two_writer.send(2)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(pipe_one_reader, readables)
|
||||
self.assertIn(pipe_two_reader, readables)
|
||||
self.assertEqual(len(readables), 2)
|
||||
for readable in readables:
|
||||
if readable == pipe_one_reader:
|
||||
msg = readable.recv()
|
||||
self.assertEqual(msg, 1)
|
||||
elif readable == pipe_two_reader:
|
||||
msg = readable.recv()
|
||||
self.assertEqual(msg, 2)
|
||||
|
||||
def testWaitQueues(self)->None:
|
||||
queue_one = Queue()
|
||||
queue_two = Queue()
|
||||
|
||||
inputs = [
|
||||
queue_one, queue_two
|
||||
]
|
||||
|
||||
queue_one.put(1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(queue_one, readables)
|
||||
self.assertEqual(len(readables), 1)
|
||||
msg = readables[0].get()
|
||||
self.assertEqual(msg, 1)
|
||||
|
||||
queue_one.put(1)
|
||||
queue_two.put(2)
|
||||
sleep(0.1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(queue_one, readables)
|
||||
self.assertIn(queue_two, readables)
|
||||
self.assertEqual(len(readables), 2)
|
||||
for readable in readables:
|
||||
if readable == queue_one:
|
||||
msg = readable.get()
|
||||
self.assertEqual(msg, 1)
|
||||
elif readable == queue_two:
|
||||
msg = readable.get()
|
||||
self.assertEqual(msg, 2)
|
||||
|
||||
|
||||
def testWaitPipesAndQueues(self)->None:
|
||||
pipe_one_reader, pipe_one_writer = Pipe()
|
||||
pipe_two_reader, pipe_two_writer = Pipe()
|
||||
queue_one = Queue()
|
||||
queue_two = Queue()
|
||||
|
||||
inputs = [
|
||||
pipe_one_reader, pipe_two_reader, queue_one, queue_two
|
||||
]
|
||||
|
||||
pipe_one_writer.send(1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(pipe_one_reader, readables)
|
||||
self.assertEqual(len(readables), 1)
|
||||
msg = readables[0].recv()
|
||||
self.assertEqual(msg, 1)
|
||||
|
||||
pipe_one_writer.send(1)
|
||||
pipe_two_writer.send(2)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(pipe_one_reader, readables)
|
||||
self.assertIn(pipe_two_reader, readables)
|
||||
self.assertEqual(len(readables), 2)
|
||||
for readable in readables:
|
||||
if readable == pipe_one_reader:
|
||||
msg = readable.recv()
|
||||
self.assertEqual(msg, 1)
|
||||
if readable == pipe_two_reader:
|
||||
msg = readable.recv()
|
||||
self.assertEqual(msg, 2)
|
||||
|
||||
queue_one.put(1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(queue_one, readables)
|
||||
self.assertEqual(len(readables), 1)
|
||||
msg = readables[0].get()
|
||||
self.assertEqual(msg, 1)
|
||||
|
||||
queue_one.put(1)
|
||||
queue_two.put(2)
|
||||
sleep(0.1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(queue_one, readables)
|
||||
self.assertIn(queue_two, readables)
|
||||
self.assertEqual(len(readables), 2)
|
||||
for readable in readables:
|
||||
if readable == queue_one:
|
||||
msg = readable.get()
|
||||
self.assertEqual(msg, 1)
|
||||
elif readable == queue_two:
|
||||
msg = readable.get()
|
||||
self.assertEqual(msg, 2)
|
||||
|
||||
queue_one.put(1)
|
||||
pipe_one_writer.send(1)
|
||||
sleep(0.1)
|
||||
readables = wait(inputs)
|
||||
|
||||
self.assertIn(queue_one, readables)
|
||||
self.assertIn(pipe_one_reader, readables)
|
||||
self.assertEqual(len(readables), 2)
|
||||
for readable in readables:
|
||||
if readable == queue_one:
|
||||
msg = readable.get()
|
||||
self.assertEqual(msg, 1)
|
||||
elif readable == pipe_one_reader:
|
||||
msg = readable.recv()
|
||||
self.assertEqual(msg, 1)
|
||||
|
||||
|
@ -71,13 +71,13 @@ class MeowTests(unittest.TestCase):
|
||||
|
||||
def testBaseMonitor(self)->None:
|
||||
with self.assertRaises(TypeError):
|
||||
BaseMonitor("", "", "")
|
||||
BaseMonitor("", "")
|
||||
|
||||
class TestMonitor(BaseMonitor):
|
||||
pass
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
TestMonitor("", "", "")
|
||||
TestMonitor("", "")
|
||||
|
||||
class FullTestMonitor(BaseMonitor):
|
||||
def start(self):
|
||||
@ -86,11 +86,9 @@ class MeowTests(unittest.TestCase):
|
||||
pass
|
||||
def _is_valid_report(self, report:Any)->None:
|
||||
pass
|
||||
def _is_valid_listen(self, listen:Any)->None:
|
||||
pass
|
||||
def _is_valid_rules(self, rules:Any)->None:
|
||||
pass
|
||||
FullTestMonitor("", "", "")
|
||||
FullTestMonitor("", "")
|
||||
|
||||
def testBaseHandler(self)->None:
|
||||
with self.assertRaises(TypeError):
|
||||
@ -103,11 +101,13 @@ class MeowTests(unittest.TestCase):
|
||||
TestHandler("")
|
||||
|
||||
class FullTestHandler(BaseHandler):
|
||||
def handle(self):
|
||||
def handle(self, event, rule):
|
||||
pass
|
||||
def start(self):
|
||||
pass
|
||||
def stop(self):
|
||||
pass
|
||||
def _is_valid_inputs(self, inputs:Any)->None:
|
||||
pass
|
||||
FullTestHandler("")
|
||||
|
||||
|
||||
|
||||
|
@ -112,14 +112,11 @@ class CorrectnessTests(unittest.TestCase):
|
||||
self.assertEqual(fep.event_mask, FILE_EVENTS)
|
||||
|
||||
def testWatchdogMonitorMinimum(self)->None:
|
||||
to_monitor = Pipe()
|
||||
from_monitor = Pipe()
|
||||
WatchdogMonitor(TEST_BASE, {}, from_monitor[PIPE_WRITE],
|
||||
to_monitor[PIPE_READ])
|
||||
WatchdogMonitor(TEST_BASE, {}, from_monitor[PIPE_WRITE])
|
||||
|
||||
def testWatchdogMonitorEventIdentificaion(self)->None:
|
||||
to_monitor = Pipe()
|
||||
from_monitor = Pipe()
|
||||
from_monitor_reader, from_monitor_writer = Pipe()
|
||||
|
||||
pattern_one = FileEventPattern(
|
||||
"pattern_one", "A", "recipe_one", "file_one")
|
||||
@ -134,14 +131,13 @@ class CorrectnessTests(unittest.TestCase):
|
||||
}
|
||||
rules = create_rules(patterns, recipes)
|
||||
|
||||
wm = WatchdogMonitor(TEST_BASE, rules, from_monitor[PIPE_WRITE],
|
||||
to_monitor[PIPE_READ])
|
||||
wm = WatchdogMonitor(TEST_BASE, rules, from_monitor_writer)
|
||||
|
||||
wm.start()
|
||||
|
||||
open(os.path.join(TEST_BASE, "A"), "w")
|
||||
if from_monitor[PIPE_READ].poll(3):
|
||||
message = from_monitor[PIPE_READ].recv()
|
||||
if from_monitor_reader.poll(3):
|
||||
message = from_monitor_reader.recv()
|
||||
|
||||
self.assertIsNotNone(message)
|
||||
event, rule = message
|
||||
@ -150,8 +146,8 @@ class CorrectnessTests(unittest.TestCase):
|
||||
self.assertEqual(event.src_path, os.path.join(TEST_BASE, "A"))
|
||||
|
||||
open(os.path.join(TEST_BASE, "B"), "w")
|
||||
if from_monitor[PIPE_READ].poll(3):
|
||||
new_message = from_monitor[PIPE_READ].recv()
|
||||
if from_monitor_reader.poll(3):
|
||||
new_message = from_monitor_reader.recv()
|
||||
else:
|
||||
new_message = None
|
||||
self.assertIsNone(new_message)
|
||||
|
@ -2,7 +2,10 @@
|
||||
import jsonschema
|
||||
import unittest
|
||||
|
||||
from recipes.jupyter_notebook_recipe import JupyterNotebookRecipe
|
||||
from multiprocessing import Pipe
|
||||
|
||||
from recipes.jupyter_notebook_recipe import JupyterNotebookRecipe, \
|
||||
PapermillHandler
|
||||
from core.correctness.vars import BAREBONES_NOTEBOOK
|
||||
|
||||
class CorrectnessTests(unittest.TestCase):
|
||||
@ -73,3 +76,36 @@ class CorrectnessTests(unittest.TestCase):
|
||||
jnr = JupyterNotebookRecipe(
|
||||
"name", BAREBONES_NOTEBOOK, source=source)
|
||||
self.assertEqual(jnr.source, source)
|
||||
|
||||
def testPapermillHanderMinimum(self)->None:
|
||||
monitor_to_handler_reader, _ = Pipe()
|
||||
|
||||
PapermillHandler([monitor_to_handler_reader])
|
||||
|
||||
def testPapermillHanderStartStop(self)->None:
|
||||
monitor_to_handler_reader, _ = Pipe()
|
||||
|
||||
ph = PapermillHandler([monitor_to_handler_reader])
|
||||
|
||||
ph.start()
|
||||
ph.stop()
|
||||
|
||||
def testPapermillHanderRepeatedStarts(self)->None:
|
||||
monitor_to_handler_reader, _ = Pipe()
|
||||
|
||||
ph = PapermillHandler([monitor_to_handler_reader])
|
||||
|
||||
ph.start()
|
||||
with self.assertRaises(RuntimeWarning):
|
||||
ph.start()
|
||||
ph.stop()
|
||||
|
||||
def testPapermillHanderStopBeforeStart(self)->None:
|
||||
monitor_to_handler_reader, _ = Pipe()
|
||||
|
||||
ph = PapermillHandler([monitor_to_handler_reader])
|
||||
|
||||
with self.assertRaises(RuntimeWarning):
|
||||
ph.stop()
|
||||
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
|
||||
import unittest
|
||||
|
||||
from typing import Any
|
||||
from typing import Any, Union
|
||||
|
||||
from core.correctness.validation import check_input, valid_string, \
|
||||
valid_dict, valid_list
|
||||
from core.correctness.validation import check_type, check_implementation, \
|
||||
valid_string, valid_dict, valid_list
|
||||
from core.correctness.vars import VALID_NAME_CHARS
|
||||
|
||||
|
||||
@ -15,21 +15,28 @@ class CorrectnessTests(unittest.TestCase):
|
||||
def tearDown(self)->None:
|
||||
return super().tearDown()
|
||||
|
||||
def testCheckInputValid(self)->None:
|
||||
check_input(1, int)
|
||||
check_input(0, int)
|
||||
check_input(False, bool)
|
||||
check_input(True, bool)
|
||||
check_input(1, Any)
|
||||
|
||||
def testCheckInputMistyped(self)->None:
|
||||
with self.assertRaises(TypeError):
|
||||
check_input(1, str)
|
||||
def testCheckTypeValid(self)->None:
|
||||
check_type(1, int)
|
||||
check_type(0, int)
|
||||
check_type(False, bool)
|
||||
check_type(True, bool)
|
||||
|
||||
def testCheckInputOrNone(self)->None:
|
||||
check_input(None, int, or_none=True)
|
||||
def testCheckTypeValidAny(self)->None:
|
||||
check_type(1, Any)
|
||||
|
||||
def testCheckTypeValidUnion(self)->None:
|
||||
check_type(1, Union[int,str])
|
||||
with self.assertRaises(TypeError):
|
||||
check_input(None, int, or_none=False)
|
||||
check_type(Union[int, str], Union[int,str])
|
||||
|
||||
def testCheckTypeMistyped(self)->None:
|
||||
with self.assertRaises(TypeError):
|
||||
check_type(1, str)
|
||||
|
||||
def testCheckTypeOrNone(self)->None:
|
||||
check_type(None, int, or_none=True)
|
||||
with self.assertRaises(TypeError):
|
||||
check_type(None, int, or_none=False)
|
||||
|
||||
def testValidStringValid(self)->None:
|
||||
valid_string("David_Marchant", VALID_NAME_CHARS)
|
||||
@ -106,3 +113,48 @@ class CorrectnessTests(unittest.TestCase):
|
||||
def testValidListMinLength(self)->None:
|
||||
with self.assertRaises(ValueError):
|
||||
valid_list([1, 2, 3], str, min_length=10)
|
||||
|
||||
def testCheckImplementationMinimum(self)->None:
|
||||
class Parent:
|
||||
def func():
|
||||
pass
|
||||
|
||||
class Child(Parent):
|
||||
def func():
|
||||
pass
|
||||
|
||||
check_implementation(Child.func, Parent)
|
||||
|
||||
def testCheckImplementationUnaltered(self)->None:
|
||||
class Parent:
|
||||
def func():
|
||||
pass
|
||||
|
||||
class Child(Parent):
|
||||
pass
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
check_implementation(Child.func, Parent)
|
||||
|
||||
def testCheckImplementationDifferingSig(self)->None:
|
||||
class Parent:
|
||||
def func():
|
||||
pass
|
||||
|
||||
class Child(Parent):
|
||||
def func(var):
|
||||
pass
|
||||
|
||||
with self.assertRaises(NotImplementedError):
|
||||
check_implementation(Child.func, Parent)
|
||||
|
||||
def testCheckImplementationAnyType(self)->None:
|
||||
class Parent:
|
||||
def func(var:Any):
|
||||
pass
|
||||
|
||||
class Child(Parent):
|
||||
def func(var:str):
|
||||
pass
|
||||
|
||||
check_implementation(Child.func, Parent)
|
||||
|
Reference in New Issue
Block a user