Exemplo n.º 1
0
def test_connect() -> None:
    ref = Reference

    instance_id = Reference('kernel')
    conduits = [
        Conduit('kernel.out', 'other.in'),
        Conduit('other.out', 'kernel.in')
    ]
    peer_dims = {ref('other'): [1]}
    peer_locations = {ref('other'): ['direct:test']}

    with patch('libmuscle.communicator.PeerManager') as pm_init:
        communicator = Communicator(instance_id, [13], None, MagicMock())

        communicator.connect(conduits, peer_dims, peer_locations)

        pm_init.assert_called_with(instance_id, [13], conduits, peer_dims,
                                   peer_locations)

        # check inferred ports
        ports = communicator._ports
        communicator.shutdown()
        assert ports['in'].name == Identifier('in')
        assert ports['in'].operator == Operator.F_INIT
        assert ports['in']._length is None

        assert ports['out'].name == Identifier('out')
        assert ports['out'].operator == Operator.O_F
        assert ports['out']._length is None
Exemplo n.º 2
0
def test_connect_inferred_ports(communicator) -> None:
    ref = Reference

    communicator._declared_ports = None

    conduits = [
        Conduit('other1.out', 'kernel.in'),
        Conduit('kernel.out1', 'other.in'),
        Conduit('kernel.out3', 'other2.in')
    ]
    peer_dims = {ref('other1'): [20, 7], ref('other'): [25], ref('other2'): []}
    peer_locations = {
        ref('other'): ['direct:test'],
        ref('other1'): ['direct:test1'],
        ref('other2'): ['direct:test2']
    }

    communicator.connect(conduits, peer_dims, peer_locations)

    ports = communicator._ports
    assert ports['in'].name == Identifier('in')
    assert ports['in'].operator == Operator.F_INIT
    assert ports['in']._length == 7
    assert ports['in']._is_resizable is False

    assert ports['out1'].name == Identifier('out1')
    assert ports['out1'].operator == Operator.O_F
    assert ports['out1']._length is None

    assert ports['out3'].name == Identifier('out3')
    assert ports['out3'].operator == Operator.O_F
    assert ports['out3']._length is None
Exemplo n.º 3
0
def test_create_identifier() -> None:
    part = Identifier('testing')
    assert str(part) == 'testing'

    part = Identifier('CapiTaLs')
    assert str(part) == 'CapiTaLs'

    part = Identifier('under_score')
    assert str(part) == 'under_score'

    part = Identifier('_underscore')
    assert str(part) == '_underscore'

    part = Identifier('digits123')
    assert str(part) == 'digits123'

    with pytest.raises(ValueError):
        Identifier('1initialdigit')

    with pytest.raises(ValueError):
        Identifier('test.period')

    with pytest.raises(ValueError):
        Identifier('test-hyphen')

    with pytest.raises(ValueError):
        Identifier('test space')

    with pytest.raises(ValueError):
        Identifier('test/slash')
Exemplo n.º 4
0
def test_endpoint_instance() -> None:
    endpoint = Endpoint(Reference('test.kernel'), [42], Identifier('port'),
                        [2])
    assert endpoint.instance() == 'test.kernel[42]'

    endpoint2 = Endpoint(Reference('test.kernel'), [], Identifier('port'), [])
    assert endpoint2.instance() == 'test.kernel'

    endpoint3 = Endpoint(Reference('test.kernel'), [], Identifier('port'), [3])
    assert endpoint3.instance() == 'test.kernel'
Exemplo n.º 5
0
def test_reference_concatenation() -> None:
    assert Reference('test') + Reference('test2') == 'test.test2'
    assert Reference('test') + Identifier('test2') == 'test.test2'
    assert Reference('test') + 5 == 'test[5]'
    assert Reference('test') + [Identifier('test2'), 5] == 'test.test2[5]'

    assert Reference('test[5]') + Reference('test2[3]') == 'test[5].test2[3]'
    assert Reference('test[5]') + Identifier('test2') == 'test[5].test2'
    assert Reference('test[5]') + 3 == 'test[5][3]'
    assert (Reference('test[5]') +
            [3, Identifier('test2')] == 'test[5][3].test2')
Exemplo n.º 6
0
def test_compare_identifier() -> None:
    assert Identifier('test') == Identifier('test')
    assert Identifier('test1') != Identifier('test2')

    assert Identifier('test') == 'test'
    assert 'test' == Identifier('test')  # pylint: disable=C0122
    assert Identifier('test') != 'test2'
    assert 'test2' != Identifier('test')  # pylint: disable=C0122
Exemplo n.º 7
0
    def __init__(self, name: str, operator: Operator, is_vector: bool,
                 is_connected: bool, our_ndims: int, peer_dims: List[int]
                 ) -> None:
        """Create a Port.

        Args:
            name: Name of this port.
            operator: Corresponding operator.
            is_vector: Whether this is a vector port.
            is_connected: Whether this port is connected to a peer.
            our_ndims: Number of dimensions of our instance set.
            peer_dims: Dimensions of the peer instance set of this port.
        """
        super().__init__(Identifier(name), operator)

        self._is_connected = is_connected

        if is_vector:
            if our_ndims == len(peer_dims):
                self._length = 0    # type: Optional[int]
            elif our_ndims + 1 == len(peer_dims):
                self._length = peer_dims[-1]
            elif our_ndims > len(peer_dims):
                raise RuntimeError(('Vector port "{}" is connected to an'
                                    ' instance set with fewer dimensions.'
                                    ' It should be connected to a scalar'
                                    ' port on a set with one more dimension,'
                                    ' or to a vector port on a set with the'
                                    ' same number of dimensions.').format(
                                        name))
            else:
                raise RuntimeError(('Port "{}" is connected to an instance set'
                                    ' with more than one dimension more than'
                                    ' its own, which is not possible.').format(
                                        name))
            self._is_open = [True] * self._length
        else:
            if our_ndims < len(peer_dims):
                raise RuntimeError(('Scalar port "{}" is connected to an'
                                    ' instance set with more dimensions.'
                                    ' It should be connected to a scalar'
                                    ' port on an instance set with the same'
                                    ' dimensions, or to a vector port on an'
                                    ' instance set with one less dimension.'
                                    ).format(name))
            elif our_ndims > len(peer_dims) + 1:
                raise RuntimeError(('Scalar port "{}" is connected to an'
                                    ' instance set with at least two fewer'
                                    ' dimensions, which is not possible.'
                                    ).format(name))
            self._length = None
            self._is_open = [True]

        self._is_resizable = is_vector and (our_ndims == len(peer_dims))
Exemplo n.º 8
0
def test_endpoint() -> None:
    kernel = Reference('test.kernel')
    index = [42]
    port = Identifier('out')
    slot = [2]

    endpoint = Endpoint(kernel, index, port, slot)
    assert endpoint.kernel == kernel
    assert endpoint.index == index
    assert endpoint.port == port
    assert endpoint.slot == slot

    assert str(endpoint) == 'test.kernel[42].out[2]'
Exemplo n.º 9
0
    def __list_declared_ports(self) -> List[Port]:
        """Returns a list of declared ports.

        This returns a list of ymmsl.Port objects, which have only the
        name and the operator, not libmuscle.Port, which has more.
        """
        result = list()
        if self._declared_ports is not None:
            for operator, port_names in self._declared_ports.items():
                for name in port_names:
                    if name.endswith('[]'):
                        name = name[:-2]
                    result.append(Port(Identifier(name), operator))
        return result
Exemplo n.º 10
0
    def __get_endpoint(self, port_name: str, slot: List[int]) -> Endpoint:
        """Determines the endpoint on our side.

        Args:
            port_name: Name of the port to send or receive on.
            slot: Slot to send or receive on.
        """
        try:
            port = Identifier(port_name)
        except ValueError as e:
            raise ValueError('"{}" is not a valid port name: {}'.format(
                port_name, e))

        return Endpoint(self._kernel, self._index, port, slot)
Exemplo n.º 11
0
 def __ports_from_declared(self) -> Dict[str, Port]:
     """Derives port definitions from supplied declaration.
     """
     ports = dict()
     declared_ports = cast(Dict[Operator, List[str]], self._declared_ports)
     for operator, port_list in declared_ports.items():
         for port_desc in port_list:
             port_name, is_vector = self.__split_port_desc(port_desc)
             if port_name.startswith('muscle_'):
                 raise RuntimeError(('Port names starting with "muscle_"'
                                     ' are reserved for MUSCLE, please'
                                     ' rename port "{}"'.format(port_name)))
             port_id = Identifier(port_name)
             is_connected = self._peer_manager.is_connected(port_id)
             if is_connected:
                 peer_port = self._peer_manager.get_peer_port(port_id)
                 peer_ce = cast(Reference, peer_port[:-1])
                 port_peer_dims = self._peer_manager.get_peer_dims(peer_ce)
             else:
                 port_peer_dims = []
             ports[port_name] = Port(port_name, operator,
                                     is_vector, is_connected,
                                     len(self._index), port_peer_dims)
     return ports
Exemplo n.º 12
0
def test_port() -> None:
    ep1 = Port(Identifier('test_in'), Operator.F_INIT)

    assert str(ep1.name) == 'test_in'
    assert ep1.operator == Operator.F_INIT
Exemplo n.º 13
0
def port_from_grpc(port: mmp.Port) -> ymmsl.Port:
    return ymmsl.Port(
            Identifier(port.name),
            operator_from_grpc(port.operator))
Exemplo n.º 14
0
def test_create_reference() -> None:
    test_ref = Reference('_testing')
    assert str(test_ref) == '_testing'
    assert len(test_ref) == 1
    assert isinstance(test_ref[0], Identifier)
    assert str(test_ref[0]) == '_testing'

    with pytest.raises(ValueError):
        Reference('1test')

    test_ref = Reference('test.testing')
    assert len(test_ref) == 2
    assert isinstance(test_ref[0], Identifier)
    assert str(test_ref[0]) == 'test'
    assert isinstance(test_ref[1], Identifier)
    assert str(test_ref[1]) == 'testing'
    assert str(test_ref) == 'test.testing'

    test_ref = Reference('test[12]')
    assert len(test_ref) == 2
    assert isinstance(test_ref[0], Identifier)
    assert str(test_ref[0]) == 'test'
    assert isinstance(test_ref[1], int)
    assert test_ref[1] == 12
    assert str(test_ref) == 'test[12]'

    test_ref = Reference('test[12].testing.ok.index[3][5]')
    assert len(test_ref) == 7
    assert isinstance(test_ref[0], Identifier)
    assert str(test_ref[0]) == 'test'
    assert isinstance(test_ref[1], int)
    assert test_ref[1] == 12
    assert isinstance(test_ref[2], Identifier)
    assert str(test_ref[2]) == 'testing'
    assert isinstance(test_ref[3], Identifier)
    assert str(test_ref[3]) == 'ok'
    assert isinstance(test_ref[4], Identifier)
    assert str(test_ref[4]) == 'index'
    assert isinstance(test_ref[5], int)
    assert test_ref[5] == 3
    assert isinstance(test_ref[6], int)
    assert test_ref[6] == 5
    assert str(test_ref) == 'test[12].testing.ok.index[3][5]'

    with pytest.raises(ValueError):
        Reference([4])

    with pytest.raises(ValueError):
        Reference([3, Identifier('test')])

    with pytest.raises(ValueError):
        Reference('ua",.u8[')

    with pytest.raises(ValueError):
        Reference('test[4')

    with pytest.raises(ValueError):
        Reference('test4]')

    with pytest.raises(ValueError):
        Reference('test[_t]')

    with pytest.raises(ValueError):
        Reference('testing_{3}')

    with pytest.raises(ValueError):
        Reference('test.(x)')

    with pytest.raises(ValueError):
        Reference('[3]test')

    with pytest.raises(ValueError):
        Reference('[4].test')
Exemplo n.º 15
0
def test_identifier_dict_key() -> None:
    test_dict = {Identifier('test'): 1}
    assert test_dict[Identifier('test')] == 1