forked from ipython/ipykernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: there can be only one comm_manager (ipython#1049)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Steven Silvester <steven.silvester@ieee.org> Fixes ipython#1043
- Loading branch information
1 parent
4dc3033
commit 2c80e6c
Showing
4 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,87 @@ | ||
from ipykernel.comm import Comm | ||
from ipykernel.comm import Comm, CommManager | ||
from ipykernel.ipkernel import IPythonKernel | ||
|
||
|
||
async def test_comm(kernel): | ||
c = Comm() | ||
c.kernel = kernel # type:ignore | ||
def test_comm(kernel): | ||
manager = CommManager(kernel=kernel) | ||
kernel.comm_manager = manager | ||
|
||
c = Comm(kernel=kernel) | ||
msgs = [] | ||
|
||
def on_close(msg): | ||
msgs.append(msg) | ||
|
||
def on_message(msg): | ||
msgs.append(msg) | ||
|
||
c.publish_msg("foo") | ||
c.open({}) | ||
c.on_msg(on_message) | ||
c.on_close(on_close) | ||
c.handle_msg({}) | ||
c.handle_close({}) | ||
c.close() | ||
assert len(msgs) == 2 | ||
|
||
|
||
def test_comm_manager(kernel): | ||
manager = CommManager(kernel=kernel) | ||
msgs = [] | ||
|
||
def foo(comm, msg): | ||
msgs.append(msg) | ||
comm.close() | ||
|
||
def fizz(comm, msg): | ||
raise RuntimeError('hi') | ||
|
||
def on_close(msg): | ||
msgs.append(msg) | ||
|
||
def on_msg(msg): | ||
msgs.append(msg) | ||
|
||
manager.register_target("foo", foo) | ||
manager.register_target("fizz", fizz) | ||
|
||
kernel.comm_manager = manager | ||
comm = Comm() | ||
comm.on_msg(on_msg) | ||
comm.on_close(on_close) | ||
manager.register_comm(comm) | ||
|
||
assert manager.get_comm(comm.comm_id) == comm | ||
assert manager.get_comm('foo') is None | ||
|
||
msg = dict(content=dict(comm_id=comm.comm_id, target_name='foo')) | ||
manager.comm_open(None, None, msg) | ||
assert len(msgs) == 1 | ||
msg['content']['target_name'] = 'bar' | ||
manager.comm_open(None, None, msg) | ||
assert len(msgs) == 1 | ||
msg = dict(content=dict(comm_id=comm.comm_id, target_name='fizz')) | ||
manager.comm_open(None, None, msg) | ||
assert len(msgs) == 1 | ||
|
||
manager.register_comm(comm) | ||
assert manager.get_comm(comm.comm_id) == comm | ||
msg = dict(content=dict(comm_id=comm.comm_id)) | ||
manager.comm_msg(None, None, msg) | ||
assert len(msgs) == 2 | ||
msg['content']['comm_id'] = 'foo' | ||
manager.comm_msg(None, None, msg) | ||
assert len(msgs) == 2 | ||
|
||
manager.register_comm(comm) | ||
assert manager.get_comm(comm.comm_id) == comm | ||
msg = dict(content=dict(comm_id=comm.comm_id)) | ||
manager.comm_close(None, None, msg) | ||
assert len(msgs) == 3 | ||
|
||
assert comm._closed | ||
|
||
|
||
def test_comm_in_manager(ipkernel: IPythonKernel) -> None: | ||
comm = Comm() | ||
assert comm.comm_id in ipkernel.comm_manager.comms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters