Exemplo n.º 1
0
class MapPyDoNothingTestCase(unittest.TestCase):#pylint: disable =R0904
    """MapPyDoNothingTestCase"""
    @classmethod
    def setUpClass(self):#pylint: disable =C0103,C0202
        """setUpClass"""
        self.mapper = MapPyDoNothing()
        success = self.mapper.birth("{}")
        if not success:
            raise Exception('InitializeFail', 'Could not start worker')

        # more like real documents
        self.testString = """{"mc": [{"hits": [{"energy_deposited": 0.037781369897893102, "energy": 315.10944148506519, "volume_name": "Stage6.dat/TrackerSolenoid0.dat/Tracker0.dat/TrackerStation1.dat/TrackerViewW.datDoubletCores", "pid": 13, "track_id": 1, "channel_id": {"tracker_number": 0, "station_number": 1, "type": "Tracker", "plane_number": 2, "fiber_number": 107}, "charge": -1.0, "mass": 105.6584, "time": 1.2404951395988231}], "energy": 210, "unit_momentum": {"y": 0, "x": 0, "z": 1}, "particle_id": 13, "tracks": {"track1": {"parent_track_id": 1, "initial_momentum": {"y": -0.013085532209047269, "x": 0.045481821695065712, "z": 0.0023252953102620381}, "particle_id": 11, "track_id": 2, "final_position": {"y": -0.068452789962020408, "x": 0.1093100432810042, "z": -4912.1539662246769}, "initial_position": {"y": -0.1066636016684737, "x": 0.10807384560972209, "z": -4912.1943706252696}, "steps": [{"energy_deposited": 0.0021748808862488752, "position": {"y": -0.068452789962020408, "x": 0.1093100432810042, "z": -4912.1539662246769}, "momentum": {"y": -0.0, "x": -0.0, "z": 0.0}}], "final_momentum": {"y": -0.0, "x": -0.0, "z": 0.0}}}, "position": {"y": -0.10000000000000001, "x": 0.10000000000000001, "z": -5000}}]}"""#pylint: disable =C0301

    def test_empty(self):
        """test_empty"""
        result = self.mapper.process("")
        self.assertTrue(result == "")

    def test_document(self):
        """test_document"""
        result = self.mapper.process(self.testString)
        doc = json.loads(result)
        doc2 = json.loads(self.testString)
        self.assertTrue(doc == doc2)

    @classmethod
    def tearDownClass(self):#pylint: disable =C0103,C0202
        """tearDownClass"""
        success = self.mapper.death()
        if not success:
            raise Exception('InitializeFail', 'Could not start worker')
        self.mapper = None
Exemplo n.º 2
0
 def test_get_worker_names(self):
     """
     Test get_worker_names.
     @param self Object reference.
     """
     # Create nested group.
     workers = [MapPyTestMap(), MapPyDoNothing(), MapPyPrint()]
     group = MapPyGroup(workers)
     group = MapPyGroup([MapPyDoNothing(), group, MapPyTestMap()])
     workers = [
         "MapPyDoNothing", ["MapPyTestMap", "MapPyDoNothing", "MapPyPrint"],
         "MapPyTestMap"
     ]
     names = group.get_worker_names()
     self.assertEquals(len(workers), len(names),
                       "Unexpected number of names")
     # This does a start-to-end comparison of the lists.
     while len(workers) != 0:
         expected = workers.pop(0)
         actual = names.pop(0)
         if isinstance(actual, ListType):
             self.assertEquals(len(expected), len(actual),
                               "Unexpected number of names in sub-list")
             expected.extend(workers)
             workers = expected
             names = actual.extend(names)
             names = actual
         else:
             self.assertEquals(expected, actual, "Unexpected name")
Exemplo n.º 3
0
 def test_death_false(self):
     """
     Test calling death where one worker's death function fails.
     @param self Object reference.
     """
     workers = [MapPyDoNothing(), MapPyDoNothing(), MapPyTestMap()]
     group = MapPyGroup(workers)
     # Force a worker in group to fail
     workers[2].birth("""{"death_result":%s}""" % MapPyTestMap.FAIL)
     result = group.death()
     self.assertFalse(result, "death unexpectedly returned True")
Exemplo n.º 4
0
 def test_birth_exception(self):
     """
     Test calling birth where one worker's birth function 
     throws an exception.
     @param self Object reference.
     """
     workers = [MapPyDoNothing(), MapPyDoNothing(), MapPyTestMap()]
     group = MapPyGroup(workers)
     # Force a worker in group to fail
     result = group.birth("""{"birth_result":%s}""" % \
         MapPyTestMap.EXCEPTION)
     self.assertFalse(result, "birth unexpectedly returned True")
Exemplo n.º 5
0
 def test_death_exception_raise(self):
     """
     Test calling death where one worker's death function 
     throws an exception and the ErrorHandler is set to
     "raise"
     @param self Object reference.
     """
     ErrorHandler.DefaultHandler().on_error = 'raise'
     workers = [MapPyDoNothing(), MapPyDoNothing(), MapPyTestMap()]
     group = MapPyGroup(workers)
     # Force a worker in group to fail
     workers[2].birth("""{"death_result":%s}""" % MapPyTestMap.EXCEPTION)
     with self.assertRaisesRegexp(MapPyGroupDeathException, ".*"):
         group.death()
Exemplo n.º 6
0
    def test_command_line_args(self):
        """
        Check that Go handles command line args switch correctly.
        @param self. Object reference.
        """
        inputer = InputPyEmptyDocument(1)
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = OutputPyJSON(open(self.tmp_file, 'w'))
        arg_temp = copy.deepcopy(sys.argv)
        sys.argv = [arg_temp[0]]

        Go(inputer, transformer, merger, outputer, config_file=self.config, \
                                                      command_line_args = True)

        outputer = OutputPyJSON(open(self.tmp_file, 'w'))
        arg_temp = copy.deepcopy(sys.argv)
        sys.argv = [arg_temp[0], "bob"]
        with self.assertRaises(SystemExit):
            Go(inputer, transformer, merger, outputer, \
                              config_file=self.config, command_line_args = True)

        sys.argv = [arg_temp[0], "-verbose_level", "1"]
        Go(inputer, transformer, merger, outputer, \
                             config_file=self.config, command_line_args = True)

        sys.argv = arg_temp
Exemplo n.º 7
0
    def test_birth_death_exception(self):
        """
        Test calling birth where one worker's birth function 
        throws an exception and other worker's death functions
        also throw exceptions.
        @param self Object reference.
        """
        ErrorHandler.DefaultHandler().on_error = 'raise'

        # Create worker that fails on death.
        class TestWorker:  # pylint:disable = C0111, W0232
            def birth(self, json_document):
                pass

            def process(self, spill):
                pass

            def death(self):  # pylint:disable = R0201
                raise ValueError("Test")

        workers = [MapPyDoNothing(), TestWorker(), MapPyTestMap()]
        group = MapPyGroup(workers)
        # Force 3rd worker to fail on birth.
        with self.assertRaisesRegexp(MapPyGroupBirthException, ".*"):
            group.birth("""{"birth_result":%s}""" % \
                MapPyTestMap.EXCEPTION)
Exemplo n.º 8
0
    def test_input_birth(self):
        """
        Check that Go raises error with bad input.
        @param self. Object reference.
        """
        inputer = FakeWorker()
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = OutputPyJSON(open(self.tmp_file, 'w'))

        with self.assertRaises(AssertionError):
            Go(inputer, transformer, merger, outputer, \
                              config_file=self.config, command_line_args=False)
Exemplo n.º 9
0
    def test_output_birth(self):
        """
        Check that Go raises error with bad outputter.
        @param self. Object reference.
        """
        inputer = InputPyEmptyDocument(1)
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = FakeWorker()

        with self.assertRaises(AssertionError):
            Go(inputer, transformer, merger, outputer, \
                               config_file=self.config, command_line_args=False)
Exemplo n.º 10
0
 def test_init_list(self):
     """
     Test with constructor given an initial list of workers.
     @param self Object reference.
     """
     workers = [MapPyTestMap(), MapPyDoNothing(), MapPyPrint()]
     group = MapPyGroup(workers)
     names = group.get_worker_names()
     self.assertEquals(len(workers), len(names),
                       "Unexpected number of workers")
     for i in range(len(workers)):
         self.assertEquals(workers[i].__class__.__name__, names[i],
                           "Unexpected name")
Exemplo n.º 11
0
 def test_dataflow_multi_process(self):
     """
     Check that Go executes okay with multi_process dataflow.
     @param self. Object reference.
     """
     inputer = InputPyEmptyDocument(1)
     transformer = MapPyDoNothing()
     merger = ReducePyDoNothing()
     outputer = OutputPyJSON(open(self.tmp_file, 'w'))
     for map_red_type in ['multi_process']:
         config = StringIO(u"""type_of_dataflow="%s" """ % map_red_type)
         with self.assertRaises(Exception):
             Go(inputer, transformer, merger, outputer, config, \
                    command_line_args = False)
Exemplo n.º 12
0
    def test_dataflow_single_thread(self):
        """
        Check that Go executes okay with pipeline_single_thread dataflow.
        @param self. Object reference.
        """
        inputer = InputPyEmptyDocument(1)
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = OutputPyJSON(open(self.tmp_file, 'w'))
        for map_red_type in ['pipeline_single_thread']:
            config = StringIO(u"""type_of_dataflow="%s" """ % map_red_type)

            Go(inputer, transformer, merger, outputer, config, \
                   command_line_args = False)
Exemplo n.º 13
0
    def test_dataflow_not_implemented(self):
        """
        Check that Go notifies user of unimplemented dataflow.
        @param self. Object reference.
        """
        inputer = InputPyEmptyDocument(1)
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = OutputPyJSON(open(self.tmp_file, 'w'))
        for map_red_type in ['many_local_threads']:
            config = StringIO(u"""type_of_dataflow="%s" """ % map_red_type)

            with self.assertRaises(NotImplementedError):
                Go(inputer, transformer, merger, outputer, config, \
                       command_line_args = False)
Exemplo n.º 14
0
    def test_type_of_dataflow_bad(self):
        """
        Check that Go raises error with bad dataflow type.
        @param self. Object reference.
        """
        inputer = InputPyEmptyDocument(1)
        transformer = MapPyDoNothing()
        merger = ReducePyDoNothing()
        outputer = OutputPyJSON(open(self.tmp_file, 'w'))

        config = StringIO(u"""type_of_dataflow="bad_type" """)

        with self.assertRaises(LookupError):
            Go(inputer, transformer, merger, outputer, config, \
               command_line_args = False)