Skip to content

Commit 67a7b2e

Browse files
committed
feat: add api test
1 parent 7616f4a commit 67a7b2e

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

Diff for: engine/e2e-test/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
3-
from test_cli_engine_list import TestCliEngineList
2+
from test_api_engine_list import TestApiEngineList
43
from test_cli_engine_get import TestCliEngineGet
4+
from test_cli_engine_list import TestCliEngineList
55

66
if __name__ == "__main__":
77
pytest.main([__file__, "-v"])

Diff for: engine/e2e-test/test_api_engine_list.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
import requests
3+
from test_runner import start_server, stop_server
4+
5+
6+
class TestApiEngineList:
7+
8+
@pytest.fixture(autouse=True)
9+
def setup_and_teardown(self):
10+
# Setup
11+
start_server()
12+
13+
yield
14+
15+
# Teardown
16+
stop_server()
17+
18+
def test_engines_list_api_run_successfully(self):
19+
response = requests.get("http://localhost:3928/engines")
20+
assert response.status_code == 200

Diff for: engine/e2e-test/test_runner.py

+46-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,56 @@
11
import platform
2+
import select
23
import subprocess
3-
import os
4-
from typing import List, Tuple
4+
import time
5+
from typing import List
56

67

78
def run(name: str, arguments: List[str]):
89
if platform.system() == "Windows":
910
executable = "build\\cortex-cpp.exe"
1011
else:
1112
executable = "build/cortex-cpp"
12-
result = subprocess.run([executable] + arguments, capture_output=True, text=True)
13+
print("Command name", name)
14+
print("Running command: ", [executable] + arguments)
15+
if len(arguments) == 0:
16+
result = subprocess.run(executable, capture_output=True, text=True, timeout=5)
17+
else:
18+
result = subprocess.run(
19+
[executable] + arguments, capture_output=True, text=True, timeout=5
20+
)
1321
return result.returncode, result.stdout, result.stderr
22+
23+
24+
def start_server(timeout=5):
25+
if platform.system() == "Windows":
26+
executable = "build\\cortex-cpp.exe"
27+
else:
28+
executable = "build/cortex-cpp"
29+
process = subprocess.Popen(
30+
executable, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
31+
)
32+
33+
success_message = "Server started"
34+
start_time = time.time()
35+
while time.time() - start_time < timeout:
36+
# Use select to check if there's data to read from stdout or stderr
37+
readable, _, _ = select.select([process.stdout, process.stderr], [], [], 0.1)
38+
39+
for stream in readable:
40+
line = stream.readline()
41+
if line:
42+
print(line.strip()) # Print output for debugging
43+
if success_message in line:
44+
# have to wait a bit for server to really up and accept connection
45+
time.sleep(0.3)
46+
return True, process # Success condition met
47+
48+
# Check if the process has ended
49+
if process.poll() is not None:
50+
return False, process # Process ended without success message
51+
52+
return False, process # Timeout reached
53+
54+
55+
def stop_server():
56+
run("Stop server", ["stop"])

Diff for: engine/main.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
void RunServer() {
2929
auto config = file_manager_utils::GetCortexConfig();
30-
LOG_INFO << "Host: " << config.apiServerHost << " Port: " << config.apiServerPort << "\n";
30+
LOG_INFO << "Host: " << config.apiServerHost
31+
<< " Port: " << config.apiServerPort << "\n";
3132

3233
// Create logs/ folder and setup log to file
3334
std::filesystem::create_directory(config.logFolderPath + "/" +
@@ -72,7 +73,8 @@ void RunServer() {
7273
LOG_INFO << "Server started, listening at: " << config.apiServerHost << ":"
7374
<< config.apiServerPort;
7475
LOG_INFO << "Please load your model";
75-
drogon::app().addListener(config.apiServerHost, std::stoi(config.apiServerPort));
76+
drogon::app().addListener(config.apiServerHost,
77+
std::stoi(config.apiServerPort));
7678
drogon::app().setThreadNum(drogon_thread_num);
7779
LOG_INFO << "Number of thread is:" << drogon::app().getThreadNum();
7880

0 commit comments

Comments
 (0)