Skip to content

Commit 84b17cf

Browse files
committed
Work on outputs processor tests.
1 parent 17f16d8 commit 84b17cf

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

jupyter_rtc_core/outputs/output_processor.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class OutputProcessor(LoggingConfigurable):
1111

1212
_cell_ids = Dict(default_value={}) # a map from msg_id -> cell_id
1313
_cell_indices = Dict(default_value={}) # a map from cell_id -> cell index in notebook
14-
_file_id = Unicode(default_value=None, allow_None=True)
14+
_file_id = Unicode(default_value=None, allow_none=True)
1515

1616
@property
1717
def settings(self):
@@ -83,19 +83,22 @@ def process_incoming_message(self, channel: str, msg: list[bytes]):
8383
When output messages are send back to the frontend, this map is used
8484
to find the cell_id for a given parent msg_id.
8585
"""
86-
header = json.loads(msg[0])
87-
msg_id = header["msg_id"]
88-
msg_type = header["msg_type"]
89-
metadata = json.loads(msg[2])
86+
if channel != "shell":
87+
return
88+
header = json.loads(msg[0]) # TODO use session unpack
89+
msg_type = header.get("msg_type")
90+
if msg_type != "execute_request":
91+
return
92+
msg_id = header.get("msg_id")
93+
metadata = json.loads(msg[2]) # TODO use session unpack
9094
cell_id = metadata.get("cellId")
9195
if cell_id is None:
9296
return
9397

9498
existing_msg_id = self.get_msg_id(cell_id)
9599
if existing_msg_id != msg_id: # cell is being re-run, clear output state
100+
self.clear(cell_id)
96101
if self._file_id is not None:
97-
self.log.info(f"Cell has been rerun, removing old outputs: {self._file_id} {cell_id}")
98-
self.clear(cell_id)
99102
self.outputs_manager.clear(file_id=self._file_id, cell_id=cell_id)
100103
self.log.info(f"Saving (msg_id, cell_id): ({msg_id} {cell_id})")
101104
self.set_cell_id(msg_id, cell_id)
Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
from ..outputs import OutputProcessor
1+
import json
2+
from pathlib import Path
3+
from tempfile import TemporaryDirectory
4+
from uuid import uuid4
5+
6+
from ..outputs import OutputProcessor, OutputsManager
7+
8+
class TestOutputProcessor(OutputProcessor):
9+
10+
settings = {}
11+
212

313
def test_instantiation():
4-
op = OutputProcessor()
14+
op = OutputProcessor()
15+
16+
def create_incoming_message(cell_id):
17+
msg_id = str(uuid4())
18+
header = {"msg_id": msg_id, "msg_type": "shell"}
19+
parent_header = {}
20+
metadata = {"cellId": cell_id}
21+
msg = [json.dumps(item) for item in [header, parent_header, metadata]]
22+
return msg_id, msg
23+
24+
def test_incoming_message():
25+
26+
with TemporaryDirectory() as td:
27+
op = TestOutputProcessor()
28+
om = OutputsManager()
29+
op.settings["outputs_manager"] = om
30+
op.outputs_path = Path(td) / "outputs"
31+
# Simulate the running of a cell
32+
cell_id = str(uuid4())
33+
msg_id, msg = create_incoming_message(cell_id)
34+
op.process_incoming_message('shell', msg)
35+
assert op.get_cell_id(msg_id) == cell_id
36+
assert op.get_msg_id(cell_id) == msg_id
37+
# # Clear the cell_id
38+
# op.clear(cell_id)
39+
# assert op.get_cell_id(msg_id) is None
40+
# assert op.get_msg_id(cell_id) is None
41+
# # Simulate the running of a cell
42+
# cell_id = str(uuid4())
43+
# msg_id, msg = create_incoming_message(cell_id)
44+
# op.process_incoming_message('shell', msg)
45+
# assert op.get_cell_id(msg_id) == cell_id
46+
# assert op.get_msg_id(cell_id) == msg_id
47+
# # Run it again without clearing to ensure it self clears
48+
# cell_id = str(uuid4())
49+
# msg_id, msg = create_incoming_message(cell_id)
50+
# assert op.get_cell_id(msg_id) == cell_id
51+
# assert op.get_msg_id(cell_id) == msg_id

0 commit comments

Comments
 (0)