|
1 | 1 | """Test IO capturing functionality""" |
2 | 2 |
|
3 | 3 | import io |
| 4 | +import warnings |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 | import zmq |
7 | 8 | from jupyter_client.session import Session |
8 | 9 |
|
9 | | -from ipykernel.iostream import IOPubThread, OutStream |
| 10 | +from ipykernel.iostream import MASTER, BackgroundSocket, IOPubThread, OutStream |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def test_io_api(): |
@@ -51,3 +52,57 @@ def test_io_isatty(): |
51 | 52 |
|
52 | 53 | stream = OutStream(session, thread, "stdout", isatty=True) |
53 | 54 | assert stream.isatty() |
| 55 | + |
| 56 | + |
| 57 | +def test_io_thread(): |
| 58 | + ctx = zmq.Context() |
| 59 | + pub = ctx.socket(zmq.PUB) |
| 60 | + thread = IOPubThread(pub) |
| 61 | + thread._setup_pipe_in() |
| 62 | + msg = [thread._pipe_uuid, b"a"] |
| 63 | + thread._handle_pipe_msg(msg) |
| 64 | + ctx1, pipe = thread._setup_pipe_out() |
| 65 | + pipe.close() |
| 66 | + thread._pipe_in.close() |
| 67 | + thread._check_mp_mode = lambda: MASTER |
| 68 | + thread._really_send([b"hi"]) |
| 69 | + ctx1.destroy() |
| 70 | + thread.close() |
| 71 | + thread.close() |
| 72 | + thread._really_send(None) |
| 73 | + |
| 74 | + |
| 75 | +def test_background_socket(): |
| 76 | + ctx = zmq.Context() |
| 77 | + pub = ctx.socket(zmq.PUB) |
| 78 | + thread = IOPubThread(pub) |
| 79 | + sock = BackgroundSocket(thread) |
| 80 | + assert sock.__class__ == BackgroundSocket |
| 81 | + with warnings.catch_warnings(): |
| 82 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 83 | + sock.linger = 101 |
| 84 | + assert thread.socket.linger == 101 |
| 85 | + assert sock.io_thread == thread |
| 86 | + sock.send(b"hi") |
| 87 | + |
| 88 | + |
| 89 | +def test_outstream(): |
| 90 | + session = Session() |
| 91 | + ctx = zmq.Context() |
| 92 | + pub = ctx.socket(zmq.PUB) |
| 93 | + thread = IOPubThread(pub) |
| 94 | + thread.start() |
| 95 | + |
| 96 | + with warnings.catch_warnings(): |
| 97 | + warnings.simplefilter("ignore", DeprecationWarning) |
| 98 | + stream = OutStream(session, pub, "stdout") |
| 99 | + stream = OutStream(session, thread, "stdout", pipe=object()) |
| 100 | + |
| 101 | + stream = OutStream(session, thread, "stdout", isatty=True, echo=io.StringIO()) |
| 102 | + with pytest.raises(io.UnsupportedOperation): |
| 103 | + stream.fileno() |
| 104 | + stream._watch_pipe_fd() |
| 105 | + stream.flush() |
| 106 | + stream.write("hi") |
| 107 | + stream.writelines(["ab", "cd"]) |
| 108 | + assert stream.writable |
0 commit comments