示例#1
0
def test_kwargs_are_received(router):

    class SubscribingClient(Client):
        received_kwargs = None

        @subscribe(topic="foo")
        def foo_topic_handler(self, **kwargs):
            SubscribingClient.received_kwargs = kwargs

    reader = SubscribingClient(url=router.url)

    assert SubscribingClient.received_kwargs is None

    with reader:
        assert SubscribingClient.received_kwargs is None

        publisher = Client(url=router.url)

        assert SubscribingClient.received_kwargs is None

        with publisher:
            publisher.publish(
                topic="foo", message="foobar", spam="eggs")

            def check_kwargs():
                assert SubscribingClient.received_kwargs == {
                    'message': 'foobar',
                    'spam': 'eggs',
                    'meta': {
                        'topic': 'foo',
                        'subscription_id': ANY,
                    },
                }

            assert_stops_raising(check_kwargs)
示例#2
0
def test_kwargs_are_received(router):
    class SubscribingClient(Client):
        received_kwargs = None

        @subscribe(topic="foo")
        def foo_topic_handler(self, **kwargs):
            SubscribingClient.received_kwargs = kwargs

    reader = SubscribingClient(url=router.url)

    assert SubscribingClient.received_kwargs is None

    with reader:
        assert SubscribingClient.received_kwargs is None

        publisher = Client(url=router.url)

        assert SubscribingClient.received_kwargs is None

        with publisher:
            publisher.publish(topic="foo", message="foobar", spam="eggs")

            def check_kwargs():
                assert SubscribingClient.received_kwargs == {
                    'message': 'foobar',
                    'spam': 'eggs',
                    'meta': {
                        'topic': 'foo',
                        'subscription_id': ANY,
                    },
                }

            assert_stops_raising(check_kwargs)
示例#3
0
def test_cannot_publish_nothing_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    with client:
        with pytest.raises(WampyError):
            client.publish(topic="foo")

        assert foo_subscriber.call_count == 0
示例#4
0
def test_cannot_publish_nothing_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    with client:
        with pytest.raises(WampyError):
            client.publish(topic="foo")

        assert foo_subscriber.call_count == 0
示例#5
0
def test_publish_kwargs_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    client.start()
    client.publish(topic="foo", message="foobar")

    def check_call_count():
        assert foo_subscriber.call_count == 1

    assert_stops_raising(check_call_count)

    client.publish(topic="foo", message="foobar")
    client.publish(topic="foo", message="spam")
    client.publish(topic="foo", message="ham")

    def check_call_count():
        assert foo_subscriber.call_count == 4

    assert_stops_raising(check_call_count)

    client.stop()
示例#6
0
def test_publish_kwargs_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    client.start()
    client.publish(topic="foo", message="foobar")

    def check_call_count():
        assert foo_subscriber.call_count == 1

    assert_stops_raising(check_call_count)

    client.publish(topic="foo", message="foobar")
    client.publish(topic="foo", message="spam")
    client.publish(topic="foo", message="ham")

    def check_call_count():
        assert foo_subscriber.call_count == 4

    assert_stops_raising(check_call_count)

    client.stop()
示例#7
0
def test_cannot_publish_args_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    with client:

        with pytest.raises(WampyError):
            client.publish("foo", )

        assert foo_subscriber.call_count == 0

        with pytest.raises(WampyError):
            client.publish("foo", "foobar")

        assert foo_subscriber.call_count == 0

        even_more_args = range(100)

        with pytest.raises(WampyError):
            client.publish(even_more_args)

        assert foo_subscriber.call_count == 0
示例#8
0
def test_cannot_publish_args_to_topic(foo_subscriber, router):
    assert foo_subscriber.call_count == 0

    client = Client(url=router.url)

    with client:

        with pytest.raises(WampyError):
            client.publish("foo",)

        assert foo_subscriber.call_count == 0

        with pytest.raises(WampyError):
            client.publish("foo", "foobar")

        assert foo_subscriber.call_count == 0

        even_more_args = range(100)

        with pytest.raises(WampyError):
            client.publish(even_more_args)

        assert foo_subscriber.call_count == 0
示例#9
0
class TestDaemon(object):
    @classmethod
    def setup_class(self):
        intents_folder = "tests/daemon_test/intents"
        dialogs_folder = "tests/daemon_test/dialogs"
        workdir = "tests/daemon_test/workdir"
        self.fake_daemon = FakeDaemon("fake_daemon", workdir, intents_folder,
                                      dialogs_folder)
        self.fake_daemon.settings.delete("/config/global")
        self.fake_daemon.settings.delete()

        self.wamp_client = Client(realm="tuxeatpi")
        self.wamp_client.start()

    @classmethod
    def teardown_class(self):
        self.fake_daemon.settings.delete("/config/global")
        self.fake_daemon.settings.delete()
        self.fake_daemon.shutdown()

        self.wamp_client.stop()

    @pytest.mark.run_loop
    def test_daemon(self, capsys):
        # Start
        self.fake_daemon._run_main_loop = False
        t = threading.Thread(target=self.fake_daemon.start)
        t = t.start()

        # Wait for start
        time.sleep(2)
        # FIRST CONFIGURATION (at init time)
        # Set global config
        self.fake_daemon.settings.save(
            {
                "language": "en_US",
                "nlu_engine": "nlu_test"
            }, "global")
        self.fake_daemon.settings.save({"param1": "value1"})
        time.sleep(2)
        # check first config
        assert self.fake_daemon.settings.language == "en_US"
        assert self.fake_daemon.settings.nlu_engine == "nlu_test"

        # Get dialog test
        dialog_rendered = self.fake_daemon.get_dialog("render_test",
                                                      test="mytest")
        assert dialog_rendered == "This is a rendering test mytest"

        # Publish Messages
        message = Message("fake_daemon.help",
                          {"arguments": {
                              "help_arg": "test1"
                          }})

        # TODO: waiting for https://github.com/noisyboiler/wampy/pull/42
        self.fake_daemon.publish(message)
        # Then remove me
        # Publish using an other wamp client
        self.wamp_client.publish(topic="fake_daemon.help",
                                 message=message.payload)

        time.sleep(2)
        assert self.fake_daemon.help_arg == "test1"
        # Publish Messages override
        message = Message("fake_daemon.help",
                          {"arguments": {
                              "help_arg": "test2"
                          }})

        # TODO: waiting for https://github.com/noisyboiler/wampy/pull/42
        self.fake_daemon.publish(message, override_topic="fake_daemon.help")
        # Then remove me
        # Publish using an other wamp client
        self.wamp_client.publish(topic="fake_daemon.help",
                                 message=message.payload)

        time.sleep(1)
        assert self.fake_daemon.help_arg == "test2"

        # Set component config
        # checkcomponent config
        assert self.fake_daemon.args1 == "value1"

        time.sleep(4)
        # SECOND CONFIGURATION (at run time)
        self.fake_daemon.settings.save({"param1": "value2"})
        self.fake_daemon.settings.save(
            {
                "language": "fr_FR",
                "nlu_engine": "nlu_test2"
            }, "global")
        time.sleep(1)
        assert self.fake_daemon.settings.language == "fr_FR"
        assert self.fake_daemon.settings.nlu_engine == "nlu_test2"
        assert self.fake_daemon.args1 == "value2"
        assert self.fake_daemon.settings.params == {"param1": "value2"}

        # TODO capture logging output
        self.fake_daemon.reload()
示例#10
0
from wampy.peers.clients import Client
# from wampy.mixins import
from wampy.peers.routers import Crossbar
import wampy.roles as subscriber
import time

# class wampyapp(Client):
#
#     @subscriber(topic = "topic1")
#     def clb (self,topicdata):
#         print topicdata

if __name__ == "__main__":
    cros = Crossbar()
    cros.start()
    client = Client(url="ws://127.0.0.1:8080/ws", realm="realm1")
    # client2 = Client(url="ws://127.0.0.1:8080/ws", realm="realm2")
    client.start()
    # client2.start()
    # client.publish(topic = "topic1", message = {'foo':"bar"})
    start = time.time()
    stop = start
    while (stop - start) < 5:
        print(stop - start)
        client.publish(topic="topic1", message={'foo': "bar"})
        client.publish(topic="topic2", message={'foo': "bar"})
        time.sleep(1)
        stop = time.time()