contextwatchdog.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document, setTimeout, window, console */
  6. import ContextWatchdog from '../src/contextwatchdog';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import Context from '@ckeditor/ckeditor5-core/src/context';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. describe( 'ContextWatchdog', () => {
  11. let element1, element2;
  12. let watchdog;
  13. let originalErrorHandler;
  14. beforeEach( () => {
  15. element1 = document.createElement( 'div' );
  16. element2 = document.createElement( 'div' );
  17. document.body.appendChild( element1 );
  18. document.body.appendChild( element2 );
  19. originalErrorHandler = window.onerror;
  20. window.onerror = sinon.spy();
  21. } );
  22. afterEach( () => {
  23. window.onerror = originalErrorHandler;
  24. element1.remove();
  25. element2.remove();
  26. sinon.restore();
  27. } );
  28. it( 'should disable adding items once the Watchdog is destroyed', async () => {
  29. watchdog = ContextWatchdog.for( Context, {} );
  30. await watchdog.destroy();
  31. let err;
  32. try {
  33. await watchdog.add( {
  34. editor2: {
  35. type: 'editor',
  36. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  37. sourceElementOrData: element1,
  38. config: {}
  39. },
  40. } );
  41. } catch ( _err ) {
  42. err = _err;
  43. }
  44. expect( err ).to.be.instanceOf( Error );
  45. expect( err.message ).to.match( /Cannot add items to destroyed watchdog\./ );
  46. } );
  47. it.skip( 'case: editor, contextItem', async () => {
  48. watchdog = ContextWatchdog.for( Context, {} );
  49. watchdog.add( {
  50. editor1: {
  51. type: 'editor',
  52. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  53. sourceElementOrData: element1,
  54. config: {}
  55. },
  56. annotatedInput: {
  57. type: 'contextItem',
  58. creator: () => { }
  59. }
  60. } );
  61. await watchdog.waitForReady();
  62. await watchdog.destroy();
  63. } );
  64. describe( 'for scenario with no items', () => {
  65. it( 'should create only context', async () => {
  66. watchdog = ContextWatchdog.for( Context, {} );
  67. await watchdog.waitForReady();
  68. expect( watchdog.context ).to.be.instanceOf( Context );
  69. await watchdog.destroy();
  70. } );
  71. it( 'should have proper states', async () => {
  72. watchdog = ContextWatchdog.for( Context, {} );
  73. expect( watchdog.state ).to.equal( 'initializing' );
  74. await watchdog.waitForReady();
  75. expect( watchdog.state ).to.equal( 'ready' );
  76. await watchdog.destroy();
  77. expect( watchdog.state ).to.equal( 'destroyed' );
  78. } );
  79. it( 'should set custom destructor if provided', async () => {
  80. const mainWatchdog = new ContextWatchdog();
  81. const customDestructor = sinon.spy( context => context.destroy() );
  82. mainWatchdog.setCreator( config => Context.create( config ) );
  83. mainWatchdog.setDestructor( customDestructor );
  84. mainWatchdog.create();
  85. await mainWatchdog.destroy();
  86. sinon.assert.calledOnce( customDestructor );
  87. } );
  88. it( 'should log if an error happens during the component destructing', async () => {
  89. const mainWatchdog = new ContextWatchdog();
  90. const consoleErrorStub = sinon.stub( console, 'error' );
  91. const err = new Error( 'foo' );
  92. mainWatchdog.setCreator( config => Context.create( config ) );
  93. mainWatchdog.setDestructor( async editor => {
  94. await editor.destroy();
  95. throw err;
  96. } );
  97. await mainWatchdog.create();
  98. await mainWatchdog._restart();
  99. sinon.assert.calledWith(
  100. consoleErrorStub,
  101. 'An error happened during destructing.',
  102. err
  103. );
  104. mainWatchdog.setDestructor( editor => editor.destroy() );
  105. await mainWatchdog.destroy();
  106. } );
  107. it( 'should handle the Watchdog configuration', async () => {
  108. // TODO
  109. const mainWatchdog = ContextWatchdog.for( Context, {}, {
  110. crashNumberLimit: Infinity
  111. } );
  112. await mainWatchdog.create();
  113. await mainWatchdog.destroy();
  114. } );
  115. describe( 'in case of error handling', () => {
  116. it( 'should restart the `Context`', async () => {
  117. watchdog = ContextWatchdog.for( Context, {} );
  118. const errorSpy = sinon.spy();
  119. await watchdog.waitForReady();
  120. const oldContext = watchdog.context;
  121. watchdog.on( 'restart', errorSpy );
  122. setTimeout( () => throwCKEditorError( 'foo', watchdog.context ) );
  123. await waitCycle();
  124. sinon.assert.calledOnce( errorSpy );
  125. expect( watchdog.context ).to.not.equal( oldContext );
  126. } );
  127. } );
  128. } );
  129. describe( 'for multiple items scenario', () => {
  130. it( 'should allow adding multiple items without waiting for promises', async () => {
  131. watchdog = ContextWatchdog.for( Context, {} );
  132. watchdog.add( {
  133. editor1: {
  134. type: 'editor',
  135. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  136. sourceElementOrData: element1,
  137. config: {}
  138. },
  139. } );
  140. watchdog.add( {
  141. editor2: {
  142. type: 'editor',
  143. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  144. sourceElementOrData: element2,
  145. config: {}
  146. },
  147. } );
  148. await watchdog.waitForReady();
  149. await watchdog.destroy();
  150. } );
  151. it( 'should throw when multiple items with the same name are added', async () => {
  152. watchdog = ContextWatchdog.for( Context, {} );
  153. watchdog.add( {
  154. editor1: {
  155. type: 'editor',
  156. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  157. sourceElementOrData: element1,
  158. config: {}
  159. },
  160. } );
  161. await watchdog.waitForReady();
  162. let err;
  163. try {
  164. await watchdog.add( {
  165. editor1: {
  166. type: 'editor',
  167. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  168. sourceElementOrData: element1,
  169. config: {}
  170. },
  171. } );
  172. } catch ( _err ) {
  173. err = _err;
  174. }
  175. await watchdog.destroy();
  176. expect( err ).to.be.instanceOf( Error );
  177. expect( err.message ).to.match( /Item with the given name is already added: 'editor1'./ );
  178. } );
  179. it( 'should throw when not added item is removed', async () => {
  180. watchdog = ContextWatchdog.for( Context, {} );
  181. await watchdog.waitForReady();
  182. let err;
  183. try {
  184. await watchdog.remove( [ 'foo' ] );
  185. } catch ( _err ) {
  186. err = _err;
  187. }
  188. await watchdog.destroy();
  189. expect( err ).to.be.instanceOf( Error );
  190. expect( err.message ).to.match( /There is no watchdog named: 'foo'\./ );
  191. } );
  192. it( 'should throw when the item is added before the context is created', async () => {
  193. const mainWatchdog = new ContextWatchdog( {}, {} );
  194. let err;
  195. try {
  196. await mainWatchdog.add( {} );
  197. } catch ( _err ) {
  198. err = _err;
  199. }
  200. expect( err ).to.be.instanceOf( Error );
  201. expect( err.message ).to.match(
  202. /Context was not created yet\. You should call the `ContextWatchdog#create\(\)` method first\./
  203. );
  204. } );
  205. it( 'should allow setting editor custom destructors', async () => {
  206. watchdog = ContextWatchdog.for( Context, {} );
  207. const destructorSpy = sinon.spy( editor => editor.destroy() );
  208. watchdog.add( {
  209. editor1: {
  210. type: 'editor',
  211. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  212. destructor: destructorSpy,
  213. sourceElementOrData: element1,
  214. config: {},
  215. },
  216. } );
  217. await watchdog.destroy();
  218. sinon.assert.calledOnce( destructorSpy );
  219. } );
  220. it( 'should throw when the item is of not known type', async () => {
  221. watchdog = ContextWatchdog.for( Context, {} );
  222. await watchdog.waitForReady();
  223. let err;
  224. try {
  225. await watchdog.add( {
  226. editor1: {
  227. type: 'foo',
  228. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  229. sourceElementOrData: element1,
  230. config: {}
  231. },
  232. } );
  233. } catch ( _err ) {
  234. watchdog._stopErrorHandling();
  235. err = _err;
  236. }
  237. await watchdog.destroy();
  238. expect( err ).to.be.instanceOf( Error );
  239. expect( err.message ).to.match( /Not supported item type: 'foo'\./ );
  240. } );
  241. it( 'should allow adding and removing items without waiting', async () => {
  242. watchdog = ContextWatchdog.for( Context, {} );
  243. watchdog.add( {
  244. editor1: {
  245. type: 'editor',
  246. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  247. sourceElementOrData: element1,
  248. config: {}
  249. },
  250. } );
  251. watchdog.add( {
  252. editor2: {
  253. type: 'editor',
  254. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  255. sourceElementOrData: element2,
  256. config: {}
  257. },
  258. } );
  259. await watchdog.waitForReady();
  260. expect( watchdog.state ).to.equal( 'ready' );
  261. watchdog.remove( [ 'editor1' ] );
  262. await watchdog.waitForReady();
  263. watchdog.remove( [ 'editor2' ] );
  264. await watchdog.waitForReady();
  265. await watchdog.destroy();
  266. } );
  267. it( 'should not change the input items', async () => {
  268. watchdog = ContextWatchdog.for( Context, {} );
  269. watchdog.add( Object.freeze( {
  270. editor1: {
  271. type: 'editor',
  272. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  273. sourceElementOrData: element1,
  274. config: {}
  275. }
  276. } ) );
  277. watchdog._restart();
  278. await watchdog.waitForReady();
  279. await watchdog.destroy();
  280. } );
  281. it( 'should return the created items instances with get( itemName )', async () => {
  282. watchdog = ContextWatchdog.for( Context, {} );
  283. watchdog.add( {
  284. editor1: {
  285. type: 'editor',
  286. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  287. sourceElementOrData: element1,
  288. config: {}
  289. },
  290. editor2: {
  291. type: 'editor',
  292. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  293. sourceElementOrData: element2,
  294. config: {}
  295. }
  296. } );
  297. await watchdog.waitForReady();
  298. expect( watchdog.get( 'editor1' ) ).to.be.instanceOf( ClassicTestEditor );
  299. expect( watchdog.get( 'editor2' ) ).to.be.instanceOf( ClassicTestEditor );
  300. await watchdog.remove( [ 'editor1' ] );
  301. expect( () => {
  302. watchdog.get( 'editor1' );
  303. } ).to.throw( /Item with the given name was not registered: editor1\./ );
  304. expect( watchdog.get( 'editor2' ) ).to.be.instanceOf( ClassicTestEditor );
  305. await watchdog.destroy();
  306. } );
  307. describe( 'in case of error handling', () => {
  308. it( 'should restart the whole structure of editors if an error happens inside the `Context`', async () => {
  309. watchdog = ContextWatchdog.for( Context, {} );
  310. watchdog.add( {
  311. editor1: {
  312. type: 'editor',
  313. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  314. sourceElementOrData: element1,
  315. config: {}
  316. }
  317. } );
  318. await watchdog.waitForReady();
  319. const oldContext = watchdog.context;
  320. const restartSpy = sinon.spy();
  321. watchdog.on( 'restart', restartSpy );
  322. setTimeout( () => throwCKEditorError( 'foo', watchdog.context ) );
  323. await waitCycle();
  324. sinon.assert.calledOnce( restartSpy );
  325. expect( watchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
  326. expect( watchdog.context ).to.not.equal( oldContext );
  327. await watchdog.destroy();
  328. } );
  329. it( 'should restart only the editor if an error happens inside the editor', async () => {
  330. watchdog = ContextWatchdog.for( Context, {} );
  331. watchdog.add( {
  332. editor1: {
  333. type: 'editor',
  334. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  335. sourceElementOrData: element1,
  336. config: {}
  337. }
  338. } );
  339. await watchdog.waitForReady();
  340. const oldContext = watchdog.context;
  341. const restartSpy = sinon.spy();
  342. const oldEditor = watchdog.get( 'editor1' );
  343. watchdog.on( 'restart', restartSpy );
  344. setTimeout( () => throwCKEditorError( 'foo', oldEditor ) );
  345. await waitCycle();
  346. sinon.assert.notCalled( restartSpy );
  347. expect( watchdog.context ).to.equal( oldContext );
  348. expect( watchdog.get( 'editor1' ) ).to.not.equal( oldEditor );
  349. await watchdog.destroy();
  350. } );
  351. it( 'should restart only the editor if an error happens inside one of the editors', async () => {
  352. watchdog = ContextWatchdog.for( Context, {} );
  353. watchdog.add( {
  354. editor1: {
  355. type: 'editor',
  356. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  357. sourceElementOrData: element1,
  358. config: {}
  359. },
  360. editor2: {
  361. type: 'editor',
  362. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  363. sourceElementOrData: element2,
  364. config: {}
  365. }
  366. } );
  367. await watchdog.waitForReady();
  368. const oldContext = watchdog.context;
  369. const editorWatchdog1 = watchdog._watchdogs.get( 'editor1' );
  370. const editorWatchdog2 = watchdog._watchdogs.get( 'editor2' );
  371. const oldEditor1 = watchdog.get( 'editor1' );
  372. const oldEditor2 = watchdog.get( 'editor2' );
  373. const mainWatchdogRestartSpy = sinon.spy();
  374. const editorWatchdog1RestartSpy = sinon.spy();
  375. const editorWatchdog2RestartSpy = sinon.spy();
  376. watchdog.on( 'restart', mainWatchdogRestartSpy );
  377. editorWatchdog1.on( 'restart', editorWatchdog1RestartSpy );
  378. editorWatchdog2.on( 'restart', editorWatchdog2RestartSpy );
  379. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog1.editor ) );
  380. await waitCycle();
  381. sinon.assert.calledOnce( editorWatchdog1RestartSpy );
  382. sinon.assert.notCalled( mainWatchdogRestartSpy );
  383. sinon.assert.notCalled( editorWatchdog2RestartSpy );
  384. expect( editorWatchdog1.state ).to.equal( 'ready' );
  385. expect( editorWatchdog2.state ).to.equal( 'ready' );
  386. expect( watchdog.state ).to.equal( 'ready' );
  387. expect( oldEditor1 ).to.not.equal( editorWatchdog1.editor );
  388. expect( oldEditor2 ).to.equal( editorWatchdog2.editor );
  389. expect( watchdog.context ).to.equal( oldContext );
  390. await watchdog.destroy();
  391. } );
  392. it( 'should handle removing and restarting at the same time', async () => {
  393. watchdog = ContextWatchdog.for( Context, {} );
  394. watchdog.add( {
  395. editor1: {
  396. type: 'editor',
  397. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  398. sourceElementOrData: element1,
  399. config: {}
  400. },
  401. editor2: {
  402. type: 'editor',
  403. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  404. sourceElementOrData: element2,
  405. config: {}
  406. }
  407. } );
  408. await watchdog.waitForReady();
  409. const editor1 = watchdog.get( 'editor1' );
  410. watchdog.remove( [ 'editor1' ] );
  411. setTimeout( () => throwCKEditorError( 'foo', editor1 ) );
  412. await waitCycle();
  413. await watchdog.waitForReady();
  414. expect( [ ...watchdog._watchdogs.keys() ] ).to.include( 'editor2' );
  415. expect( [ ...watchdog._watchdogs.keys() ] ).to.not.include( 'editor1' );
  416. await watchdog.destroy();
  417. } );
  418. it( 'should handle restarting the item instance many times', async () => {
  419. watchdog = ContextWatchdog.for( Context, {} );
  420. watchdog.add( {
  421. editor1: {
  422. type: 'editor',
  423. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  424. sourceElementOrData: element1,
  425. config: {}
  426. },
  427. editor2: {
  428. type: 'editor',
  429. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  430. sourceElementOrData: element2,
  431. config: {}
  432. }
  433. } );
  434. await watchdog.waitForReady();
  435. const editorWatchdog = watchdog._watchdogs.get( 'editor1' );
  436. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
  437. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
  438. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
  439. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog.editor ) );
  440. await waitCycle();
  441. expect( editorWatchdog.state ).to.equal( 'crashedPermanently' );
  442. expect( watchdog.state ).to.equal( 'ready' );
  443. await watchdog.destroy();
  444. } );
  445. } );
  446. } );
  447. } );
  448. function throwCKEditorError( name, context ) {
  449. throw new CKEditorError( name, context );
  450. }
  451. function waitCycle() {
  452. return new Promise( res => setTimeout( res ) );
  453. }