8
0

contextwatchdog.js 15 KB

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