示例#1
0
def test_echo_service():
    msg = 'testing'
    mock = PyrosMock()
    print("msg sent is {0}".format(msg))
    recv_msg = mock.service('random_service', msg)
    print("msg received is {0}".format(recv_msg))
    assert msg == recv_msg
示例#2
0
def test_other_topic():
    msg = 'testing'
    mock = PyrosMock()
    mock.topic('random_topic', msg)
    print("msg sent is {0}".format(msg))
    recv_msg = mock.topic('random_topic_2')
    print("msg received is {0}".format(recv_msg))
    assert recv_msg is None
示例#3
0
def test_echo_same_topic():
    msg = 'testing'
    mock = PyrosMock()
    mock.topic('random_topic', msg)
    print("msg sent is {0}".format(msg))
    recv_msg = mock.topic('random_topic')
    print("msg received is {0}".format(recv_msg))
    assert msg == recv_msg
示例#4
0
def test_mocknode_subscribers_detect_setup():  # Here we check that this node actually detects a topic upon setup
    mockn = PyrosMock()
    assert not mockn.is_alive()

    assert hasattr(mockn, 'topics')

    mockn.start()
    try:
        assert mockn.is_alive()

        with mock_subscriber_remote('test_topic', statusecho_topic):

            print("Discovering topics Service...")
            topics = pyzmp.discover("topics", 3)  # we wait a bit to let it time to start
            assert topics is not None
            assert len(topics.providers) == 1

            res = topics.call()
            assert not 'test_topic' in res  # topic not detected since not in list of exposed topics

            print("Discovering setup Service...")
            setup = pyzmp.discover("setup", 3)  # we wait a bit to let it time to start
            assert setup is not None
            assert len(setup.providers) == 1

            setup.call(kwargs={'services': [], 'topics': ['test_topic'], 'params': []})

            time.sleep(mockn.update_interval + 1)  # waiting for update to kick in

            res = topics.call()
            assert 'test_topic' in res
    finally:
        mockn.shutdown()
        assert not mockn.is_alive()
示例#5
0
def test_mocknode_provide_services():  # Here we check that this node actually provides all the services
    mockn = PyrosMock()
    assert not mockn.is_alive()

    assert hasattr(mockn, 'msg_build')
    assert hasattr(mockn, 'topic')
    assert hasattr(mockn, 'topics')
    assert hasattr(mockn, 'service')
    assert hasattr(mockn, 'services')
    assert hasattr(mockn, 'param')
    assert hasattr(mockn, 'params')
    assert hasattr(mockn, 'setup')

    mockn.start()
    try:
        assert mockn.is_alive()

        print("Discovering msg_build Service...")
        msg_build = pyzmp.discover("msg_build", 5)  # we wait a bit to let it time to start
        assert msg_build is not None
        assert len(msg_build.providers) == 1

        print("Discovering topic Service...")
        topic = pyzmp.discover("topic", 5)  # we wait a bit to let it time to start
        assert topic is not None
        assert len(topic.providers) == 1

        print("Discovering topics Service...")
        topic_list = pyzmp.discover("topics", 5)  # we wait a bit to let it time to start
        assert topic_list is not None
        assert len(topic_list.providers) == 1

        print("Discovering service Service...")
        service = pyzmp.discover("service", 5)  # we wait a bit to let it time to start
        assert service is not None
        assert len(service.providers) == 1

        print("Discovering services Service...")
        service_list = pyzmp.discover("services", 5)  # we wait a bit to let it time to start
        assert service_list is not None
        assert len(service_list.providers) == 1

        print("Discovering param Service...")
        param = pyzmp.discover("param", 5)  # we wait a bit to let it time to start
        assert param is not None
        assert len(param.providers) == 1

        print("Discovering params Service...")
        param_list = pyzmp.discover("params", 5)  # we wait a bit to let it time to start
        assert param_list is not None
        assert len(param_list.providers) == 1

        print("Discovering setup Service...")
        param_list = pyzmp.discover("setup", 5)  # we wait a bit to let it time to start
        assert param_list is not None
        assert len(param_list.providers) == 1
    finally:
        mockn.shutdown()
        assert not mockn.is_alive()
示例#6
0
def test_mocknode_creation_termination():
    mockn = PyrosMock()
    assert not mockn.is_alive()
    mockn.start()
    try:
        assert mockn.is_alive()
    finally:
        mockn.shutdown()
        assert not mockn.is_alive()
示例#7
0
def test_mocknode_subscribers_detect_throttled():
    """
    Testing that the mocknode detection of topics is throttled properly
    :return:
    """
    mockn = PyrosMock()
    assert not mockn.is_alive()

    assert hasattr(mockn, 'topics')

    mockn.update_interval = 5  # we wait 5 seconds between each update_throttled call
    mockn.start()  # one update will be triggered, and then nothing for the next 10 seconds
    try:
        assert mockn.is_alive()

        print("Discovering setup Service...")
        setup = pyzmp.discover("setup", 3)  # we wait a bit to let it time to start
        assert setup is not None
        assert len(setup.providers) == 1

        setup.call(kwargs={'services': [], 'topics': ['test_topic'], 'params': []})

        with mock_subscriber_remote('test_topic', statusecho_topic):

            print("Discovering topics Service...")
            topics = pyzmp.discover("topics", 3)  # we wait a bit to let it time to start
            assert topics is not None
            assert len(topics.providers) == 1

            # topic is very likely not detected yet ( we didn't wait after creating and exposing it )
            res = topics.call()
            assert not 'test_topic' in res

            time.sleep(mockn.update_interval + 1)  # make sure we let update time to kick in

            # topic has to be detected now
            res = topics.call()
            assert 'test_topic' in res

    finally:  # to make sure we clean up on failure
        mockn.shutdown()
        assert not mockn.is_alive()
示例#8
0
def test_mocknode_publishers_detect():  # Here we check that this node actually detects a topic
    mockn = PyrosMock(kwargs={
        'services': [],
        'publishers': ['test_topic'],
        'subscribers': [],
        'params': []
    })
    assert not mockn.is_alive()

    assert hasattr(mockn, 'topics')

    # starting the node
    mockn.start()

    # checking interface is still None here ( instantiated in child only )
    assert mockn.interface is None

    # Services are initialized in run() method of pyzmp.Node, after interface has been initialized
    try:
        assert mockn.is_alive()

        with mock_publisher_remote('test_topic', statusecho_topic):

            # asserting the mock system has done its job from our point of view at least
            assert 'test_topic' in topics_available_remote
            assert topics_available_type_remote['test_topic'] == statusecho_topic

            # Getting topics list from child process
            print("Discovering topics Service...")
            topics = pyzmp.discover("topics", 3)  # we wait a bit to let it time to start
            assert topics is not None
            assert len(topics.providers) == 1

            time.sleep(mockn.update_interval + 1)  # make sure we let update time to kick in
            # TODO : change timeout
            res = topics.call(recv_timeout=6000000)
            # the mock system should have done its job from the other process perspective too
            # via multiprocess manager list
            assert 'test_topic' in res  # topic detected since in list of exposed topics

    finally:
        mockn.shutdown()
        assert not mockn.is_alive()
示例#9
0
 def setup_class(cls):
     cls.mockInstance = PyrosMock()
     assert cls.mockInstance.start()
示例#10
0
def test_echo_service_default():
    msg = 'testing'
    mock = PyrosMock()
    assert mock.service('random_service') is None
示例#11
0
def test_echo_topic_default():
    mock = PyrosMock()
    recv_msg = mock.topic('random_topic')
    assert recv_msg is None
示例#12
0
def test_msg_build():
    msg = PyrosMock().msg_build('fake_connec_name')
    print("msg is of type {0}".format(type(msg)))
    assert isinstance(msg, str)
示例#13
0
 def setUp(self):
     self.mockInstance = PyrosMock()
     # setting up mockinterface instance
     cmd_conn = self.mockInstance.start()
     self.client = PyrosClient(cmd_conn)
示例#14
0
class TestPyrosClientOnMock(unittest.TestCase):
    def setUp(self):
        self.mockInstance = PyrosMock()
        # setting up mockinterface instance
        cmd_conn = self.mockInstance.start()
        self.client = PyrosClient(cmd_conn)

    def tearDown(self):
        self.mockInstance.shutdown()

    ### TOPICS ###

    # TODO : test list features more !
    def test_list_all(self):
        t = self.client.topics()
        # Make sure we get all mockinterface topics
        assert t is not None

    def test_inject_None(
            self):  # injecting None is meaningless and should return false
        assert self.client.topic_inject(
            'random_topic', None
        )  # simply check that it injected (default Empty since we support kwargs)

    def test_inject_Empty(self):
        assert self.client.topic_inject(
            'random_topic')  # simply check if injected

    #TODO : how to test strict backend with Mock ?
    #def test_inject_Wrong(self):
    #    with assert_raises(Exception) as expt:  # TODO : be more specific
    #        data = 42
    #        self.client.topic_inject('random_topic', data)  # simply check exception raised
    #    # assert_equal(expt, smthg...)

    def test_extract_None(self):
        assert self.client.topic_extract(
            'random_topic') is None  # simply check if nothing extracted

    def test_inject_extract_echo_Empty(self):
        assert self.client.topic_inject('random_topic')  # default should be {}
        print "injected message content {0}".format({})
        recv = self.client.topic_extract('random_topic')
        print "extracted message {0}".format(recv)
        assert recv == {}

    def test_inject_extract_echo_Simple_Arg(self):
        data = 'data_string'
        assert self.client.topic_inject('random_topic', data)
        print "injected message content {0}".format(data)
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == data

    def test_inject_extract_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        assert self.client.topic_inject('random_topic', data)
        print "injected message content {0}".format(data)
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == data

    def test_inject_extract_echo_Simple_KWArgs(self):
        assert self.client.topic_inject('random_topic', data='data_string')
        print "injected message content {0}".format("data='data_string'")
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == {'data': 'data_string'}

    def test_inject_extract_echo_Complex_KWArgs(self):
        assert self.client.topic_inject('random_topic',
                                        first='first_string',
                                        second='second_string')
        print "injected message content {0}".format(
            "first='first_string', second='second_string'")
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == {'first': 'first_string', 'second': 'second_string'}

    ### SERVICES ###
    # TODO : think how to test strict backend with Mock ?
    #def test_call_Wrong(self):
    #    with assert_raises(Exception) as expt:  # TODO : be more specific
    #        data = 42
    #        print "request content {0}".format(data)
    #        resp = self.client.service_call('random_service', data)  # simply check exception raised
    #    # assert_equal(expt, smthg...)

    def test_call_echo_Simple_Arg(self):
        data = 'data_string'
        print "request content {0}".format(data)
        resp = self.client.service_call('random_service', data)
        print "response content {0}".format(resp)
        assert resp == data

    def test_call_echo_None(self):
        print "request content {0}".format({})
        resp = self.client.service_call(
            'random_service', None
        )  # calling with None is interpreted as default (Empty) call (since we support kwargs)
        print "response content {0}".format(resp)
        assert resp == {}

    def test_call_echo_Empty(self):
        print "request content {0}".format({})
        resp = self.client.service_call('random_service')
        print "response content {0}".format(resp)
        assert resp == {}

    def test_call_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        print "request content {0}".format(data)
        resp = self.client.service_call('random_service', data)
        print "response content {0}".format(resp)
        assert resp == data

    def test_call_echo_Simple_KWArgs(self):
        print "request content {0}".format("data='data_string'")
        resp = self.client.service_call('random_service', data='data_string')
        print "response content {0}".format(resp)
        assert resp == {'data': 'data_string'}

    def test_call_echo_Complex_KWArgs(self):
        print "injected message content {0}".format(
            "first='first_string', second='second_string'")
        resp = self.client.service_call('random_service',
                                        first='first_string',
                                        second='second_string')
        print "extracted message content {0}".format(resp)
        assert resp == {'first': 'first_string', 'second': 'second_string'}

    ### PARAMS ###
    #def test_set_None(self):  # injecting None is meaningless and should return false
    #    assert not self.client.param_set('random_param', None)  # simply check that it didnt set

    def test_set_Empty(self):
        assert self.client.param_set('random_param')  # simply check if set

    def test_get_None(self):
        assert self.client.param_get(
            'random_param') is None  # simply check if nothing got

    def test_set_get_echo_Empty(self):
        assert self.client.param_set('random_param')  # default should be {}
        print "set value content {0}".format({})
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {}

    def test_set_get_echo_Simple_Arg(self):
        data = 'data_string'
        assert self.client.param_set('random_param', data)
        print "set value content {0}".format(data)
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == data

    def test_set_get_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        assert self.client.param_set('random_param', data)
        print "set value content {0}".format(data)
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == data

    def test_set_get_echo_Simple_KWArgs(self):
        assert self.client.param_set('random_param', data='data_string')
        print "set value content {0}".format("data='data_string'")
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {'data': 'data_string'}

    def test_set_get_echo_Complex_KWArgs(self):
        assert self.client.param_set('random_param',
                                     first='first_string',
                                     second='second_string')
        print "set value content {0}".format(
            "first='first_string', second='second_string'")
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {'first': 'first_string', 'second': 'second_string'}
示例#15
0
 def setUp(self):
     self.mockInstance = PyrosMock()
     # setting up mockinterface instance
     cmd_conn = self.mockInstance.start()
     self.client = PyrosClient(cmd_conn)
示例#16
0
class TestPyrosClientOnMock(unittest.TestCase):
    def setUp(self):
        self.mockInstance = PyrosMock()
        # setting up mockinterface instance
        cmd_conn = self.mockInstance.start()
        self.client = PyrosClient(cmd_conn)

    def tearDown(self):
        self.mockInstance.shutdown()

    ### TOPICS ###

    # TODO : test list features more !
    def test_list_all(self):
        t = self.client.topics()
        # Make sure we get all mockinterface topics
        assert t is not None


    def test_inject_None(self):  # injecting None is meaningless and should return false
        assert self.client.topic_inject('random_topic', None)  # simply check that it injected (default Empty since we support kwargs)

    def test_inject_Empty(self):
        assert self.client.topic_inject('random_topic')  # simply check if injected

    #TODO : how to test strict backend with Mock ?
    #def test_inject_Wrong(self):
    #    with assert_raises(Exception) as expt:  # TODO : be more specific
    #        data = 42
    #        self.client.topic_inject('random_topic', data)  # simply check exception raised
    #    # assert_equal(expt, smthg...)

    def test_extract_None(self):
        assert self.client.topic_extract('random_topic') is None  # simply check if nothing extracted

    def test_inject_extract_echo_Empty(self):
        assert self.client.topic_inject('random_topic')  # default should be {}
        print "injected message content {0}".format({})
        recv = self.client.topic_extract('random_topic')
        print "extracted message {0}".format(recv)
        assert recv == {}

    def test_inject_extract_echo_Simple_Arg(self):
        data = 'data_string'
        assert self.client.topic_inject('random_topic', data)
        print "injected message content {0}".format(data)
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == data

    def test_inject_extract_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        assert self.client.topic_inject('random_topic', data)
        print "injected message content {0}".format(data)
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == data

    def test_inject_extract_echo_Simple_KWArgs(self):
        assert self.client.topic_inject('random_topic', data='data_string')
        print "injected message content {0}".format("data='data_string'")
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == {'data':'data_string'}

    def test_inject_extract_echo_Complex_KWArgs(self):
        assert self.client.topic_inject('random_topic', first='first_string', second='second_string')
        print "injected message content {0}".format("first='first_string', second='second_string'")
        recv = self.client.topic_extract('random_topic')
        print "extracted message content {0}".format(recv)
        assert recv == {'first': 'first_string', 'second': 'second_string'}

    ### SERVICES ###
    # TODO : think how to test strict backend with Mock ?
    #def test_call_Wrong(self):
    #    with assert_raises(Exception) as expt:  # TODO : be more specific
    #        data = 42
    #        print "request content {0}".format(data)
    #        resp = self.client.service_call('random_service', data)  # simply check exception raised
    #    # assert_equal(expt, smthg...)

    def test_call_echo_Simple_Arg(self):
        data = 'data_string'
        print "request content {0}".format(data)
        resp = self.client.service_call('random_service', data)
        print "response content {0}".format(resp)
        assert resp == data

    def test_call_echo_None(self):
        print "request content {0}".format({})
        resp = self.client.service_call('random_service', None)  # calling with None is interpreted as default (Empty) call (since we support kwargs)
        print "response content {0}".format(resp)
        assert resp == {}

    def test_call_echo_Empty(self):
        print "request content {0}".format({})
        resp = self.client.service_call('random_service')
        print "response content {0}".format(resp)
        assert resp == {}

    def test_call_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        print "request content {0}".format(data)
        resp = self.client.service_call('random_service', data)
        print "response content {0}".format(resp)
        assert resp == data

    def test_call_echo_Simple_KWArgs(self):
        print "request content {0}".format("data='data_string'")
        resp = self.client.service_call('random_service', data='data_string')
        print "response content {0}".format(resp)
        assert resp == {'data': 'data_string'}

    def test_call_echo_Complex_KWArgs(self):
        print "injected message content {0}".format("first='first_string', second='second_string'")
        resp = self.client.service_call('random_service', first='first_string', second='second_string')
        print "extracted message content {0}".format(resp)
        assert resp == {'first': 'first_string', 'second': 'second_string'}

    ### PARAMS ###
    #def test_set_None(self):  # injecting None is meaningless and should return false
    #    assert not self.client.param_set('random_param', None)  # simply check that it didnt set

    def test_set_Empty(self):
        assert self.client.param_set('random_param')  # simply check if set

    def test_get_None(self):
        assert self.client.param_get('random_param') is None  # simply check if nothing got

    def test_set_get_echo_Empty(self):
        assert self.client.param_set('random_param')  # default should be {}
        print "set value content {0}".format({})
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {}

    def test_set_get_echo_Simple_Arg(self):
        data = 'data_string'
        assert self.client.param_set('random_param', data)
        print "set value content {0}".format(data)
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == data

    def test_set_get_echo_Complex_Arg(self):
        data = {'first': 'first_string', 'second': 'second_string'}
        assert self.client.param_set('random_param', data)
        print "set value content {0}".format(data)
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == data

    def test_set_get_echo_Simple_KWArgs(self):
        assert self.client.param_set('random_param', data='data_string')
        print "set value content {0}".format("data='data_string'")
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {'data':'data_string'}

    def test_set_get_echo_Complex_KWArgs(self):
        assert self.client.param_set('random_param', first='first_string', second='second_string')
        print "set value content {0}".format("first='first_string', second='second_string'")
        recv = self.client.param_get('random_param')
        print "got value content {0}".format(recv)
        assert recv == {'first': 'first_string', 'second': 'second_string'}