Skip to content

Commit 74fcdd0

Browse files
committed
test: speed up tests
1 parent f20322f commit 74fcdd0

File tree

6 files changed

+27
-40
lines changed

6 files changed

+27
-40
lines changed

dev-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-r requirements.txt
22
flake8==4.0.1
33
black==21.12b0
4-
websockets==10.1
4+
websocket-client==1.4.2
55
maturin==0.12.11
66
isort==5.10.1
77
pre-commit==2.19.0

integration_tests/conftest.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def session():
3939
base_routes = os.path.join(current_file_path, "./base_routes.py")
4040
command = ["python3", base_routes]
4141
process = spawn_process(command)
42-
time.sleep(5)
42+
time.sleep(1)
4343
yield
4444
kill_process(process)
4545

@@ -50,7 +50,7 @@ def default_session():
5050
base_routes = os.path.join(current_file_path, "./base_routes.py")
5151
command = ["python3", base_routes]
5252
process = spawn_process(command)
53-
time.sleep(5)
53+
time.sleep(1)
5454
yield
5555
kill_process(process)
5656

@@ -75,7 +75,7 @@ def dev_session():
7575
base_routes = os.path.join(current_file_path, "./base_routes.py")
7676
command = ["python3", base_routes, "--dev"]
7777
process = spawn_process(command)
78-
time.sleep(5)
78+
time.sleep(1)
7979
yield
8080
kill_process(process)
8181

@@ -88,6 +88,6 @@ def test_session():
8888
base_routes = os.path.join(current_file_path, "./base_routes.py")
8989
command = ["python3", base_routes, "--dev"]
9090
process = spawn_process(command)
91-
time.sleep(5)
91+
time.sleep(1)
9292
yield
9393
kill_process(process)

integration_tests/test_web_sockets.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import asyncio
2-
3-
from websockets import connect
1+
from websocket import create_connection
42

53
BASE_URL = "ws://127.0.0.1:5000"
64

75

86
def test_web_socket(session):
9-
async def start_ws(uri):
10-
async with connect(uri) as websocket:
11-
assert await websocket.recv() == "Hello world, from ws"
12-
await websocket.send("My name is?")
13-
assert await websocket.recv() == "Whaaat??"
14-
await websocket.send("My name is?")
15-
assert await websocket.recv() == "Whooo??"
16-
await websocket.send("My name is?")
17-
assert await websocket.recv() == "*chika* *chika* Slim Shady."
18-
19-
asyncio.run(start_ws(f"{BASE_URL}/web_socket"))
7+
ws = create_connection(f"{BASE_URL}/web_socket")
8+
assert ws.recv() == "Hello world, from ws"
9+
ws.send("My name is?")
10+
assert ws.recv() == "Whaaat??"
11+
ws.send("My name is?")
12+
assert ws.recv() == "Whooo??"
13+
ws.send("My name is?")
14+
assert ws.recv() == "*chika* *chika* Slim Shady."

robyn/test-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ watchdog
44
requests==2.26.0
55
uvloop==0.17.0
66
multiprocess==0.70.12.2
7-
websockets==10.1
7+
websocket-client==1.4.2
88
jinja2==3.0.2

src/server.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,27 @@ impl Server {
9494
let directories = self.directories.clone();
9595
let workers = Arc::new(workers);
9696

97-
let asyncio = py.import("asyncio").unwrap();
97+
let asyncio = py.import("asyncio")?;
98+
let event_loop = asyncio.call_method0("new_event_loop")?;
99+
asyncio.call_method1("set_event_loop", (event_loop,))?;
98100

99-
let event_loop = asyncio.call_method0("new_event_loop").unwrap();
100-
asyncio
101-
.call_method1("set_event_loop", (event_loop,))
102-
.unwrap();
103101
let startup_handler = self.startup_handler.clone();
104102
let shutdown_handler = self.shutdown_handler.clone();
105103

106-
let task_locals = Arc::new(pyo3_asyncio::TaskLocals::new(event_loop).copy_context(py)?);
107-
let task_locals_cleanup = task_locals.clone();
104+
let task_locals = pyo3_asyncio::TaskLocals::new(event_loop).copy_context(py)?;
105+
let task_locals_copy = task_locals.clone();
108106

109107
thread::spawn(move || {
110108
actix_web::rt::System::new().block_on(async move {
111109
debug!("The number of workers are {}", workers.clone());
112-
execute_event_handler(startup_handler, &task_locals)
110+
execute_event_handler(startup_handler, &task_locals_copy)
113111
.await
114112
.unwrap();
115113

116114
HttpServer::new(move || {
117115
let mut app = App::new();
118116

119-
let task_locals = task_locals.clone();
117+
let task_locals = task_locals_copy.clone();
120118
let directories = directories.read().unwrap();
121119

122120
// this loop matches three types of directory serving
@@ -168,7 +166,7 @@ impl Server {
168166
global_request_headers,
169167
body,
170168
req| {
171-
pyo3_asyncio::tokio::scope_local((*task_locals).clone(), async move {
169+
pyo3_asyncio::tokio::scope_local(task_locals.clone(), async move {
172170
index(
173171
router,
174172
const_router,
@@ -198,13 +196,12 @@ impl Server {
198196
debug!("Ctrl c handler");
199197
Python::with_gil(|py| {
200198
pyo3_asyncio::tokio::run(py, async move {
201-
execute_event_handler(shutdown_handler, &task_locals_cleanup)
199+
execute_event_handler(shutdown_handler, &task_locals.clone())
202200
.await
203201
.unwrap();
204202
Ok(())
205203
})
206-
.unwrap();
207-
})
204+
})?
208205
}
209206
Ok(())
210207
}

src/web_socket_connection.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ use pyo3_asyncio::TaskLocals;
1010
use uuid::Uuid;
1111

1212
use std::collections::HashMap;
13-
use std::sync::Arc;
1413

1514
/// Define HTTP actor
1615
#[derive(Clone)]
1716
struct MyWs {
1817
id: Uuid,
1918
router: HashMap<String, FunctionInfo>,
20-
// can probably try removing arc from here
21-
// and use clone_ref()
22-
task_locals: Arc<TaskLocals>,
19+
task_locals: TaskLocals,
2320
}
2421

2522
fn get_function_output<'a>(
@@ -129,10 +126,8 @@ pub async fn start_web_socket(
129126
req: HttpRequest,
130127
stream: web::Payload,
131128
router: HashMap<String, FunctionInfo>,
132-
task_locals: Arc<TaskLocals>,
129+
task_locals: TaskLocals,
133130
) -> Result<HttpResponse, Error> {
134-
// execute the async function here
135-
136131
ws::start(
137132
MyWs {
138133
router,

0 commit comments

Comments
 (0)