kernel.js
317 lines
| 9.3 KiB
| application/javascript
|
JavascriptLexer
Matthias BUSSONNIER
|
r14718 | |||
// | ||||
MinRK
|
r17640 | // Kernel tests | ||
Matthias BUSSONNIER
|
r14718 | // | ||
casper.notebook_test(function () { | ||||
Jessica B. Hamrick
|
r18212 | // test that the kernel is running | ||
Jessica B. Hamrick
|
r18210 | this.then(function () { | ||
Jessica B. Hamrick
|
r18212 | this.test.assert(this.kernel_running(), 'kernel is running'); | ||
Jessica B. Hamrick
|
r18210 | }); | ||
Jessica B. Hamrick
|
r18212 | // 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(); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.waitFor(this.kernel_disconnected); | ||
this.wait_for_kernel_ready(); | ||||
Jessica B. Hamrick
|
r18212 | this.then(function () { | ||
this.test.assert(this.kernel_running(), 'kernel restarted'); | ||||
}); | ||||
// test reconnect | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel.stop_channels(); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.waitFor(this.kernel_disconnected); | ||
Jessica B. Hamrick
|
r18212 | this.thenEvaluate(function () { | ||
IPython.notebook.kernel.reconnect(); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18212 | this.then(function () { | ||
this.test.assert(this.kernel_running(), 'kernel reconnected'); | ||||
}); | ||||
// test kernel_info_request | ||||
Matthias BUSSONNIER
|
r14718 | this.evaluate(function () { | ||
IPython.notebook.kernel.kernel_info( | ||||
function(msg){ | ||||
MinRK
|
r17640 | IPython._kernel_info_response = msg; | ||
Jessica B. Hamrick
|
r18210 | }); | ||
Matthias BUSSONNIER
|
r14718 | }); | ||
this.waitFor( | ||||
function () { | ||||
return this.evaluate(function(){ | ||||
return IPython._kernel_info_response; | ||||
}); | ||||
}); | ||||
Matthias BUSSONNIER
|
r14719 | this.then(function () { | ||
Matthias BUSSONNIER
|
r14722 | var kernel_info_response = this.evaluate(function(){ | ||
Matthias BUSSONNIER
|
r14719 | return IPython._kernel_info_response; | ||
Matthias BUSSONNIER
|
r14718 | }); | ||
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'); | ||||
Matthias BUSSONNIER
|
r14719 | }); | ||
Jessica B. Hamrick
|
r18212 | |||
// test kill | ||||
MinRK
|
r17640 | this.thenEvaluate(function () { | ||
Jessica B. Hamrick
|
r18210 | IPython.notebook.kernel.kill(); | ||
MinRK
|
r17640 | }); | ||
Jessica B. Hamrick
|
r18232 | this.waitFor(this.kernel_disconnected); | ||
MinRK
|
r17640 | this.then(function () { | ||
Jessica B. Hamrick
|
r18210 | this.test.assert(!this.kernel_running(), 'kernel is not running'); | ||
MinRK
|
r17640 | }); | ||
Jessica B. Hamrick
|
r18212 | |||
// test start | ||||
var url; | ||||
this.then(function () { | ||||
url = this.evaluate(function () { | ||||
return IPython.notebook.kernel.start(); | ||||
}); | ||||
}); | ||||
this.then(function () { | ||||
this.test.assertEquals(url, "/api/kernels", "start url is correct"); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18212 | this.then(function () { | ||
this.test.assert(this.kernel_running(), 'kernel is running'); | ||||
}); | ||||
// test start with parameters | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel.kill(); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.waitFor(this.kernel_disconnected); | ||
Jessica B. Hamrick
|
r18212 | this.then(function () { | ||
url = this.evaluate(function () { | ||||
return IPython.notebook.kernel.start({foo: "bar"}); | ||||
}); | ||||
}); | ||||
this.then(function () { | ||||
this.test.assertEquals(url, "/api/kernels?foo=bar", "start url with params is correct"); | ||||
}); | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18212 | this.then(function () { | ||
this.test.assert(this.kernel_running(), 'kernel is running'); | ||||
}); | ||||
Jessica B. Hamrick
|
r18223 | |||
// check for events in kill/start cycle | ||||
this.event_test( | ||||
'kill/start', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_killed.Kernel', | ||
Jessica B. Hamrick
|
r18230 | 'kernel_created.Kernel', | ||
Jessica B. Hamrick
|
r18238 | 'kernel_connected.Kernel', | ||
'kernel_starting.Kernel', | ||||
'kernel_ready.Kernel' | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
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 | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18223 | |||
// check for events in disconnect/connect cycle | ||||
this.event_test( | ||||
'reconnect', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_reconnecting.Kernel', | ||
'kernel_connected.Kernel', | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
function () { | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel.stop_channels(); | ||||
IPython.notebook.kernel.reconnect(1); | ||||
}); | ||||
} | ||||
); | ||||
// wait for any last idle/busy messages to be handled | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18223 | |||
// check for events in the restart cycle | ||||
this.event_test( | ||||
'restart', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_restarting.Kernel', | ||
Jessica B. Hamrick
|
r18230 | 'kernel_created.Kernel', | ||
Jessica B. Hamrick
|
r18238 | 'kernel_connected.Kernel', | ||
'kernel_starting.Kernel', | ||||
'kernel_ready.Kernel' | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
function () { | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel.restart(); | ||||
}); | ||||
} | ||||
); | ||||
// wait for any last idle/busy messages to be handled | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18223 | |||
// check for events in the interrupt cycle | ||||
this.event_test( | ||||
'interrupt', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_interrupting.Kernel', | ||
'kernel_busy.Kernel', | ||||
'kernel_idle.Kernel' | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
function () { | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel.interrupt(); | ||||
}); | ||||
} | ||||
); | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18223 | |||
// check for events after ws close | ||||
this.event_test( | ||||
'ws_closed_ok', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_disconnected.Kernel', | ||
'kernel_reconnecting.Kernel', | ||||
'kernel_connected.Kernel', | ||||
'kernel_busy.Kernel', | ||||
'kernel_idle.Kernel' | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
function () { | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel._ws_closed("", false); | ||||
}); | ||||
} | ||||
); | ||||
// wait for any last idle/busy messages to be handled | ||||
Jessica B. Hamrick
|
r18232 | this.wait_for_kernel_ready(); | ||
Jessica B. Hamrick
|
r18223 | |||
// check for events after ws close (error) | ||||
this.event_test( | ||||
'ws_closed_error', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_disconnected.Kernel', | ||
'kernel_connection_failed.Kernel' | ||||
Jessica B. Hamrick
|
r18223 | ], | ||
function () { | ||||
this.thenEvaluate(function () { | ||||
IPython.notebook.kernel._ws_closed("", true); | ||||
}); | ||||
} | ||||
); | ||||
Jessica B. Hamrick
|
r18233 | |||
// 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', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_restarting.Kernel', | ||
'kernel_autorestarting.Kernel', | ||||
Jessica B. Hamrick
|
r18233 | ], | ||
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', | ||||
[ | ||||
Jessica B. Hamrick
|
r18238 | 'kernel_restarting.Kernel', | ||
'kernel_autorestarting.Kernel', | ||||
Jessica B. Hamrick
|
r18233 | '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 | ||||
); | ||||
Matthias BUSSONNIER
|
r14718 | }); | ||