Skip to content

Commit

Permalink
Improve socket mode tests (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamBergamin authored Nov 21, 2023
1 parent eec2617 commit 9209e7b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
19 changes: 17 additions & 2 deletions tests/adapter_tests/socket_mode/mock_socket_mode_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import logging
import sys
import threading
import time
import requests
from multiprocessing.context import Process
from typing import List, Optional
from typing import List
from unittest import TestCase

from tests.utils import get_mock_server_mode
Expand All @@ -22,6 +24,11 @@ def start_thread_socket_mode_server(test: TestCase, port: int):
def _start_thread_socket_mode_server():
logger = logging.getLogger(__name__)
app: Flask = Flask(__name__)

@app.route("/state")
def state():
return json.dumps({"success": True}), 200, {"ContentType": "application/json"}

sockets: Sockets = Sockets(app)

envelopes_to_consume: List[str] = list(socket_mode_envelopes)
Expand Down Expand Up @@ -81,12 +88,20 @@ def start_socket_mode_server(test, port: int):
test.sm_thread = threading.Thread(target=start_thread_socket_mode_server(test, port))
test.sm_thread.daemon = True
test.sm_thread.start()
time.sleep(2) # wait for the server
wait_for_socket_mode_server(port, 2) # wait for the server
else:
test.sm_process = Process(target=start_process_socket_mode_server, kwargs={"port": port})
test.sm_process.start()


def wait_for_socket_mode_server(port: int, secs: int):
start_time = time.time()
while (time.time() - start_time) < secs:
response = requests.get(url=f"http://localhost:{port}/state")
if response.ok:
break


def stop_socket_mode_server(test):
if get_mock_server_mode() == "threading":
print(test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def setup_method(self):
base_url="http://localhost:8888",
)
start_socket_mode_server(self, 3011)
time.sleep(2) # wait for the server

def teardown_method(self):
cleanup_mock_web_api_server(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def setup_method(self):
base_url="http://localhost:8888",
)
start_socket_mode_server(self, 3012)
time.sleep(2) # wait for the server

def teardown_method(self):
cleanup_mock_web_api_server(self)
Expand Down
1 change: 0 additions & 1 deletion tests/adapter_tests/socket_mode/test_lazy_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def setup_method(self):
base_url="http://localhost:8888",
)
start_socket_mode_server(self, 3011)
time.sleep(2) # wait for the server

def teardown_method(self):
cleanup_mock_web_api_server(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def event_loop(self):
@pytest.mark.asyncio
async def test_events(self):
start_socket_mode_server(self, 3021)
await asyncio.sleep(1) # wait for the server

app = AsyncApp(client=self.web_client)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def event_loop(self):
@pytest.mark.asyncio
async def test_lazy_listeners(self):
start_socket_mode_server(self, 3021)
await asyncio.sleep(1) # wait for the server

app = AsyncApp(client=self.web_client)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def event_loop(self):
@pytest.mark.asyncio
async def test_events(self):
start_socket_mode_server(self, 3022)
await asyncio.sleep(1) # wait for the server

app = AsyncApp(client=self.web_client)

Expand Down

0 comments on commit 9209e7b

Please sign in to comment.