Ejemplo n.º 1
0
    async def test_send_message_while_disconnection(self):
        if is_ci_unstable_test_skip_enabled():
            return
        t = Thread(target=start_socket_mode_server(self, 3001))
        t.daemon = True
        t.start()

        client = SocketModeClient(
            app_token="xapp-A111-222-xyz",
            web_client=self.web_client,
            auto_reconnect_enabled=False,
            trace_enabled=True,
        )

        try:
            time.sleep(1)  # wait for the server
            client.wss_uri = "ws://0.0.0.0:3001/link"
            await client.connect()
            await asyncio.sleep(1)  # wait for the message receiver
            await client.send_message("foo")

            await client.disconnect()
            await asyncio.sleep(1)  # wait for the message receiver
            try:
                await client.send_message("foo")
                self.fail("ConnectionError is expected here")
            except ConnectionError as _:
                pass

            await client.connect()
            await asyncio.sleep(1)  # wait for the message receiver
            await client.send_message("foo")
        finally:
            await client.close()
            self.server.stop()
            self.server.close()
    async def test_interactions(self):
        t = Thread(target=start_socket_mode_server(self, 3001))
        t.daemon = True
        t.start()

        received_messages = []
        received_socket_mode_requests = []

        async def message_handler(message: WSMessage):
            self.logger.info(f"Raw Message: {message}")
            await asyncio.sleep(randint(50, 200) / 1000)
            received_messages.append(message.data)

        async def socket_mode_listener(
            self: AsyncBaseSocketModeClient, request: SocketModeRequest,
        ):
            self.logger.info(f"Socket Mode Request: {request.payload}")
            await asyncio.sleep(randint(50, 200) / 1000)
            received_socket_mode_requests.append(request.payload)

        client = SocketModeClient(
            app_token="xapp-A111-222-xyz",
            web_client=self.web_client,
            on_message_listeners=[message_handler],
            auto_reconnect_enabled=False,
        )
        client.socket_mode_request_listeners.append(socket_mode_listener)

        try:
            time.sleep(1)  # wait for the server
            client.wss_uri = "ws://0.0.0.0:3001/link"
            await client.connect()
            await asyncio.sleep(1)  # wait for the message receiver

            for _ in range(10):
                await client.send_message("foo")
                await client.send_message("bar")
                await client.send_message("baz")

            expected = (
                socket_mode_envelopes
                + [socket_mode_hello_message]
                + ["foo", "bar", "baz"] * 10
            )
            expected.sort()
            expected.sort()

            count = 0
            while count < 10 and len(received_messages) < len(expected):
                await asyncio.sleep(0.2)
                count += 0.2

            received_messages.sort()
            self.assertEqual(received_messages, expected)

            self.assertEqual(
                len(socket_mode_envelopes), len(received_socket_mode_requests)
            )
        finally:
            await client.close()
            self.server.stop()
            self.server.close()