Skip to content

Commit 97e8a02

Browse files
committed
Extract FrontendDummy::execute_request() and variants
1 parent a4272d1 commit 97e8a02

File tree

2 files changed

+152
-498
lines changed

2 files changed

+152
-498
lines changed

crates/amalthea/src/fixtures/dummy_frontend.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,77 @@ impl DummyFrontend {
236236
})
237237
}
238238

239+
/// Sends an execute request and handles the standard message flow:
240+
/// busy -> execute_input -> idle -> execute_reply.
241+
/// Asserts that the input code matches and returns the execution count.
242+
#[track_caller]
243+
pub fn execute_request_invisibly(&self, code: &str) -> u32 {
244+
self.send_execute_request(code, ExecuteRequestOptions::default());
245+
self.recv_iopub_busy();
246+
247+
let input = self.recv_iopub_execute_input();
248+
assert_eq!(input.code, code);
249+
250+
self.recv_iopub_idle();
251+
252+
let execution_count = self.recv_shell_execute_reply();
253+
assert_eq!(execution_count, input.execution_count);
254+
255+
execution_count
256+
}
257+
258+
/// Sends an execute request and handles the standard message flow with a result:
259+
/// busy -> execute_input -> execute_result -> idle -> execute_reply.
260+
/// Asserts that the input code matches and passes the result to the callback.
261+
/// Returns the execution count.
262+
#[track_caller]
263+
pub fn execute_request<F>(&self, code: &str, result_check: F) -> u32
264+
where
265+
F: FnOnce(String),
266+
{
267+
self.send_execute_request(code, ExecuteRequestOptions::default());
268+
self.recv_iopub_busy();
269+
270+
let input = self.recv_iopub_execute_input();
271+
assert_eq!(input.code, code);
272+
273+
let result = self.recv_iopub_execute_result();
274+
result_check(result);
275+
276+
self.recv_iopub_idle();
277+
278+
let execution_count = self.recv_shell_execute_reply();
279+
assert_eq!(execution_count, input.execution_count);
280+
281+
execution_count
282+
}
283+
284+
/// Sends an execute request that produces an error and handles the standard message flow:
285+
/// busy -> execute_input -> execute_error -> idle -> execute_reply_exception.
286+
/// Passes the error message to the callback for custom assertions.
287+
/// Returns the execution count.
288+
#[track_caller]
289+
pub fn execute_request_error<F>(&self, code: &str, error_check: F) -> u32
290+
where
291+
F: FnOnce(String),
292+
{
293+
self.send_execute_request(code, ExecuteRequestOptions::default());
294+
self.recv_iopub_busy();
295+
296+
let input = self.recv_iopub_execute_input();
297+
assert_eq!(input.code, code);
298+
299+
let error_msg = self.recv_iopub_execute_error();
300+
error_check(error_msg);
301+
302+
self.recv_iopub_idle();
303+
304+
let execution_count = self.recv_shell_execute_reply_exception();
305+
assert_eq!(execution_count, input.execution_count);
306+
307+
execution_count
308+
}
309+
239310
/// Sends a Jupyter message on the Stdin socket
240311
pub fn send_stdin<T: ProtocolMessage>(&self, msg: T) {
241312
Self::send(&self.stdin_socket, &self.session, msg);

0 commit comments

Comments
 (0)