watchdog.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Watchdog from '../src/watchdog';
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  8. describe( 'Watchdog', () => {
  9. afterEach( () => sinon.restore() );
  10. describe( 'create()', () => {
  11. it( 'should create an editor instance', () => {
  12. const watchdog = new Watchdog();
  13. const editorCreateSpy = sinon.spy( VirtualTestEditor, 'create' );
  14. const editorDestroySpy = sinon.spy( VirtualTestEditor.prototype, 'destroy' );
  15. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  16. watchdog.setDestructor( editor => editor.destroy() );
  17. return watchdog.create( document.createElement( 'div' ), {} )
  18. .then( () => {
  19. sinon.assert.calledOnce( editorCreateSpy );
  20. sinon.assert.notCalled( editorDestroySpy );
  21. return watchdog.destroy();
  22. } )
  23. .then( () => {
  24. sinon.assert.calledOnce( editorCreateSpy );
  25. sinon.assert.calledOnce( editorDestroySpy );
  26. } )
  27. } );
  28. } );
  29. describe( 'restart()', () => {
  30. it( 'should restart the editor', () => {
  31. const watchdog = new Watchdog();
  32. const editorCreateSpy = sinon.spy( VirtualTestEditor, 'create' );
  33. const editorDestroySpy = sinon.spy( VirtualTestEditor.prototype, 'destroy' );
  34. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  35. watchdog.setDestructor( editor => editor.destroy() );
  36. return watchdog.create( document.createElement( 'div' ), {} )
  37. .then( () => {
  38. sinon.assert.calledOnce( editorCreateSpy );
  39. sinon.assert.notCalled( editorDestroySpy );
  40. return watchdog.restart();
  41. } )
  42. .then( () => {
  43. sinon.assert.calledTwice( editorCreateSpy );
  44. sinon.assert.calledOnce( editorDestroySpy );
  45. return watchdog.destroy();
  46. } );
  47. } );
  48. it( 'should restart the editor with the same data', () => {
  49. const watchdog = new Watchdog();
  50. // VirtualTestEditor doesn't handle the `config.initialData` properly.
  51. // See https://github.com/ckeditor/ckeditor5-core/issues/180.
  52. const FakeEditor = getFakeEditor();
  53. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  54. watchdog.setDestructor( editor => editor.destroy() );
  55. return watchdog.create( document.createElement( 'div' ), { initialData: 'foo' } )
  56. .then( () => {
  57. expect( watchdog.editor.getData() ).to.equal( 'foo' );
  58. return watchdog.restart();
  59. } )
  60. .then( () => {
  61. expect( watchdog.editor.getData() ).to.equal( 'foo' );
  62. return watchdog.destroy();
  63. } );
  64. } );
  65. } );
  66. describe( 'editor', () => {
  67. it( 'should be the current editor instance', () => {
  68. const watchdog = new Watchdog();
  69. watchdog.setCreator( ( el, config ) => VirtualTestEditor.create( el, config ) );
  70. watchdog.setDestructor( editor => editor.destroy() );
  71. expect( watchdog.editor ).to.be.null;
  72. let oldEditor;
  73. return watchdog.create( document.createElement( 'div' ), {} )
  74. .then( () => {
  75. oldEditor = watchdog.editor;
  76. expect( watchdog.editor ).to.be.instanceOf( VirtualTestEditor );
  77. return watchdog.restart();
  78. } )
  79. .then( () => {
  80. expect( watchdog.editor ).to.be.instanceOf( VirtualTestEditor );
  81. expect( watchdog.editor ).to.not.equal( oldEditor );
  82. return watchdog.destroy();
  83. } )
  84. .then( () => {
  85. expect( watchdog.editor ).to.be.null;
  86. } );
  87. } );
  88. } );
  89. describe( 'error handling', () => {
  90. it( 'Watchdog should not restart editor during the initialization', () => {
  91. const watchdog = new Watchdog();
  92. watchdog.setCreator( () => Promise.reject( new Error( 'foo' ) ) );
  93. watchdog.setDestructor( () => { } );
  94. return watchdog.create( document.createElement( 'div' ) ).then(
  95. () => { throw new Error( '`watchdog.create()` should throw an error.' ) },
  96. err => {
  97. expect( err ).to.be.instanceOf( Error );
  98. expect( err.message ).to.equal( 'foo' );
  99. }
  100. );
  101. } );
  102. it( 'Watchdog should not restart editor during the destroy', () => {
  103. const watchdog = new Watchdog();
  104. watchdog.setCreator( el => VirtualTestEditor.create( el ) );
  105. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  106. return Promise.resolve()
  107. .then( () => watchdog.create( document.createElement( 'div' ) ) )
  108. .then( () => watchdog.destroy() )
  109. .then(
  110. () => { throw new Error( '`watchdog.create()` should throw an error.' ) },
  111. err => {
  112. expect( err ).to.be.instanceOf( Error );
  113. expect( err.message ).to.equal( 'foo' );
  114. }
  115. );
  116. } );
  117. it( 'Watchdog should catch editor errors and restart the editor during the runtime', () => {
  118. const watchdog = new Watchdog();
  119. const FakeEditor = getFakeEditor();
  120. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  121. watchdog.setDestructor( editor => editor.destroy() );
  122. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  123. const originalErrorHandler = window.onerror;
  124. window.onerror = undefined;
  125. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  126. setTimeout( () => { watchdog.editor.throwEditorError() } );
  127. return new Promise( res => {
  128. watchdog.on( 'restart', () => {
  129. window.onerror = originalErrorHandler;
  130. res();
  131. } );
  132. } );
  133. } );
  134. } );
  135. it( 'Watchdog should not catch non-editor errors', () => {
  136. const watchdog = new Watchdog();
  137. const FakeEditor = getFakeEditor();
  138. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  139. watchdog.setDestructor( editor => editor.destroy() );
  140. const editorErrorSpy = sinon.spy();
  141. watchdog.on( 'error', editorErrorSpy );
  142. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  143. const originalErrorHandler = window.onerror;
  144. window.onerror = undefined;
  145. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  146. setTimeout( () => { throw new Error( 'foo' ); } );
  147. return new Promise( res => {
  148. setTimeout( () => {
  149. window.onerror = originalErrorHandler;
  150. sinon.assert.notCalled( editorErrorSpy );
  151. res();
  152. }, 5 );
  153. } );
  154. } );
  155. } );
  156. it( 'Watchdog should not catch other editor errors', () => {
  157. const watchdog1 = new Watchdog();
  158. const watchdog2 = new Watchdog();
  159. const FakeEditor1 = getFakeEditor();
  160. const FakeEditor2 = getFakeEditor();
  161. watchdog1.setCreator( ( el, config ) => FakeEditor1.create( el, config ) );
  162. watchdog1.setDestructor( editor => editor.destroy() );
  163. watchdog2.setCreator( ( el, config ) => FakeEditor2.create( el, config ) );
  164. watchdog2.setDestructor( editor => editor.destroy() );
  165. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  166. const originalErrorHandler = window.onerror;
  167. window.onerror = undefined;
  168. return Promise.all( [
  169. watchdog1.create( document.createElement( 'div' ) ),
  170. watchdog2.create( document.createElement( 'div' ) )
  171. ] ).then( () => {
  172. return new Promise( res => {
  173. let watchdog1ErrorSpy = sinon.spy();
  174. let watchdog2ErrorSpy = sinon.spy();
  175. watchdog1.on( 'restart', watchdog1ErrorSpy );
  176. watchdog2.on( 'restart', watchdog2ErrorSpy );
  177. setTimeout( () => {
  178. throw new CKEditorError( 'foo', undefined, watchdog2.editor );
  179. } );
  180. // TODO - timing.
  181. setTimeout( () => {
  182. window.onerror = originalErrorHandler;
  183. sinon.assert.notCalled( watchdog1ErrorSpy );
  184. sinon.assert.calledOnce( watchdog2ErrorSpy );
  185. res();
  186. }, 5 );
  187. } );
  188. } );
  189. } );
  190. it( 'Watchdog should catch editor errors and restart the editor during the runtime if the editor can be found from the ctx', () => {
  191. const watchdog = new Watchdog();
  192. const FakeEditor = getFakeEditor();
  193. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  194. watchdog.setDestructor( editor => editor.destroy() );
  195. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  196. const originalErrorHandler = window.onerror;
  197. window.onerror = undefined;
  198. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  199. setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor.model.document ); } );
  200. return new Promise( res => {
  201. watchdog.on( 'restart', () => {
  202. window.onerror = originalErrorHandler;
  203. res();
  204. } )
  205. } );
  206. } );
  207. } );
  208. it( 'Watchdog should catch editor errors and restart the editor during the runtime if the editor can be found from the ctx #2', () => {
  209. const watchdog = new Watchdog();
  210. const FakeEditor = getFakeEditor();
  211. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  212. watchdog.setDestructor( editor => editor.destroy() );
  213. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  214. const originalErrorHandler = window.onerror;
  215. window.onerror = undefined;
  216. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  217. setTimeout( () => { throw new CKEditorError( 'foo', undefined, { foo: [ 1, 2, 3, { bar: watchdog.editor } ] } ); } );
  218. return new Promise( res => {
  219. watchdog.on( 'restart', () => {
  220. window.onerror = originalErrorHandler;
  221. res();
  222. } )
  223. } );
  224. } );
  225. } );
  226. it( 'editor should be restarted maximum 3 times by default', () => {
  227. const watchdog = new Watchdog();
  228. const FakeEditor = getFakeEditor();
  229. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  230. watchdog.setDestructor( editor => editor.destroy() );
  231. const errorSpy = sinon.spy();
  232. watchdog.on( 'error', errorSpy );
  233. const restartSpy = sinon.spy();
  234. watchdog.on( 'restart', restartSpy );
  235. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  236. const originalErrorHandler = window.onerror;
  237. window.onerror = undefined;
  238. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  239. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  240. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  241. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  242. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  243. return new Promise( res => {
  244. setTimeout( () => {
  245. expect( errorSpy.callCount ).to.equal( 4 );
  246. expect( watchdog.crashes.length ).to.equal( 4 );
  247. expect( restartSpy.callCount ).to.equal( 3 );
  248. window.onerror = originalErrorHandler;
  249. res();
  250. }, 5 );
  251. } );
  252. } );
  253. } );
  254. it( 'editor should be restarted maximum of `crashNumberLimit` if the option is set', () => {
  255. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  256. const FakeEditor = getFakeEditor();
  257. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  258. watchdog.setDestructor( editor => editor.destroy() );
  259. const errorSpy = sinon.spy();
  260. watchdog.on( 'error', errorSpy );
  261. const restartSpy = sinon.spy();
  262. watchdog.on( 'restart', restartSpy );
  263. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  264. const originalErrorHandler = window.onerror;
  265. window.onerror = undefined;
  266. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  267. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  268. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  269. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  270. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  271. return new Promise( res => {
  272. setTimeout( () => {
  273. expect( errorSpy.callCount ).to.equal( 4 );
  274. expect( watchdog.crashes.length ).to.equal( 4 );
  275. expect( restartSpy.callCount ).to.equal( 2 );
  276. window.onerror = originalErrorHandler;
  277. res();
  278. }, 5 );
  279. } );
  280. } );
  281. } );
  282. it( 'editor should be reinitialized with the last data ', () => {
  283. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  284. const FakeEditor = getFakeEditor();
  285. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  286. watchdog.setDestructor( editor => editor.destroy() );
  287. const errorSpy = sinon.spy();
  288. watchdog.on( 'error', errorSpy );
  289. const restartSpy = sinon.spy();
  290. watchdog.on( 'restart', restartSpy );
  291. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  292. const originalErrorHandler = window.onerror;
  293. window.onerror = undefined;
  294. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  295. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  296. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  297. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  298. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  299. return new Promise( res => {
  300. setTimeout( () => {
  301. expect( errorSpy.callCount ).to.equal( 4 );
  302. expect( watchdog.crashes.length ).to.equal( 4 );
  303. expect( restartSpy.callCount ).to.equal( 2 );
  304. window.onerror = originalErrorHandler;
  305. res();
  306. }, 5 );
  307. } );
  308. } );
  309. } );
  310. } );
  311. describe( 'crashes', () => {
  312. it( 'should be an array of caught errors by the Watchdog', () => {
  313. const watchdog = new Watchdog();
  314. const FakeEditor = getFakeEditor();
  315. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  316. watchdog.setDestructor( editor => editor.destroy() );
  317. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  318. const originalErrorHandler = window.onerror;
  319. window.onerror = undefined;
  320. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  321. setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor ); } );
  322. setTimeout( () => { throw new CKEditorError( 'bar', undefined, watchdog.editor ); } );
  323. return new Promise( res => {
  324. // TODO - timing.
  325. setTimeout( () => {
  326. window.onerror = originalErrorHandler;
  327. expect( watchdog.crashes[ 0 ].message ).to.equal( 'Uncaught CKEditorError: foo' );
  328. expect( watchdog.crashes[ 1 ].message ).to.equal( 'Uncaught CKEditorError: bar' );
  329. res();
  330. }, 5 );
  331. } );
  332. } );
  333. } );
  334. } );
  335. } );
  336. function getFakeEditor() {
  337. return class FakeEditor {
  338. static create( el, config ) {
  339. const editor = new this();
  340. return Promise.resolve()
  341. .then( () => editor.create( el, config ) );
  342. }
  343. create( el, config = {} ) {
  344. this.el = el;
  345. this.config = config;
  346. this._data = config.initialData || '';
  347. return this;
  348. }
  349. constructor() {
  350. this.model = {
  351. document: {
  352. version: 0
  353. }
  354. };
  355. }
  356. getData() {
  357. return this._data;
  358. }
  359. destroy() {
  360. this.el.remove();
  361. return Promise.resolve();
  362. }
  363. throwEditorError() {
  364. throw new CKEditorError( 'foo', undefined, this );
  365. }
  366. }
  367. }