watchdog.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 intercept 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 hide intercepted 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. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  141. const originalErrorHandler = window.onerror;
  142. const windowErrorSpy = sinon.spy();
  143. window.onerror = windowErrorSpy
  144. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  145. setTimeout( () => { watchdog.editor.throwEditorError() } );
  146. return new Promise( res => {
  147. watchdog.on( 'restart', () => {
  148. window.onerror = originalErrorHandler;
  149. sinon.assert.calledOnce( windowErrorSpy );
  150. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: foo' );
  151. res();
  152. } );
  153. } );
  154. } );
  155. } );
  156. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  157. const watchdog = new Watchdog();
  158. const FakeEditor = getFakeEditor();
  159. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  160. watchdog.setDestructor( editor => editor.destroy() );
  161. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  162. const originalErrorHandler = window.onerror;
  163. window.onerror = undefined;
  164. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  165. setTimeout( () => { watchdog.editor.throwEditorError() } );
  166. return new Promise( res => {
  167. watchdog.on( 'restart', () => {
  168. window.onerror = originalErrorHandler;
  169. res();
  170. } );
  171. } );
  172. } );
  173. } );
  174. it( 'Watchdog should not intercept non-editor errors', () => {
  175. const watchdog = new Watchdog();
  176. const FakeEditor = getFakeEditor();
  177. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  178. watchdog.setDestructor( editor => editor.destroy() );
  179. const editorErrorSpy = sinon.spy();
  180. watchdog.on( 'error', editorErrorSpy );
  181. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  182. const originalErrorHandler = window.onerror;
  183. window.onerror = undefined;
  184. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  185. setTimeout( () => { throw new Error( 'foo' ); } );
  186. return new Promise( res => {
  187. setTimeout( () => {
  188. window.onerror = originalErrorHandler;
  189. sinon.assert.notCalled( editorErrorSpy );
  190. res();
  191. }, 5 );
  192. } );
  193. } );
  194. } );
  195. it( 'Watchdog should not intercept other editor errors', () => {
  196. const watchdog1 = new Watchdog();
  197. const watchdog2 = new Watchdog();
  198. const FakeEditor1 = getFakeEditor();
  199. const FakeEditor2 = getFakeEditor();
  200. watchdog1.setCreator( ( el, config ) => FakeEditor1.create( el, config ) );
  201. watchdog1.setDestructor( editor => editor.destroy() );
  202. watchdog2.setCreator( ( el, config ) => FakeEditor2.create( el, config ) );
  203. watchdog2.setDestructor( editor => editor.destroy() );
  204. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  205. const originalErrorHandler = window.onerror;
  206. window.onerror = undefined;
  207. return Promise.all( [
  208. watchdog1.create( document.createElement( 'div' ) ),
  209. watchdog2.create( document.createElement( 'div' ) )
  210. ] ).then( () => {
  211. return new Promise( res => {
  212. let watchdog1ErrorSpy = sinon.spy();
  213. let watchdog2ErrorSpy = sinon.spy();
  214. watchdog1.on( 'restart', watchdog1ErrorSpy );
  215. watchdog2.on( 'restart', watchdog2ErrorSpy );
  216. setTimeout( () => {
  217. throw new CKEditorError( 'foo', undefined, watchdog2.editor );
  218. } );
  219. // TODO - timing.
  220. setTimeout( () => {
  221. window.onerror = originalErrorHandler;
  222. sinon.assert.notCalled( watchdog1ErrorSpy );
  223. sinon.assert.calledOnce( watchdog2ErrorSpy );
  224. res();
  225. }, 5 );
  226. } );
  227. } );
  228. } );
  229. it( 'Watchdog should intercept editor errors and restart the editor during the runtime if the editor can be found from the ctx', () => {
  230. const watchdog = new Watchdog();
  231. const FakeEditor = getFakeEditor();
  232. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  233. watchdog.setDestructor( editor => editor.destroy() );
  234. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  235. const originalErrorHandler = window.onerror;
  236. window.onerror = undefined;
  237. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  238. setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor.model.document ); } );
  239. return new Promise( res => {
  240. watchdog.on( 'restart', () => {
  241. window.onerror = originalErrorHandler;
  242. res();
  243. } )
  244. } );
  245. } );
  246. } );
  247. it( 'Watchdog should intercept editor errors and restart the editor during the runtime if the editor can be found from the ctx #2', () => {
  248. const watchdog = new Watchdog();
  249. const FakeEditor = getFakeEditor();
  250. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  251. watchdog.setDestructor( editor => editor.destroy() );
  252. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  253. const originalErrorHandler = window.onerror;
  254. window.onerror = undefined;
  255. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  256. setTimeout( () => { throw new CKEditorError( 'foo', undefined, { foo: [ 1, 2, 3, { bar: watchdog.editor } ] } ); } );
  257. return new Promise( res => {
  258. watchdog.on( 'restart', () => {
  259. window.onerror = originalErrorHandler;
  260. res();
  261. } )
  262. } );
  263. } );
  264. } );
  265. it( 'editor should be restarted maximum 3 times by default', () => {
  266. const watchdog = new Watchdog();
  267. const FakeEditor = getFakeEditor();
  268. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  269. watchdog.setDestructor( editor => editor.destroy() );
  270. const errorSpy = sinon.spy();
  271. watchdog.on( 'error', errorSpy );
  272. const restartSpy = sinon.spy();
  273. watchdog.on( 'restart', restartSpy );
  274. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  275. const originalErrorHandler = window.onerror;
  276. window.onerror = undefined;
  277. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  278. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  279. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  280. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  281. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  282. return new Promise( res => {
  283. setTimeout( () => {
  284. expect( errorSpy.callCount ).to.equal( 4 );
  285. expect( watchdog.crashes.length ).to.equal( 4 );
  286. expect( restartSpy.callCount ).to.equal( 3 );
  287. window.onerror = originalErrorHandler;
  288. res();
  289. }, 5 );
  290. } );
  291. } );
  292. } );
  293. it( 'editor should be restarted maximum of `crashNumberLimit` if the option is set', () => {
  294. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  295. const FakeEditor = getFakeEditor();
  296. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  297. watchdog.setDestructor( editor => editor.destroy() );
  298. const errorSpy = sinon.spy();
  299. watchdog.on( 'error', errorSpy );
  300. const restartSpy = sinon.spy();
  301. watchdog.on( 'restart', restartSpy );
  302. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  303. const originalErrorHandler = window.onerror;
  304. window.onerror = undefined;
  305. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  306. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  307. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  308. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  309. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  310. return new Promise( res => {
  311. setTimeout( () => {
  312. expect( errorSpy.callCount ).to.equal( 4 );
  313. expect( watchdog.crashes.length ).to.equal( 4 );
  314. expect( restartSpy.callCount ).to.equal( 2 );
  315. window.onerror = originalErrorHandler;
  316. res();
  317. }, 5 );
  318. } );
  319. } );
  320. } );
  321. it( 'editor should be reinitialized with the last data ', () => {
  322. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  323. const FakeEditor = getFakeEditor();
  324. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  325. watchdog.setDestructor( editor => editor.destroy() );
  326. const errorSpy = sinon.spy();
  327. watchdog.on( 'error', errorSpy );
  328. const restartSpy = sinon.spy();
  329. watchdog.on( 'restart', restartSpy );
  330. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  331. const originalErrorHandler = window.onerror;
  332. window.onerror = undefined;
  333. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  334. setTimeout( () => { throw new CKEditorError( 'foo1', undefined, watchdog.editor ); } );
  335. setTimeout( () => { throw new CKEditorError( 'foo2', undefined, watchdog.editor ); } );
  336. setTimeout( () => { throw new CKEditorError( 'foo3', undefined, watchdog.editor ); } );
  337. setTimeout( () => { throw new CKEditorError( 'foo4', undefined, watchdog.editor ); } );
  338. return new Promise( res => {
  339. setTimeout( () => {
  340. expect( errorSpy.callCount ).to.equal( 4 );
  341. expect( watchdog.crashes.length ).to.equal( 4 );
  342. expect( restartSpy.callCount ).to.equal( 2 );
  343. window.onerror = originalErrorHandler;
  344. res();
  345. }, 5 );
  346. } );
  347. } );
  348. } );
  349. } );
  350. describe( 'crashes', () => {
  351. it( 'should be an array of caught errors by the Watchdog', () => {
  352. const watchdog = new Watchdog();
  353. const FakeEditor = getFakeEditor();
  354. watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
  355. watchdog.setDestructor( editor => editor.destroy() );
  356. // sinon.stub( window, 'onerror' ).value( undefined ); and similar don't work.
  357. const originalErrorHandler = window.onerror;
  358. window.onerror = undefined;
  359. return watchdog.create( document.createElement( 'div' ) ).then( () => {
  360. setTimeout( () => { throw new CKEditorError( 'foo', undefined, watchdog.editor ); } );
  361. setTimeout( () => { throw new CKEditorError( 'bar', undefined, watchdog.editor ); } );
  362. return new Promise( res => {
  363. // TODO - timing.
  364. setTimeout( () => {
  365. window.onerror = originalErrorHandler;
  366. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  367. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  368. res();
  369. }, 5 );
  370. } );
  371. } );
  372. } );
  373. } );
  374. } );
  375. function getFakeEditor() {
  376. return class FakeEditor {
  377. static create( el, config ) {
  378. const editor = new this();
  379. return Promise.resolve()
  380. .then( () => editor.create( el, config ) );
  381. }
  382. create( el, config = {} ) {
  383. this.el = el;
  384. this.config = config;
  385. this._data = config.initialData || '';
  386. return this;
  387. }
  388. constructor() {
  389. this.model = {
  390. document: {
  391. version: 0
  392. }
  393. };
  394. }
  395. getData() {
  396. return this._data;
  397. }
  398. destroy() {
  399. this.el.remove();
  400. return Promise.resolve();
  401. }
  402. throwEditorError() {
  403. throw new CKEditorError( 'foo', undefined, this );
  404. }
  405. }
  406. }