session.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Tests for the Session object
  3. //
  4. casper.notebook_test(function () {
  5. var that = this;
  6. var get_info = function () {
  7. return that.evaluate(function () {
  8. return JSON.parse(JSON.stringify(IPython.notebook.session._get_model()));
  9. });
  10. };
  11. // test that the kernel is running
  12. this.then(function () {
  13. this.test.assert(this.kernel_running(), 'session: kernel is running');
  14. });
  15. // test list
  16. this.thenEvaluate(function () {
  17. IPython._sessions = null;
  18. IPython.notebook.session.list(function (data) {
  19. IPython._sessions = data;
  20. });
  21. });
  22. this.waitFor(function () {
  23. return this.evaluate(function () {
  24. return IPython._sessions !== null;
  25. });
  26. });
  27. this.then(function () {
  28. var num_sessions = this.evaluate(function () {
  29. return IPython._sessions.length;
  30. });
  31. this.test.assertEquals(num_sessions, 1, 'one session running');
  32. });
  33. // test get_info
  34. var session_info = get_info();
  35. this.thenEvaluate(function () {
  36. IPython._session_info = null;
  37. IPython.notebook.session.get_info(function (data) {
  38. IPython._session_info = data;
  39. });
  40. });
  41. this.waitFor(function () {
  42. return this.evaluate(function () {
  43. return IPython._session_info !== null;
  44. });
  45. });
  46. this.then(function () {
  47. var new_session_info = this.evaluate(function () {
  48. return IPython._session_info;
  49. });
  50. this.test.assertEquals(session_info.name, new_session_info.name, 'session: notebook name correct');
  51. this.test.assertEquals(session_info.path, new_session_info.path, 'session: notebook path correct');
  52. this.test.assertEquals(session_info.kernel.name, new_session_info.kernel.name, 'session: kernel name correct');
  53. this.test.assertEquals(session_info.kernel.id, new_session_info.kernel.id, 'session: kernel id correct');
  54. });
  55. // test rename_notebook
  56. //
  57. // TODO: the PATCH request isn't supported by phantom, so this test always
  58. // fails, see https://github.com/ariya/phantomjs/issues/11384
  59. // when this is fixed we can properly run this test
  60. //
  61. // this.thenEvaluate(function () {
  62. // IPython._renamed = false;
  63. // IPython.notebook.session.rename_notebook(
  64. // "foo",
  65. // "bar",
  66. // function (data) {
  67. // IPython._renamed = true;
  68. // }
  69. // );
  70. // });
  71. // this.waitFor(function () {
  72. // return this.evaluate(function () {
  73. // return IPython._renamed;
  74. // });
  75. // });
  76. // this.then(function () {
  77. // var info = get_info();
  78. // this.test.assertEquals(info.notebook.name, "foo", "notebook was renamed");
  79. // this.test.assertEquals(info.notebook.path, "bar", "notebook path was changed");
  80. // });
  81. // test delete
  82. this.thenEvaluate(function () {
  83. IPython.notebook.session.delete();
  84. });
  85. this.waitFor(this.kernel_disconnected);
  86. this.then(function () {
  87. this.test.assert(!this.kernel_running(), 'session deletes kernel');
  88. });
  89. // check for events when starting the session
  90. this.event_test(
  91. 'start_session',
  92. [
  93. 'kernel_created.Session',
  94. 'kernel_connected.Kernel',
  95. 'kernel_ready.Kernel'
  96. ],
  97. function () {
  98. this.thenEvaluate(function () {
  99. IPython.notebook.session.start();
  100. });
  101. }
  102. );
  103. this.wait_for_kernel_ready();
  104. // check for events when killing the session
  105. this.event_test(
  106. 'delete_session',
  107. ['kernel_killed.Session'],
  108. function () {
  109. this.thenEvaluate(function () {
  110. IPython.notebook.session.delete();
  111. });
  112. }
  113. );
  114. this.thenEvaluate( function() {IPython.notebook.session.start()});
  115. this.wait_for_kernel_ready();
  116. // check for events when restarting the session
  117. this.event_test(
  118. 'restart_session',
  119. [
  120. 'kernel_killed.Session',
  121. 'kernel_created.Session',
  122. 'kernel_connected.Kernel',
  123. 'kernel_ready.Kernel'
  124. ],
  125. function () {
  126. this.thenEvaluate(function () {
  127. IPython.notebook.session.restart();
  128. });
  129. }
  130. );
  131. this.wait_for_kernel_ready();
  132. // test handling of failed restart
  133. this.event_test(
  134. 'failed_restart',
  135. [
  136. 'kernel_restarting.Kernel',
  137. 'kernel_autorestarting.Kernel',
  138. 'kernel_killed.Session',
  139. 'kernel_dead.Kernel',
  140. ],
  141. function () {
  142. this.thenEvaluate(function () {
  143. var cell = IPython.notebook.get_cell(0);
  144. cell.set_text("import os\n" +
  145. "from IPython.kernel.connect import get_connection_file\n" +
  146. "with open(get_connection_file(), 'w') as f:\n" +
  147. " f.write('garbage')\n" +
  148. "os._exit(1)");
  149. cell.execute();
  150. });
  151. },
  152. // need an extra-long timeout, because it needs to try
  153. // restarting the kernel 5 times!
  154. 20000
  155. );
  156. this.thenEvaluate( function() {IPython.notebook.session.start()});
  157. this.wait_for_kernel_ready();
  158. // check for events when starting a nonexistant kernel
  159. this.event_test(
  160. 'bad_start_session',
  161. [
  162. 'kernel_killed.Session',
  163. 'kernel_dead.Session'
  164. ],
  165. function () {
  166. this.thenEvaluate(function () {
  167. IPython.notebook.session.restart({kernel_name: 'foo'});
  168. });
  169. }
  170. );
  171. });