Exemplo n.º 1
0
    def setUp(self):
        self.p = Process('process1', SyncFactory('threading'))

        # create a child ManagerController block
        params = ManagerController.MethodMeta. \
            prepare_input_map(mri='childBlock', configDir="/tmp")
        self.c_child = ManagerController(self.p, [], params)
        self.b_child = self.c_child.block

        self.sm = self.c_child.stateMachine

        params = Part.MethodMeta.prepare_input_map(name='part1')
        part1 = Part(self.p, params)
        params = {'name': 'part2', 'mri': 'childBlock'}
        params = ChildPart.MethodMeta.prepare_input_map(**params)
        part2 = ChildPart(self.p, params)

        # create a root block for the ManagerController block to reside in
        parts = [part1, part2]
        params = ManagerController.MethodMeta.prepare_input_map(
            mri='mainBlock', configDir="/tmp")
        self.c = ManagerController(self.p, parts, params)
        self.b = self.c.block

        # check that do_initial_reset works asynchronously
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.READY, timeout=1)

        self.checkState(self.sm.READY)
    def setUp(self):
        self.p = Process("process1", SyncFactory("threading"))

        # create a child ManagerController block
        params = ManagerController.MethodMeta.prepare_input_map(mri="childBlock")
        self.c_child = ManagerController(self.p, [], params)
        self.b_child = self.c_child.block

        self.sm = self.c_child.stateMachine

        params = Part.MethodMeta.prepare_input_map(name="part1")
        part1 = Part(self.p, params)
        params = {"name": "part2", "mri": "childBlock"}
        params = ChildPart.MethodMeta.prepare_input_map(**params)
        part2 = ChildPart(self.p, params)

        # create a root block for the ManagerController block to reside in
        parts = [part1, part2]
        params = {"mri": "mainBlock"}
        params = ManagerController.MethodMeta.prepare_input_map(**params)
        self.c = ManagerController(self.p, parts, params)
        self.b = self.c.block

        # check that do_initial_reset works asynchronously
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.READY, timeout=1)

        self.checkState(self.sm.READY)
Exemplo n.º 3
0
def PandABox(process, params):

    # Connect to the Control port
    control = PandABoxControl(process, params.hostname, params.port)
    control.start()

    # Create a block updater
    poller = PandABoxPoller(process, control)

    # Get some information about what is available in this PandABox
    blocks_data = control.get_blocks_data()
    parts = OrderedDict()
    ret = []

    for block_name, block_data in blocks_data.items():
        block_names = []
        if block_data.number == 1:
            block_names.append(block_name)
        else:
            for i in range(block_data.number):
                block_names.append("%s%d" % (block_name, i+1))
        for bn in block_names:
            malcolm_name = "%s:%s" % (params.name, bn)
            ret.append(poller.make_panda_block(malcolm_name, bn, block_data))
            part_params = LayoutPart.MethodMeta.prepare_input_map(
                dict(name=bn, child=malcolm_name))
            parts[bn] = LayoutPart(process, part_params)

    # Make a controller
    controller = ManagerController(params.name, process, parts)
    ret.append(controller.block)

    # Start polling
    poller.start()

    return ret
Exemplo n.º 4
0
class TestManagerController(unittest.TestCase):
    maxDiff = None

    def checkState(self, state, child=True, parent=True):
        if child:
            self.assertEqual(self.c_child.state.value, state)
        if parent:
            self.assertEqual(self.c.state.value, state)

    def setUp(self):
        self.p = Process('process1', SyncFactory('threading'))

        # create a child ManagerController block
        params = ManagerController.MethodMeta. \
            prepare_input_map(mri='childBlock', configDir="/tmp")
        self.c_child = ManagerController(self.p, [], params)
        self.b_child = self.c_child.block

        self.sm = self.c_child.stateMachine

        params = Part.MethodMeta.prepare_input_map(name='part1')
        part1 = Part(self.p, params)
        params = {'name': 'part2', 'mri': 'childBlock'}
        params = ChildPart.MethodMeta.prepare_input_map(**params)
        part2 = ChildPart(self.p, params)

        # create a root block for the ManagerController block to reside in
        parts = [part1, part2]
        params = ManagerController.MethodMeta.prepare_input_map(
            mri='mainBlock', configDir="/tmp")
        self.c = ManagerController(self.p, parts, params)
        self.b = self.c.block

        # check that do_initial_reset works asynchronously
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.READY, timeout=1)

        self.checkState(self.sm.READY)

    def tearDown(self):
        self.p.stop()

    def test_init(self):

        # the following block attributes should be created by a call to
        # set_attributes via _set_block_children in __init__
        self.assertEqual(self.b['layout'].meta.typeid,
                         'malcolm:core/TableMeta:1.0')
        self.assertEqual(self.b['layoutName'].meta.typeid,
                         'malcolm:core/ChoiceMeta:1.0')

        # the following hooks should be created via _find_hooks in __init__
        self.assertEqual(
            self.c.hook_names, {
                self.c.Reset: "Reset",
                self.c.Disable: "Disable",
                self.c.Layout: "Layout",
                self.c.ReportOutports: "ReportOutports",
                self.c.Load: "Load",
                self.c.Save: "Save",
            })

        # check instantiation of object tree via logger names
        self.assertEqual(self.c._logger.name, 'ManagerController(mainBlock)')
        self.assertEqual(self.c.parts['part1']._logger.name,
                         'ManagerController(mainBlock).part1')
        self.assertEqual(self.c.parts['part2']._logger.name,
                         'ManagerController(mainBlock).part2')
        self.assertEqual(self.c_child._logger.name,
                         'ManagerController(childBlock)')

    def test_edit(self):
        structure = MagicMock()
        self.c.load_structure = structure
        self.c.edit()
        # editing only affects one level
        self.checkState(self.sm.EDITABLE, child=False)
        self.assertEqual(self.c.load_structure, structure)

    def test_edit_exception(self):
        self.c.edit()
        with self.assertRaises(Exception):
            self.c.edit()

    def check_expected_save(self, x=0.0, y=0.0, visible="false"):
        expected = [
            x.strip() for x in ("""{
          "layout": {
            "part2": {
              "x": %s,
              "y": %s,
              "visible": %s
            }
          },
          "part2": {}
        }""" % (x, y, visible)).splitlines()
        ]
        actual = [
            x.strip()
            for x in open("/tmp/mainBlock/testSaveLayout.json").readlines()
        ]
        self.assertEqual(actual, expected)

    def test_save(self):
        self.c.edit()
        params = {'layoutName': 'testSaveLayout'}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)
        self.check_expected_save()
        self.checkState(self.sm.AFTER_RESETTING, child=False)
        self.assertEqual(self.c.layout_name.value, 'testSaveLayout')
        os.remove("/tmp/mainBlock/testSaveLayout.json")
        self.c.edit()
        params = {'layoutName': None}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)
        self.check_expected_save()
        self.assertEqual(self.c.layout_name.value, 'testSaveLayout')

    def move_child_block(self):
        self.assertEqual(self.b.layout.x, [0])
        new_layout = Table(self.c.layout.meta)
        new_layout.name = ["part2"]
        new_layout.mri = ["P45-MRI"]
        new_layout.x = [10]
        new_layout.y = [20]
        new_layout.visible = [True]
        self.b.layout = new_layout
        self.assertEqual(self.b.layout.x, [10])

    def test_move_child_block_dict(self):
        self.b.edit()
        self.assertEqual(self.b.layout.x, [0])
        new_layout = dict(name=["part2"],
                          mri=[""],
                          x=[10],
                          y=[20],
                          visible=[True])
        self.b.layout = new_layout
        self.assertEqual(self.b.layout.x, [10])

    def test_revert(self):
        self.c.edit()
        self.move_child_block()
        self.assertEqual(self.b.layout.x, [10])
        self.c.revert()
        self.assertEqual(self.b.layout.x, [0])
        self.checkState(self.sm.AFTER_RESETTING, child=False)

    def test_set_and_load_layout(self):
        self.c.edit()
        self.checkState(self.sm.EDITABLE, child=False)

        new_layout = Table(self.c.layout.meta)
        new_layout.name = ["part2"]
        new_layout.mri = ["P45-MRI"]
        new_layout.x = [10]
        new_layout.y = [20]
        new_layout.visible = [True]
        self.b.layout = new_layout
        self.assertEqual(self.c.parts['part2'].x, 10)
        self.assertEqual(self.c.parts['part2'].y, 20)
        self.assertEqual(self.c.parts['part2'].visible, True)

        # save the layout, modify and restore it
        params = {'layoutName': 'testSaveLayout'}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)
        self.check_expected_save(10.0, 20.0, "true")

        self.c.parts['part2'].x = 30
        self.b.layoutName = 'testSaveLayout'
        self.assertEqual(self.c.parts['part2'].x, 10)
class TestManagerController(unittest.TestCase):
    def checkState(self, state, child=True, parent=True):
        if child:
            self.assertEqual(self.c_child.state.value, state)
        if parent:
            self.assertEqual(self.c.state.value, state)

    def setUp(self):
        self.p = Process("process1", SyncFactory("threading"))

        # create a child ManagerController block
        params = ManagerController.MethodMeta.prepare_input_map(mri="childBlock")
        self.c_child = ManagerController(self.p, [], params)
        self.b_child = self.c_child.block

        self.sm = self.c_child.stateMachine

        params = Part.MethodMeta.prepare_input_map(name="part1")
        part1 = Part(self.p, params)
        params = {"name": "part2", "mri": "childBlock"}
        params = ChildPart.MethodMeta.prepare_input_map(**params)
        part2 = ChildPart(self.p, params)

        # create a root block for the ManagerController block to reside in
        parts = [part1, part2]
        params = {"mri": "mainBlock"}
        params = ManagerController.MethodMeta.prepare_input_map(**params)
        self.c = ManagerController(self.p, parts, params)
        self.b = self.c.block

        # check that do_initial_reset works asynchronously
        self.p.start()

        # wait until block is Ready
        task = Task("block_ready_task", self.p)
        task.when_matches(self.b["state"], self.sm.READY, timeout=1)

        self.checkState(self.sm.READY)

    def tearDown(self):
        self.p.stop()

    def test_init(self):

        # the following block attributes should be created by a call to
        # set_attributes via _set_block_children in __init__
        self.assertEqual(self.b["layout"].meta.typeid, "malcolm:core/TableMeta:1.0")
        self.assertEqual(self.b["layoutName"].meta.typeid, "malcolm:core/StringMeta:1.0")

        # the following hooks should be created via _find_hooks in __init__
        self.assertEqual(
            self.c.hook_names,
            {
                self.c.Reset: "Reset",
                self.c.Disable: "Disable",
                self.c.Layout: "Layout",
                self.c.ReportOutports: "ReportOutports",
                self.c.Load: "Load",
                self.c.Save: "Save",
            },
        )

        # check instantiation of object tree via logger names
        self.assertEqual(self.c._logger.name, "ManagerController(mainBlock)")
        self.assertEqual(self.c.parts["part1"]._logger.name, "ManagerController(mainBlock).part1")
        self.assertEqual(self.c.parts["part2"]._logger.name, "ManagerController(mainBlock).part2")
        self.assertEqual(self.c_child._logger.name, "ManagerController(childBlock)")

    def test_edit(self):
        self.c.edit()
        # editing only affects one level
        self.checkState(self.sm.EDITABLE, child=False)
        self.assertEqual(self.c.revert_structure, self.c._save_to_structure())

    def test_edit_exception(self):
        self.c.edit()
        with self.assertRaises(Exception):
            self.c.edit()

    def test_save(self):
        self.c.edit()
        params = {"layoutName": "testSaveLayout"}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)
        self.checkState(self.sm.AFTER_RESETTING, child=False)
        self.assertEqual(self.c.layout_name.value, "testSaveLayout")
        self.c.edit()
        params = {"layoutName": None}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)

    def test_revert(self):
        self.c.edit()
        self.c.revert()
        self.checkState(self.sm.AFTER_RESETTING, child=False)

    def test_set_and_load_layout(self):
        self.c.edit()
        self.checkState(self.sm.EDITABLE, child=False)

        new_layout = Table(self.c.layout.meta)
        new_layout.name = ["part2"]
        new_layout.mri = ["P45-MRI"]
        new_layout.x = [10]
        new_layout.y = [20]
        new_layout.visible = [True]
        self.b.layout = new_layout
        self.assertEqual(self.c.parts["part2"].x, 10)
        self.assertEqual(self.c.parts["part2"].y, 20)
        self.assertEqual(self.c.parts["part2"].visible, True)

        # save the layout, modify and restore it
        params = {"layoutName": "testSaveLayout"}
        params = ManagerController.save.MethodMeta.prepare_input_map(**params)
        self.c.save(params)

        self.c.edit()
        new_layout.x = [30]
        self.b.layout = new_layout
        self.assertEqual(self.c.parts["part2"].x, 30)
        self.b.layoutName = "testSaveLayout"
        self.assertEqual(self.c.parts["part2"].x, 10)
 def setUp(self):
     self.c = ManagerController('block', MagicMock())
     self.b = self.c.block