-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
kernel.js
325 lines (305 loc) · 9.62 KB
/
kernel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// Kernel tests
//
casper.notebook_test(function () {
// test that the kernel is running
this.then(function () {
this.test.assert(this.kernel_running(), 'kernel is running');
});
// test list
this.thenEvaluate(function () {
IPython._kernels = null;
IPython.notebook.kernel.list(function (data) {
IPython._kernels = data;
});
});
this.waitFor(function () {
return this.evaluate(function () {
return IPython._kernels !== null;
});
});
this.then(function () {
var num_kernels = this.evaluate(function () {
return IPython._kernels.length;
});
this.test.assertEquals(num_kernels, 1, 'one kernel running');
});
// test get_info
var kernel_info = this.evaluate(function () {
return {
name: IPython.notebook.kernel.name,
id: IPython.notebook.kernel.id
};
});
this.thenEvaluate(function () {
IPython._kernel_info = null;
IPython.notebook.kernel.get_info(function (data) {
IPython._kernel_info = data;
});
});
this.waitFor(function () {
return this.evaluate(function () {
return IPython._kernel_info !== null;
});
});
this.then(function () {
var new_kernel_info = this.evaluate(function () {
return IPython._kernel_info;
});
this.test.assertEquals(kernel_info.name, new_kernel_info.name, 'kernel: name correct');
this.test.assertEquals(kernel_info.id, new_kernel_info.id, 'kernel: id correct');
});
// test interrupt
this.thenEvaluate(function () {
IPython._interrupted = false;
IPython.notebook.kernel.interrupt(function () {
IPython._interrupted = true;
});
});
this.waitFor(function () {
return this.evaluate(function () {
return IPython._interrupted;
});
});
this.then(function () {
var interrupted = this.evaluate(function () {
return IPython._interrupted;
});
this.test.assert(interrupted, 'kernel was interrupted');
});
// test restart
this.thenEvaluate(function () {
IPython.notebook.kernel.restart();
});
this.waitFor(this.kernel_disconnected);
this.wait_for_kernel_ready();
this.then(function () {
this.test.assert(this.kernel_running(), 'kernel restarted');
});
// test reconnect
this.thenEvaluate(function () {
IPython.notebook.kernel.stop_channels();
});
this.waitFor(this.kernel_disconnected);
this.thenEvaluate(function () {
IPython.notebook.kernel.reconnect();
});
this.wait_for_kernel_ready();
this.then(function () {
this.test.assert(this.kernel_running(), 'kernel reconnected');
});
// test kernel_info_request
this.evaluate(function () {
IPython.notebook.kernel.kernel_info(
function(msg){
IPython._kernel_info_response = msg;
});
});
this.waitFor(
function () {
return this.evaluate(function(){
return IPython._kernel_info_response;
});
});
this.then(function () {
var kernel_info_response = this.evaluate(function(){
return IPython._kernel_info_response;
});
this.test.assertTrue( kernel_info_response.msg_type === 'kernel_info_reply', 'Kernel info request return kernel_info_reply');
this.test.assertTrue( kernel_info_response.content !== undefined, 'Kernel_info_reply is not undefined');
});
// test kill
this.thenEvaluate(function () {
IPython.notebook.kernel.kill();
});
this.waitFor(this.kernel_disconnected);
this.then(function () {
this.test.assert(!this.kernel_running(), 'kernel is not running');
});
// test start
var url, base_url;
this.then(function () {
base_url = this.evaluate(function () {
return IPython.notebook.base_url;
});
url = this.evaluate(function () {
return IPython.notebook.kernel.start();
});
});
this.then(function () {
this.test.assertEquals(url, base_url + "api/kernels", "start url is correct");
});
this.wait_for_kernel_ready();
this.then(function () {
this.test.assert(this.kernel_running(), 'kernel is running');
});
// test start with parameters
this.thenEvaluate(function () {
IPython.notebook.kernel.kill();
});
this.waitFor(this.kernel_disconnected);
this.then(function () {
url = this.evaluate(function () {
return IPython.notebook.kernel.start({foo: "bar"});
});
});
this.then(function () {
this.test.assertEquals(url, base_url + "api/kernels?foo=bar", "start url with params is correct");
});
this.wait_for_kernel_ready();
this.then(function () {
this.test.assert(this.kernel_running(), 'kernel is running');
});
// check for events in kill/start cycle
this.event_test(
'kill/start',
[
'kernel_killed.Kernel',
'kernel_starting.Kernel',
'kernel_created.Kernel',
'kernel_connected.Kernel',
'kernel_ready.Kernel'
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel.kill();
});
this.waitFor(this.kernel_disconnected);
this.thenEvaluate(function () {
IPython.notebook.kernel.start();
});
}
);
// wait for any last idle/busy messages to be handled
this.wait_for_kernel_ready();
// check for events in disconnect/connect cycle
this.event_test(
'reconnect',
[
'kernel_reconnecting.Kernel',
'kernel_connected.Kernel',
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel.stop_channels();
IPython.notebook.kernel.reconnect(1);
});
}
);
// wait for any last idle/busy messages to be handled
this.wait_for_kernel_ready();
// check for events in the restart cycle
this.event_test(
'restart',
[
'kernel_restarting.Kernel',
'kernel_created.Kernel',
'kernel_connected.Kernel',
'kernel_ready.Kernel'
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel.restart();
});
}
);
// wait for any last idle/busy messages to be handled
this.wait_for_kernel_ready();
// check for events in the interrupt cycle
this.event_test(
'interrupt',
[
'kernel_interrupting.Kernel',
'kernel_busy.Kernel',
'kernel_idle.Kernel'
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel.interrupt();
});
}
);
this.wait_for_kernel_ready();
// check for events after ws close
this.event_test(
'ws_closed_ok',
[
'kernel_disconnected.Kernel',
'kernel_reconnecting.Kernel',
'kernel_connected.Kernel',
'kernel_busy.Kernel',
'kernel_idle.Kernel'
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel._ws_closed("", false);
});
}
);
// wait for any last idle/busy messages to be handled
this.wait_for_kernel_ready();
// check for events after ws close (error)
this.event_test(
'ws_closed_error',
[
'kernel_disconnected.Kernel',
'kernel_connection_failed.Kernel',
'kernel_reconnecting.Kernel',
'kernel_connected.Kernel',
'kernel_busy.Kernel',
'kernel_idle.Kernel'
],
function () {
this.thenEvaluate(function () {
IPython.notebook.kernel._ws_closed("", true);
});
}
);
// wait for any last idle/busy messages to be handled
this.wait_for_kernel_ready();
// start the kernel back up
this.thenEvaluate(function () {
IPython.notebook.kernel.restart();
});
this.waitFor(this.kernel_running);
this.wait_for_kernel_ready();
// test handling of autorestarting messages
this.event_test(
'autorestarting',
[
'kernel_restarting.Kernel',
'kernel_autorestarting.Kernel',
],
function () {
this.thenEvaluate(function () {
var cell = IPython.notebook.get_cell(0);
cell.set_text('import os\n' + 'os._exit(1)');
cell.execute();
});
}
);
this.wait_for_kernel_ready();
// test handling of failed restart
this.event_test(
'failed_restart',
[
'kernel_restarting.Kernel',
'kernel_autorestarting.Kernel',
'kernel_dead.Kernel'
],
function () {
this.thenEvaluate(function () {
var cell = IPython.notebook.get_cell(0);
cell.set_text("import os\n" +
"from IPython.kernel.connect import get_connection_file\n" +
"with open(get_connection_file(), 'w') as f:\n" +
" f.write('garbage')\n" +
"os._exit(1)");
cell.execute();
});
},
// need an extra-long timeout, because it needs to try
// restarting the kernel 5 times!
20000
);
});