| 1 |
# coding: utf-8 |
| 2 |
# /*########################################################################## |
| 3 |
# |
| 4 |
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
# ###########################################################################*/ |
| 25 |
|
| 26 |
__authors__ = ["H. Payno"] |
| 27 |
__license__ = "MIT" |
| 28 |
__date__ = "02/11/2020" |
| 29 |
|
| 30 |
|
| 31 |
import unittest |
| 32 |
from processview.core.superviseprocess import SuperviseProcess |
| 33 |
from processview.core.manager import ProcessManager |
| 34 |
from processview.core.manager import DatasetState |
| 35 |
from processview.core.dataset import Dataset |
| 36 |
from processview.core.dataset import DatasetIdentifier |
| 37 |
import gc |
| 38 |
|
| 39 |
|
| 40 |
class TestProcessManager(unittest.TestCase): |
| 41 |
""" |
| 42 |
test ProcessManager |
| 43 |
""" |
| 44 |
|
| 45 |
def testRegisterProcess(self): |
| 46 |
"""insure registration and unregistration are working""" |
| 47 |
manager = ProcessManager() |
| 48 |
manager.clear() |
| 49 |
self.assertEqual(len(manager.get_processes()), 0) |
| 50 |
p1 = SuperviseProcess() |
| 51 |
self.assertEqual(len(manager.get_processes()), 1) |
| 52 |
p1 = None |
| 53 |
gc.collect() |
| 54 |
self.assertEqual(len(manager.get_processes()), 0) |
| 55 |
p1 = SuperviseProcess() |
| 56 |
p2 = SuperviseProcess() |
| 57 |
self.assertEqual(len(manager.get_processes()), 2) |
| 58 |
self.assertEqual(len(ProcessManager().get_processes()), 2) |
| 59 |
self.assertTrue(p1 in manager.get_processes()) |
| 60 |
self.assertTrue(p2 in manager.get_processes()) |
| 61 |
p1 = None |
| 62 |
p2 = None |
| 63 |
gc.collect() |
| 64 |
self.assertEqual(len(manager.get_processes()), 0) |
| 65 |
|
| 66 |
def testProcessStatesUpdate(self): |
| 67 |
"""insure providing states works well""" |
| 68 |
p1 = SuperviseProcess(name="sp1") |
| 69 |
p2 = SuperviseProcess(name="sp1") |
| 70 |
manager = ProcessManager() |
| 71 |
manager.clear() |
| 72 |
scan_1 = _DummyScan("scan1") |
| 73 |
scan_2 = _DummyScan("scan2") |
| 74 |
manager.notify_dataset_state( |
| 75 |
dataset=scan_1, state=DatasetState.PENDING, process=p1 |
| 76 |
) |
| 77 |
manager.notify_dataset_state( |
| 78 |
dataset=scan_2, state=DatasetState.SUCCEED, process=p1 |
| 79 |
) |
| 80 |
manager.notify_dataset_state( |
| 81 |
dataset=scan_2, state=DatasetState.FAILED, process=p2 |
| 82 |
) |
| 83 |
# test 'getScanState' |
| 84 |
self.assertEqual( |
| 85 |
manager.get_dataset_state(dataset_id=scan_2, process=p2), |
| 86 |
DatasetState.FAILED, |
| 87 |
) |
| 88 |
|
| 89 |
self.assertEqual(manager.get_dataset_state(dataset_id=scan_1, process=p2), None) |
| 90 |
|
| 91 |
# test 'getScanStream' |
| 92 |
self.assertEqual( |
| 93 |
manager.get_dataset_stream(dataset=scan_2, time_stamp=False), |
| 94 |
( |
| 95 |
(p1.process_id, DatasetState.SUCCEED), |
| 96 |
(p2.process_id, DatasetState.FAILED), |
| 97 |
), |
| 98 |
) |
| 99 |
|
| 100 |
# test 'getProcessHistory' |
| 101 |
self.assertEqual( |
| 102 |
manager.get_process_history(process=p1), |
| 103 |
( |
| 104 |
(scan_1.get_identifier(), DatasetState.PENDING), |
| 105 |
(scan_2.get_identifier(), DatasetState.SUCCEED), |
| 106 |
), |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
class _DummyScan(Dataset): |
| 111 |
def __init__(self, name): |
| 112 |
super().__init__() |
| 113 |
self.name = name |
| 114 |
|
| 115 |
def __str__(self) -> str: |
| 116 |
return self.name |
| 117 |
|
| 118 |
def get_identifier(self) -> DatasetIdentifier: |
| 119 |
return _DummyIdentifier(self) |
| 120 |
|
| 121 |
|
| 122 |
class _DummyIdentifier(DatasetIdentifier): |
| 123 |
def __init__(self, dataset): |
| 124 |
super().__init__(dataset, metadata={"name": dataset.name}) |
| 125 |
|
| 126 |
def __str__(self): |
| 127 |
return self.name() |
| 128 |
|
| 129 |
def __eq__(self, other): |
| 130 |
return self.name() == other.name() |
| 131 |
|
| 132 |
def __hash__(self): |
| 133 |
return hash(self.name()) |
| 134 |
|
| 135 |
|
| 136 |
def suite(): |
| 137 |
test_suite = unittest.TestSuite() |
| 138 |
for ui in (TestProcessManager,): |
| 139 |
test_suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(ui)) |
| 140 |
return test_suite |