Skip to content

Commit 9209e7b

Browse files
Improve socket mode tests (#991)
1 parent eec2617 commit 9209e7b

File tree

7 files changed

+17
-8
lines changed

7 files changed

+17
-8
lines changed

tests/adapter_tests/socket_mode/mock_socket_mode_server.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import json
12
import logging
23
import sys
34
import threading
45
import time
6+
import requests
57
from multiprocessing.context import Process
6-
from typing import List, Optional
8+
from typing import List
79
from unittest import TestCase
810

911
from tests.utils import get_mock_server_mode
@@ -22,6 +24,11 @@ def start_thread_socket_mode_server(test: TestCase, port: int):
2224
def _start_thread_socket_mode_server():
2325
logger = logging.getLogger(__name__)
2426
app: Flask = Flask(__name__)
27+
28+
@app.route("/state")
29+
def state():
30+
return json.dumps({"success": True}), 200, {"ContentType": "application/json"}
31+
2532
sockets: Sockets = Sockets(app)
2633

2734
envelopes_to_consume: List[str] = list(socket_mode_envelopes)
@@ -81,12 +88,20 @@ def start_socket_mode_server(test, port: int):
8188
test.sm_thread = threading.Thread(target=start_thread_socket_mode_server(test, port))
8289
test.sm_thread.daemon = True
8390
test.sm_thread.start()
84-
time.sleep(2) # wait for the server
91+
wait_for_socket_mode_server(port, 2) # wait for the server
8592
else:
8693
test.sm_process = Process(target=start_process_socket_mode_server, kwargs={"port": port})
8794
test.sm_process.start()
8895

8996

97+
def wait_for_socket_mode_server(port: int, secs: int):
98+
start_time = time.time()
99+
while (time.time() - start_time) < secs:
100+
response = requests.get(url=f"http://localhost:{port}/state")
101+
if response.ok:
102+
break
103+
104+
90105
def stop_socket_mode_server(test):
91106
if get_mock_server_mode() == "threading":
92107
print(test)

tests/adapter_tests/socket_mode/test_interactions_builtin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def setup_method(self):
2727
base_url="http://localhost:8888",
2828
)
2929
start_socket_mode_server(self, 3011)
30-
time.sleep(2) # wait for the server
3130

3231
def teardown_method(self):
3332
cleanup_mock_web_api_server(self)

tests/adapter_tests/socket_mode/test_interactions_websocket_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def setup_method(self):
2727
base_url="http://localhost:8888",
2828
)
2929
start_socket_mode_server(self, 3012)
30-
time.sleep(2) # wait for the server
3130

3231
def teardown_method(self):
3332
cleanup_mock_web_api_server(self)

tests/adapter_tests/socket_mode/test_lazy_listeners.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def setup_method(self):
2727
base_url="http://localhost:8888",
2828
)
2929
start_socket_mode_server(self, 3011)
30-
time.sleep(2) # wait for the server
3130

3231
def teardown_method(self):
3332
cleanup_mock_web_api_server(self)

tests/adapter_tests_async/socket_mode/test_async_aiohttp.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def event_loop(self):
3939
@pytest.mark.asyncio
4040
async def test_events(self):
4141
start_socket_mode_server(self, 3021)
42-
await asyncio.sleep(1) # wait for the server
4342

4443
app = AsyncApp(client=self.web_client)
4544

tests/adapter_tests_async/socket_mode/test_async_lazy_listeners.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def event_loop(self):
3939
@pytest.mark.asyncio
4040
async def test_lazy_listeners(self):
4141
start_socket_mode_server(self, 3021)
42-
await asyncio.sleep(1) # wait for the server
4342

4443
app = AsyncApp(client=self.web_client)
4544

tests/adapter_tests_async/socket_mode/test_async_websockets.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def event_loop(self):
3939
@pytest.mark.asyncio
4040
async def test_events(self):
4141
start_socket_mode_server(self, 3022)
42-
await asyncio.sleep(1) # wait for the server
4342

4443
app = AsyncApp(client=self.web_client)
4544

0 commit comments

Comments
 (0)