##// END OF EJS Templates
ensure kernels are cleaned up in embed_kernel tests
MinRK -
Show More
@@ -17,6 +17,7 b' import sys'
17 import tempfile
17 import tempfile
18 import time
18 import time
19
19
20 from contextlib import contextmanager
20 from subprocess import Popen, PIPE
21 from subprocess import Popen, PIPE
21
22
22 import nose.tools as nt
23 import nose.tools as nt
@@ -51,12 +52,13 b' def teardown():'
51 pass
52 pass
52
53
53
54
54 def _launch_kernel(cmd):
55 @contextmanager
56 def setup_kernel(cmd):
55 """start an embedded kernel in a subprocess, and wait for it to be ready
57 """start an embedded kernel in a subprocess, and wait for it to be ready
56
58
57 Returns
59 Returns
58 -------
60 -------
59 kernel, kernel_manager: Popen instance and connected KernelManager
61 kernel_manager: connected KernelManager instance
60 """
62 """
61 kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE, env=env)
63 kernel = Popen([sys.executable, '-c', cmd], stdout=PIPE, stderr=PIPE, env=env)
62 connection_file = os.path.join(IPYTHONDIR,
64 connection_file = os.path.join(IPYTHONDIR,
@@ -81,7 +83,10 b' def _launch_kernel(cmd):'
81 km.load_connection_file()
83 km.load_connection_file()
82 km.start_channels()
84 km.start_channels()
83
85
84 return kernel, km
86 try:
87 yield km
88 finally:
89 km.stop_channels()
85
90
86 def test_embed_kernel_basic():
91 def test_embed_kernel_basic():
87 """IPython.embed_kernel() is basically functional"""
92 """IPython.embed_kernel() is basically functional"""
@@ -95,26 +100,26 b' def test_embed_kernel_basic():'
95 '',
100 '',
96 ])
101 ])
97
102
98 kernel, km = _launch_kernel(cmd)
103 with setup_kernel(cmd) as km:
99 shell = km.shell_channel
104 shell = km.shell_channel
100
105
101 # oinfo a (int)
106 # oinfo a (int)
102 msg_id = shell.object_info('a')
107 msg_id = shell.object_info('a')
103 msg = shell.get_msg(block=True, timeout=2)
108 msg = shell.get_msg(block=True, timeout=2)
104 content = msg['content']
109 content = msg['content']
105 nt.assert_true(content['found'])
110 nt.assert_true(content['found'])
106
111
107 msg_id = shell.execute("c=a*2")
112 msg_id = shell.execute("c=a*2")
108 msg = shell.get_msg(block=True, timeout=2)
113 msg = shell.get_msg(block=True, timeout=2)
109 content = msg['content']
114 content = msg['content']
110 nt.assert_equals(content['status'], u'ok')
115 nt.assert_equals(content['status'], u'ok')
111
116
112 # oinfo c (should be 10)
117 # oinfo c (should be 10)
113 msg_id = shell.object_info('c')
118 msg_id = shell.object_info('c')
114 msg = shell.get_msg(block=True, timeout=2)
119 msg = shell.get_msg(block=True, timeout=2)
115 content = msg['content']
120 content = msg['content']
116 nt.assert_true(content['found'])
121 nt.assert_true(content['found'])
117 nt.assert_equals(content['string_form'], u'10')
122 nt.assert_equals(content['string_form'], u'10')
118
123
119 def test_embed_kernel_namespace():
124 def test_embed_kernel_namespace():
120 """IPython.embed_kernel() inherits calling namespace"""
125 """IPython.embed_kernel() inherits calling namespace"""
@@ -128,26 +133,26 b' def test_embed_kernel_namespace():'
128 '',
133 '',
129 ])
134 ])
130
135
131 kernel, km = _launch_kernel(cmd)
136 with setup_kernel(cmd) as km:
132 shell = km.shell_channel
137 shell = km.shell_channel
133
138
134 # oinfo a (int)
139 # oinfo a (int)
135 msg_id = shell.object_info('a')
140 msg_id = shell.object_info('a')
136 msg = shell.get_msg(block=True, timeout=2)
141 msg = shell.get_msg(block=True, timeout=2)
137 content = msg['content']
142 content = msg['content']
138 nt.assert_true(content['found'])
143 nt.assert_true(content['found'])
139 nt.assert_equals(content['string_form'], u'5')
144 nt.assert_equals(content['string_form'], u'5')
140
145
141 # oinfo b (str)
146 # oinfo b (str)
142 msg_id = shell.object_info('b')
147 msg_id = shell.object_info('b')
143 msg = shell.get_msg(block=True, timeout=2)
148 msg = shell.get_msg(block=True, timeout=2)
144 content = msg['content']
149 content = msg['content']
145 nt.assert_true(content['found'])
150 nt.assert_true(content['found'])
146 nt.assert_equals(content['string_form'], u'hi there')
151 nt.assert_equals(content['string_form'], u'hi there')
147
152
148 # oinfo c (undefined)
153 # oinfo c (undefined)
149 msg_id = shell.object_info('c')
154 msg_id = shell.object_info('c')
150 msg = shell.get_msg(block=True, timeout=2)
155 msg = shell.get_msg(block=True, timeout=2)
151 content = msg['content']
156 content = msg['content']
152 nt.assert_false(content['found'])
157 nt.assert_false(content['found'])
153
158
General Comments 0
You need to be logged in to leave comments. Login now