def test_interactions(self):
        t = Thread(target=start_socket_mode_server(self, 3012))
        t.daemon = True
        t.start()

        received_messages = []
        received_socket_mode_requests = []

        def message_handler(ws_app, message):
            self.logger.info(f"Raw Message: {message}")
            time.sleep(randint(50, 200) / 1000)
            received_messages.append(message)

        def socket_mode_request_handler(client: BaseSocketModeClient,
                                        request: SocketModeRequest):
            self.logger.info(f"Socket Mode Request: {request}")
            time.sleep(randint(50, 200) / 1000)
            received_socket_mode_requests.append(request)

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

        try:
            time.sleep(1)  # wait for the server
            client.wss_uri = "ws://0.0.0.0:3012/link"
            client.connect()
            self.assertTrue(client.is_connected())
            time.sleep(1)  # wait for the message receiver

            for _ in range(10):
                client.send_message("foo")
                client.send_message("bar")
                client.send_message("baz")
            self.assertTrue(client.is_connected())

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

            count = 0
            while count < 10 and len(received_messages) < len(expected):
                time.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:
            client.close()
            self.server.stop()
            self.server.close()
Beispiel #2
0
    def test_send_message_while_disconnection(self):
        if is_ci_unstable_test_skip_enabled():
            return
        t = Thread(target=start_socket_mode_server(self, 3012))
        t.daemon = True
        t.start()
        time.sleep(2)  # wait for the server

        try:
            self.reset_sever_state()
            client = SocketModeClient(
                app_token="xapp-A111-222-xyz",
                web_client=self.web_client,
                auto_reconnect_enabled=False,
                trace_enabled=True,
            )
            client.wss_uri = "ws://0.0.0.0:3012/link"
            client.connect()
            time.sleep(1)  # wait for the connection
            client.send_message("foo")

            client.disconnect()
            time.sleep(1)  # wait for the connection
            try:
                client.send_message("foo")
                # TODO: The client may not raise an exception here
                # self.fail("WebSocketException is expected here")
            except WebSocketException as _:
                pass

            client.connect()
            time.sleep(1)  # wait for the connection
            client.send_message("foo")
        finally:
            client.close()
            self.server.stop()
            self.server.close()
    app_token=os.environ.get("SLACK_SDK_TEST_SOCKET_MODE_APP_TOKEN"),
    web_client=WebClient(
        token=os.environ.get("SLACK_SDK_TEST_SOCKET_MODE_BOT_TOKEN"),
        # pip3 install proxy.py
        # proxy --port 9000 --log-level d
        proxy="http://localhost:9000",
    ),
    trace_enabled=True,
    # pip3 install proxy.py
    # proxy --port 9000 --log-level d
    http_proxy_host="localhost",
    http_proxy_port=9000,
    http_proxy_auth=("user", "pass"),
)

if __name__ == "__main__":

    def process(client: SocketModeClient, req: SocketModeRequest):
        if req.type == "events_api":
            response = SocketModeResponse(envelope_id=req.envelope_id)
            client.send_socket_mode_response(response)
            client.web_client.reactions_add(
                name="eyes",
                channel=req.payload["event"]["channel"],
                timestamp=req.payload["event"]["ts"],
            )

    client.socket_mode_request_listeners.append(process)
    client.connect()
    Event().wait()