watchdog.js 19 KB

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