watchdog.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. describe( 'Watchdog', () => {
  11. afterEach( () => sinon.restore() );
  12. describe( 'create()', () => {
  13. it( 'should create an editor instance', () => {
  14. const watchdog = new Watchdog();
  15. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  16. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  17. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  18. watchdog.setDestructor( editor => editor.destroy() );
  19. return watchdog.create( document.createElement( 'div' ), {} )
  20. .then( () => {
  21. sinon.assert.calledOnce( editorCreateSpy );
  22. sinon.assert.notCalled( editorDestroySpy );
  23. return watchdog.destroy();
  24. } )
  25. .then( () => {
  26. sinon.assert.calledOnce( editorCreateSpy );
  27. sinon.assert.calledOnce( editorDestroySpy );
  28. } );
  29. } );
  30. it( 'should throw an error when the creator is not defined', () => {
  31. const watchdog = new Watchdog();
  32. watchdog.setDestructor( editor => editor.destroy() );
  33. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-creator-not-defined/ );
  34. } );
  35. it( 'should throw an error when the destructor is not defined', () => {
  36. const watchdog = new Watchdog();
  37. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  38. expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-destructor-not-defined/ );
  39. } );
  40. } );
  41. describe( 'restart()', () => {
  42. it( 'should restart the editor', () => {
  43. const watchdog = new Watchdog();
  44. const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
  45. const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
  46. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  47. watchdog.setDestructor( editor => editor.destroy() );
  48. return watchdog.create( document.createElement( 'div' ), {} )
  49. .then( () => {
  50. sinon.assert.calledOnce( editorCreateSpy );
  51. sinon.assert.notCalled( editorDestroySpy );
  52. return watchdog.restart();
  53. } )
  54. .then( () => {
  55. sinon.assert.calledTwice( editorCreateSpy );
  56. sinon.assert.calledOnce( editorDestroySpy );
  57. return watchdog.destroy();
  58. } );
  59. } );
  60. it( 'should restart the editor with the same data', () => {
  61. const watchdog = new Watchdog();
  62. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  63. watchdog.setDestructor( editor => editor.destroy() );
  64. return watchdog.create( document.createElement( 'div' ), {
  65. initialData: '<p>foo</p>',
  66. plugins: [ Paragraph ]
  67. } )
  68. .then( () => {
  69. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  70. return watchdog.restart();
  71. } )
  72. .then( () => {
  73. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  74. return watchdog.destroy();
  75. } );
  76. } );
  77. } );
  78. describe( 'editor', () => {
  79. it( 'should be the current editor instance', () => {
  80. const watchdog = new Watchdog();
  81. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  82. watchdog.setDestructor( editor => editor.destroy() );
  83. expect( watchdog.editor ).to.be.null;
  84. let oldEditor;
  85. return watchdog.create( document.createElement( 'div' ), {} )
  86. .then( () => {
  87. oldEditor = watchdog.editor;
  88. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  89. return watchdog.restart();
  90. } )
  91. .then( () => {
  92. expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
  93. expect( watchdog.editor ).to.not.equal( oldEditor );
  94. return watchdog.destroy();
  95. } )
  96. .then( () => {
  97. expect( watchdog.editor ).to.be.null;
  98. } );
  99. } );
  100. } );
  101. describe( 'error handling', () => {
  102. it( 'Watchdog should not restart editor during the initialization', () => {
  103. const watchdog = new Watchdog();
  104. watchdog.setCreator( el =>
  105. FakeEditor.create( el )
  106. .then( () => Promise.reject( new Error( 'foo' ) ) )
  107. );
  108. watchdog.setDestructor( editor => editor.destroy() );
  109. return watchdog.create( document.createElement( 'div' ) ).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 not restart editor during the destroy', () => {
  118. const watchdog = new Watchdog();
  119. watchdog.setCreator( el => FakeEditor.create( el ) );
  120. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  121. return Promise.resolve()
  122. .then( () => watchdog.create( document.createElement( 'div' ) ) )
  123. .then( () => watchdog.destroy() )
  124. .then(
  125. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  126. err => {
  127. expect( err ).to.be.instanceOf( Error );
  128. expect( err.message ).to.equal( 'foo' );
  129. }
  130. );
  131. } );
  132. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  133. const watchdog = new Watchdog();
  134. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  135. watchdog.setDestructor( editor => editor.destroy() );
  136. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  137. const originalErrorHandler = window.onerror;
  138. window.onerror = undefined;
  139. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  140. setTimeout( () => {
  141. throw new CKEditorError( 'foo', undefined, watchdog.editor );
  142. } );
  143. return new Promise( res => {
  144. watchdog.on( 'restart', () => {
  145. window.onerror = originalErrorHandler;
  146. watchdog.destroy().then( res );
  147. } );
  148. } );
  149. } );
  150. } );
  151. it( 'Watchdog should not hide intercepted errors', () => {
  152. const watchdog = new Watchdog();
  153. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  154. watchdog.setDestructor( editor => editor.destroy() );
  155. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  156. const originalErrorHandler = window.onerror;
  157. const windowErrorSpy = sinon.spy();
  158. window.onerror = windowErrorSpy;
  159. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  160. setTimeout( () => {
  161. throw new CKEditorError( 'foo', undefined, watchdog.editor );
  162. } );
  163. return new Promise( res => {
  164. watchdog.on( 'restart', () => {
  165. window.onerror = originalErrorHandler;
  166. sinon.assert.calledOnce( windowErrorSpy );
  167. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: foo' );
  168. watchdog.destroy().then( res );
  169. } );
  170. } );
  171. } );
  172. } );
  173. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  174. const watchdog = new Watchdog();
  175. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  176. watchdog.setDestructor( editor => editor.destroy() );
  177. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  178. const originalErrorHandler = window.onerror;
  179. window.onerror = undefined;
  180. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  181. setTimeout( () => {
  182. throw new CKEditorError( 'foo', undefined, watchdog.editor );
  183. } );
  184. return new Promise( res => {
  185. watchdog.on( 'restart', () => {
  186. window.onerror = originalErrorHandler;
  187. watchdog.destroy().then( res );
  188. } );
  189. } );
  190. } );
  191. } );
  192. it( 'Watchdog should not intercept non-editor errors', () => {
  193. const watchdog = new Watchdog();
  194. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  195. watchdog.setDestructor( editor => editor.destroy() );
  196. const editorErrorSpy = sinon.spy();
  197. watchdog.on( 'error', editorErrorSpy );
  198. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  199. const originalErrorHandler = window.onerror;
  200. window.onerror = undefined;
  201. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  202. setTimeout( () => {
  203. throw new Error( 'foo' );
  204. } );
  205. return new Promise( res => {
  206. setTimeout( () => {
  207. window.onerror = originalErrorHandler;
  208. sinon.assert.notCalled( editorErrorSpy );
  209. watchdog.destroy().then( res );
  210. }, 5 );
  211. } );
  212. } );
  213. } );
  214. it( 'Watchdog should not intercept other editor errors', () => {
  215. const watchdog1 = new Watchdog();
  216. const watchdog2 = new Watchdog();
  217. watchdog1.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  218. watchdog1.setDestructor( editor => editor.destroy() );
  219. watchdog2.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  220. watchdog2.setDestructor( editor => editor.destroy() );
  221. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  222. const originalErrorHandler = window.onerror;
  223. window.onerror = undefined;
  224. return Promise.all( [
  225. watchdog1.create( document.createElement( 'div' ) ),
  226. watchdog2.create( document.createElement( 'div' ) )
  227. ] ).then( () => {
  228. return new Promise( res => {
  229. const watchdog1ErrorSpy = sinon.spy();
  230. const watchdog2ErrorSpy = sinon.spy();
  231. watchdog1.on( 'restart', watchdog1ErrorSpy );
  232. watchdog2.on( 'restart', watchdog2ErrorSpy );
  233. setTimeout( () => {
  234. throw new CKEditorError( 'foo', undefined, watchdog2.editor );
  235. } );
  236. // TODO - timing.
  237. setTimeout( () => {
  238. window.onerror = originalErrorHandler;
  239. sinon.assert.notCalled( watchdog1ErrorSpy );
  240. sinon.assert.calledOnce( watchdog2ErrorSpy );
  241. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  242. .then( res );
  243. }, 5 );
  244. } );
  245. } );
  246. } );
  247. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the ctx', () => {
  248. const watchdog = new Watchdog();
  249. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  250. watchdog.setDestructor( editor => editor.destroy() );
  251. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  252. const originalErrorHandler = window.onerror;
  253. window.onerror = undefined;
  254. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  255. setTimeout( () => {
  256. throw new CKEditorError( 'foo', undefined, watchdog.editor.model.document );
  257. } );
  258. return new Promise( res => {
  259. watchdog.on( 'restart', () => {
  260. window.onerror = originalErrorHandler;
  261. watchdog.destroy().then( res );
  262. } );
  263. } );
  264. } );
  265. } );
  266. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the ctx #2', () => {
  267. const watchdog = new Watchdog();
  268. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  269. watchdog.setDestructor( editor => editor.destroy() );
  270. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  271. const originalErrorHandler = window.onerror;
  272. window.onerror = undefined;
  273. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  274. setTimeout( () => {
  275. throw new CKEditorError( 'foo', undefined, { foo: [ 1, 2, 3, { bar: watchdog.editor } ] } );
  276. } );
  277. return new Promise( res => {
  278. watchdog.on( 'restart', () => {
  279. window.onerror = originalErrorHandler;
  280. watchdog.destroy().then( res );
  281. } );
  282. } );
  283. } );
  284. } );
  285. it( 'editor should be restarted maximum 3 times by default', () => {
  286. const watchdog = new Watchdog();
  287. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  288. watchdog.setDestructor( editor => editor.destroy() );
  289. const errorSpy = sinon.spy();
  290. watchdog.on( 'error', errorSpy );
  291. const restartSpy = sinon.spy();
  292. watchdog.on( 'restart', restartSpy );
  293. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  294. const originalErrorHandler = window.onerror;
  295. window.onerror = undefined;
  296. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  297. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  298. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  299. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  300. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  301. return new Promise( res => {
  302. setTimeout( () => {
  303. expect( errorSpy.callCount ).to.equal( 4 );
  304. expect( watchdog.crashes.length ).to.equal( 4 );
  305. expect( restartSpy.callCount ).to.equal( 3 );
  306. window.onerror = originalErrorHandler;
  307. watchdog.destroy().then( res );
  308. }, 5 );
  309. } );
  310. } );
  311. } );
  312. it( 'editor should be restarted maximum of `crashNumberLimit` if the option is set', () => {
  313. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  314. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  315. watchdog.setDestructor( editor => editor.destroy() );
  316. const errorSpy = sinon.spy();
  317. watchdog.on( 'error', errorSpy );
  318. const restartSpy = sinon.spy();
  319. watchdog.on( 'restart', restartSpy );
  320. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  321. const originalErrorHandler = window.onerror;
  322. window.onerror = undefined;
  323. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  324. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  325. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  326. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  327. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  328. return new Promise( res => {
  329. setTimeout( () => {
  330. expect( errorSpy.callCount ).to.equal( 4 );
  331. expect( watchdog.crashes.length ).to.equal( 4 );
  332. expect( restartSpy.callCount ).to.equal( 2 );
  333. window.onerror = originalErrorHandler;
  334. watchdog.destroy().then( res );
  335. }, 5 );
  336. } );
  337. } );
  338. } );
  339. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  340. const watchdog = new Watchdog();
  341. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  342. watchdog.setDestructor( editor => editor.destroy() );
  343. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  344. const originalErrorHandler = window.onerror;
  345. window.onerror = undefined;
  346. sinon.stub( console, 'error' );
  347. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  348. setTimeout( () => throwCKEditorError( 'foo' ) );
  349. return new Promise( res => {
  350. setTimeout( () => {
  351. window.onerror = originalErrorHandler;
  352. expect( watchdog.crashes ).to.deep.equal( [] );
  353. sinon.assert.calledOnce( console.error );
  354. sinon.assert.calledWithExactly(
  355. console.error,
  356. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  357. );
  358. watchdog.destroy().then( res );
  359. }, 5 );
  360. } );
  361. } );
  362. } );
  363. it( 'editor should be restarted with the data before the crash #1', () => {
  364. const watchdog = new Watchdog();
  365. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  366. watchdog.setDestructor( editor => editor.destroy() );
  367. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  368. const originalErrorHandler = window.onerror;
  369. window.onerror = undefined;
  370. return watchdog.create( document.createElement( 'div' ), {
  371. initialData: '<p>foo</p>',
  372. plugins: [ Paragraph ]
  373. } ).then( () => {
  374. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  375. return new Promise( res => {
  376. watchdog.on( 'restart', () => {
  377. window.onerror = originalErrorHandler;
  378. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  379. watchdog.destroy().then( res );
  380. } );
  381. } );
  382. } );
  383. } );
  384. it( 'editor should be restarted with the data before the crash #2', () => {
  385. const watchdog = new Watchdog();
  386. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  387. watchdog.setDestructor( editor => editor.destroy() );
  388. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  389. const originalErrorHandler = window.onerror;
  390. window.onerror = undefined;
  391. return watchdog.create( document.createElement( 'div' ), {
  392. initialData: '<p>foo</p>',
  393. plugins: [ Paragraph ]
  394. } ).then( () => {
  395. const doc = watchdog.editor.model.document;
  396. watchdog.editor.model.change( writer => {
  397. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  398. } );
  399. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  400. return new Promise( res => {
  401. watchdog.on( 'restart', () => {
  402. window.onerror = originalErrorHandler;
  403. console.log( watchdog.editor.getData() );
  404. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  405. watchdog.destroy().then( res );
  406. } );
  407. } );
  408. } );
  409. } );
  410. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  411. const watchdog = new Watchdog();
  412. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  413. watchdog.setDestructor( editor => editor.destroy() );
  414. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  415. const originalErrorHandler = window.onerror;
  416. window.onerror = undefined;
  417. return watchdog.create( document.createElement( 'div' ), {
  418. initialData: '<p>foo</p>',
  419. plugins: [ Paragraph ]
  420. } ).then( () => {
  421. const doc = watchdog.editor.model.document;
  422. watchdog.editor.model.document.version = -1000;
  423. watchdog.editor.model.change( writer => {
  424. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  425. } );
  426. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  427. return new Promise( res => {
  428. watchdog.on( 'restart', () => {
  429. window.onerror = originalErrorHandler;
  430. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  431. watchdog.destroy().then( res );
  432. } );
  433. } );
  434. } );
  435. } );
  436. } );
  437. describe( 'crashes', () => {
  438. it( 'should be an array of caught errors by the Watchdog', () => {
  439. const watchdog = new Watchdog();
  440. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  441. watchdog.setDestructor( editor => editor.destroy() );
  442. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  443. const originalErrorHandler = window.onerror;
  444. window.onerror = undefined;
  445. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  446. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  447. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  448. return new Promise( res => {
  449. // TODO - timing.
  450. setTimeout( () => {
  451. window.onerror = originalErrorHandler;
  452. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  453. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  454. watchdog.destroy().then( res );
  455. }, 5 );
  456. } );
  457. } );
  458. } );
  459. } );
  460. } );
  461. // Wrap Editor to follow the API.
  462. class FakeEditor extends VirtualTestEditor {
  463. static create( elementOrData, config ) {
  464. return super.create( config );
  465. }
  466. }
  467. function throwCKEditorError( name, ctx ) {
  468. throw new CKEditorError( name, undefined, ctx );
  469. }