def test_create_queue_bad_schema(self):
        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        with pytest.raises(SchemaValidationFailed):
            registry.create_queue("queue_name", test_app, "test", {}, "fifo")

        with pytest.raises(SchemaValidationFailed):
            registry.create_queue("queue_name", test_app, {}, "test", "fifo")
    def test_create_queue_simple(self, queue_type, expected_cls):
        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        queue = registry.create_queue("queue_name", test_app, {}, {},
                                      queue_type)

        assert isinstance(queue, expected_cls)
        assert registry.get_channel("queue_name") is queue
    def test_queue_closure(self, queue_type, cookie):
        event = Event()
        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        registry.create_queue("/fifo-closure", test_app, {"type": "string"},
                              {}, queue_type)

        server = MessageServer(11023, ApplicationRegistry(), registry,
                               SynonymRegistry(), event.set)
        thread = Thread(target=server.run)
        thread.start()
        event.wait()

        conn = WeaveConnection()
        conn.connect()

        def patch_receive(receiver, event):
            original = receiver.receive

            def receive():
                event.set()
                original()

            receiver.receive = receive

        def wrap_run(receiver):
            def run():
                try:
                    receiver.run()
                except:
                    pass

            return run

        e1 = Event()
        r1 = Receiver(conn, "/fifo-closure", cookie=next(cookie))
        r1.start()
        patch_receive(r1, e1)
        t1 = Thread(target=wrap_run(r1))
        t1.start()

        e2 = Event()
        r2 = Receiver(conn, "/fifo-closure", cookie=next(cookie))
        r2.start()
        patch_receive(r2, e2)
        t2 = Thread(target=wrap_run(r2))
        t2.start()

        e1.wait()
        e2.wait()

        server.shutdown()
        thread.join()
        t1.join()
        t2.join()
    def test_queue_already_exists(self):
        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        queue = registry.create_queue("queue_name", test_app, {}, {},
                                      "sessionized")
        assert isinstance(queue, SessionizedQueue)

        with pytest.raises(ObjectAlreadyExists):
            registry.create_queue("queue_name", test_app, {}, {}, "fifo")
    def test_queue_connect_fail(self):
        backup = RoundRobinQueue.connect
        RoundRobinQueue.connect = lambda self: False

        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        with pytest.raises(InternalError):
            registry.create_queue("queue_name", test_app, {}, {}, "fifo")

        RoundRobinQueue.connect = backup
    def setup_class(cls):
        event = Event()

        schema = {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "string"
                }
            },
            "required": ["foo"]
        }

        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)
        registry.create_queue("/a.b.c", test_app, schema, {}, 'fifo')
        registry.create_queue("/test.sessionized", test_app,
                              {"type": "string"}, {}, 'sessionized')
        registry.create_queue("/test.sessionized2", test_app,
                              {"type": "string"}, {}, 'sessionized')
        registry.create_queue("/test.sessionized/several", test_app,
                              {"type": "string"}, {}, 'sessionized')
        registry.create_queue("/test.fifo/simple", test_app,
                              {"type": "string"}, {}, 'fifo')
        registry.create_queue("/test.fifo/test-disconnect", test_app,
                              {"type": "string"}, {}, 'fifo')
        registry.create_queue("/test.sessionized/test-disconnect", test_app,
                              {"type": "string"}, {}, 'sessionized')
        registry.create_queue('/multicast/1', test_app, {"type": "string"}, {},
                              'multicast')
        registry.create_queue('/multicast/2', test_app, {"type": "string"}, {},
                              'multicast')

        synonym_registry = SynonymRegistry()
        synonym_registry.register("/multi", "/multicast/2")

        cls.server = MessageServer(11023, apps, registry, synonym_registry,
                                   event.set)
        cls.server_thread = Thread(target=cls.server.run)
        cls.server_thread.start()
        event.wait()
        cls.conn = WeaveConnection.local()
        cls.conn.connect()
    def test_shutdown(self):
        test_app = Plugin("test", "test", "test-token")
        apps = ApplicationRegistry()
        registry = ChannelRegistry(apps)

        queue1 = registry.create_queue("queue1", test_app, {}, {}, "fifo")
        queue2 = registry.create_queue("queue2", test_app, {}, {},
                                       "sessionized")

        flag = []

        def disconnect_fn():
            flag.append(None)

        queue1.disconnect = disconnect_fn
        queue2.disconnect = disconnect_fn

        registry.shutdown()

        assert len(flag) == 2

        with pytest.raises(ObjectClosed):
            registry.create_queue("queue3", test_app, {}, {}, "fifo")
 def test_get_queue_invalid(self):
     test_app = Plugin("test", "test", "test-token")
     apps = ApplicationRegistry()
     registry = ChannelRegistry(apps)
     with pytest.raises(ObjectNotFound):
         registry.get_channel("test_queue")
 def test_create_queue_bad_queue_type(self):
     test_app = Plugin("test", "test", "test-token")
     apps = ApplicationRegistry()
     registry = ChannelRegistry(apps)
     with pytest.raises(BadArguments):
         registry.create_queue("queue_name", test_app, {}, {}, "bad-type")