Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add execute_result message #22

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
toolchain: '1.74.0'

- name: Run tests
run: cargo test
run: cargo test --features test_ipython

test-evcxr:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ zeromq = "0.3.4"

[features]
# Feature flags for starting different types of Kernels in the test suite
test_ipython = []
test_evcxr = []
test_irkernel = []
test_deno = []
22 changes: 22 additions & 0 deletions src/jupyter/iopub_content/execute_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
https://jupyter-client.readthedocs.io/en/latest/messaging.html#id6
*/
use std::collections::HashMap;

use bytes::Bytes;

use serde::Deserialize;

#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub struct ExecuteResult {
execution_count: u32,
data: HashMap<String, serde_json::Value>,
metadata: serde_json::Value,
}

impl From<Bytes> for ExecuteResult {
fn from(bytes: Bytes) -> Self {
serde_json::from_slice(&bytes).expect("Failed to deserialize ExecuteResult")
}
}
1 change: 1 addition & 0 deletions src/jupyter/iopub_content/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod display_data;
pub mod execute_result;
pub mod status;
pub mod stream;
19 changes: 18 additions & 1 deletion src/jupyter/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ zeromq::ZmqMessage -> WireProtocol -> Response -> Message<T> with Jupyter messag
use crate::jupyter::constants::EMPTY_DICT_BYTES;
use crate::jupyter::header::Header;
use crate::jupyter::iopub_content::display_data::{DisplayData, UpdateDisplayData};
use crate::jupyter::iopub_content::execute_result::ExecuteResult;
use crate::jupyter::iopub_content::status::Status;
use crate::jupyter::iopub_content::stream::Stream;
use crate::jupyter::message::Message;
Expand All @@ -22,12 +23,16 @@ pub struct UnmodeledContent(serde_json::Value);

#[derive(Debug)]
pub enum Response {
Status(Message<Status>),
// Request/reply from shell channel
KernelInfo(Message<KernelInfoReply>),
Execute(Message<ExecuteReply>),
// Messages from iopub channel
Status(Message<Status>),
ExecuteResult(Message<ExecuteResult>),
Stream(Message<Stream>),
DisplayData(Message<DisplayData>),
UpdateDisplayData(Message<UpdateDisplayData>),
// Messages I haven't modeled yet, crate is WIP
Unmodeled(Message<UnmodeledContent>),
}

Expand All @@ -38,6 +43,7 @@ impl Response {
Response::Status(msg) => msg.parent_msg_id(),
Response::KernelInfo(msg) => msg.parent_msg_id(),
Response::Execute(msg) => msg.parent_msg_id(),
Response::ExecuteResult(msg) => msg.parent_msg_id(),
Response::Stream(msg) => msg.parent_msg_id(),
Response::DisplayData(msg) => msg.parent_msg_id(),
Response::UpdateDisplayData(msg) => msg.parent_msg_id(),
Expand All @@ -51,6 +57,7 @@ impl Response {
Response::Status(msg) => msg.header.msg_type.to_owned(),
Response::KernelInfo(msg) => msg.header.msg_type.to_owned(),
Response::Execute(msg) => msg.header.msg_type.to_owned(),
Response::ExecuteResult(msg) => msg.header.msg_type.to_owned(),
Response::Stream(msg) => msg.header.msg_type.to_owned(),
Response::DisplayData(msg) => msg.header.msg_type.to_owned(),
Response::UpdateDisplayData(msg) => msg.header.msg_type.to_owned(),
Expand Down Expand Up @@ -101,6 +108,16 @@ impl From<WireProtocol> for Response {
};
Response::Execute(msg)
}
"execute_result" => {
let content: ExecuteResult = wp.content.into();
let msg: Message<ExecuteResult> = Message {
header,
parent_header,
metadata: Some(metadata),
content,
};
Response::ExecuteResult(msg)
}
"stream" => {
let content: Stream = wp.content.into();
let msg: Message<Stream> = Message {
Expand Down
17 changes: 14 additions & 3 deletions tests/test_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ use kernel_sidecar_rs::kernels::JupyterKernel;
// Start Kernel (type based on feature flags) and wait for ZMQ channels to come up
async fn start_kernel() -> (JupyterKernel, Client) {
let silent = true;
let kernel = if cfg!(feature = "test_evcxr") {
let kernel = if cfg!(feature = "test_ipython") {
JupyterKernel::ipython(silent)
} else if cfg!(feature = "test_evcxr") {
JupyterKernel::evcxr(silent)
} else if cfg!(feature = "test_irkernel") {
JupyterKernel::irkernel(silent)
} else if cfg!(feature = "test_deno") {
JupyterKernel::deno(silent)
} else {
JupyterKernel::ipython(silent)
panic!("For tests, choose one feature flag from: test_ipython, test_evcxr, test_irkernel, test_deno")
};
let client = Client::new(kernel.connection_info.clone()).await;
client.heartbeat().await;
Expand Down Expand Up @@ -53,7 +55,16 @@ async fn test_execute_request() {
let action = client.execute_request("2 + 2".to_string(), handlers).await;
action.await;
let counts = handler.counts.lock().await;
// status busy -> execute_input -> stream -> status idle & execute_reply
// All kernel types should give status busy -> status idle -> execute reply
assert_eq!(counts["status"], 2);
assert_eq!(counts["execute_reply"], 1);
// Python, Rust, and Deno will give execute_result on 2 + 2. R will give display_data.
#[cfg(any(
feature = "test_ipython",
feature = "test_evcxr",
feature = "test_deno"
))]
assert_eq!(counts["execute_result"], 1);
#[cfg(feature = "test_irkernel")]
assert_eq!(counts["display_data"], 1);
}