contextwatchdog.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 mainWatchdog;
  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. mainWatchdog = ContextWatchdog.for( Context, {} );
  30. await mainWatchdog.destroy();
  31. let err;
  32. try {
  33. await mainWatchdog.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. mainWatchdog = ContextWatchdog.for( Context, {} );
  49. mainWatchdog.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 mainWatchdog.waitForReady();
  62. await mainWatchdog.destroy();
  63. } );
  64. describe( 'for scenario with no items', () => {
  65. it( 'should create only context', async () => {
  66. mainWatchdog = ContextWatchdog.for( Context, {} );
  67. await mainWatchdog.waitForReady();
  68. expect( mainWatchdog.context ).to.be.instanceOf( Context );
  69. await mainWatchdog.destroy();
  70. } );
  71. it( 'should have proper states', async () => {
  72. mainWatchdog = ContextWatchdog.for( Context, {} );
  73. expect( mainWatchdog.state ).to.equal( 'initializing' );
  74. await mainWatchdog.waitForReady();
  75. expect( mainWatchdog.state ).to.equal( 'ready' );
  76. await mainWatchdog.destroy();
  77. expect( mainWatchdog.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. mainWatchdog = ContextWatchdog.for( Context, {} );
  118. const errorSpy = sinon.spy();
  119. await mainWatchdog.waitForReady();
  120. const oldContext = mainWatchdog.context;
  121. mainWatchdog.on( 'restart', errorSpy );
  122. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  123. await waitCycle();
  124. sinon.assert.calledOnce( errorSpy );
  125. expect( mainWatchdog.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. mainWatchdog = ContextWatchdog.for( Context, {} );
  132. mainWatchdog.add( {
  133. editor1: {
  134. type: 'editor',
  135. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  136. sourceElementOrData: element1,
  137. config: {}
  138. },
  139. } );
  140. mainWatchdog.add( {
  141. editor2: {
  142. type: 'editor',
  143. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  144. sourceElementOrData: element2,
  145. config: {}
  146. },
  147. } );
  148. await mainWatchdog.waitForReady();
  149. await mainWatchdog.destroy();
  150. } );
  151. it( 'should throw when multiple items with the same name are added', async () => {
  152. mainWatchdog = ContextWatchdog.for( Context, {} );
  153. mainWatchdog.add( {
  154. editor1: {
  155. type: 'editor',
  156. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  157. sourceElementOrData: element1,
  158. config: {}
  159. },
  160. } );
  161. await mainWatchdog.waitForReady();
  162. let err;
  163. try {
  164. await mainWatchdog.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. mainWatchdog._actionQueue._clear();
  176. await mainWatchdog.destroy();
  177. expect( err ).to.be.instanceOf( Error );
  178. expect( err.message ).to.match( /Watchdog with the given name is already added: 'editor1'./ );
  179. } );
  180. it( 'should throw when not added item is removed', async () => {
  181. mainWatchdog = ContextWatchdog.for( Context, {} );
  182. await mainWatchdog.waitForReady();
  183. let err;
  184. try {
  185. await mainWatchdog.remove( [ 'foo' ] );
  186. } catch ( _err ) {
  187. err = _err;
  188. }
  189. mainWatchdog._actionQueue._clear();
  190. await mainWatchdog.destroy();
  191. expect( err ).to.be.instanceOf( Error );
  192. expect( err.message ).to.match( /There is no watchdog named: 'foo'\./ );
  193. } );
  194. it( 'should throw when the item is added before the context is created', async () => {
  195. const mainWatchdog = new ContextWatchdog( {}, {} );
  196. let err;
  197. try {
  198. await mainWatchdog.add( {} );
  199. } catch ( _err ) {
  200. err = _err;
  201. }
  202. expect( err ).to.be.instanceOf( Error );
  203. expect( err.message ).to.match(
  204. /Context was not created yet\. You should call the `ContextWatchdog#create\(\)` method first\./
  205. );
  206. } );
  207. it( 'should allow setting editor custom destructors', async () => {
  208. mainWatchdog = ContextWatchdog.for( Context, {} );
  209. const destructorSpy = sinon.spy( editor => editor.destroy() );
  210. mainWatchdog.add( {
  211. editor1: {
  212. type: 'editor',
  213. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  214. destructor: destructorSpy,
  215. sourceElementOrData: element1,
  216. config: {},
  217. },
  218. } );
  219. await mainWatchdog.destroy();
  220. sinon.assert.calledOnce( destructorSpy );
  221. } );
  222. it( 'should throw when the item is of not known type', async () => {
  223. mainWatchdog = ContextWatchdog.for( Context, {} );
  224. await mainWatchdog.waitForReady();
  225. let err;
  226. try {
  227. await mainWatchdog.add( {
  228. editor1: {
  229. type: 'foo',
  230. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  231. sourceElementOrData: element1,
  232. config: {}
  233. },
  234. } );
  235. } catch ( _err ) {
  236. mainWatchdog._stopErrorHandling();
  237. err = _err;
  238. }
  239. mainWatchdog._actionQueue._clear();
  240. await mainWatchdog.destroy();
  241. expect( err ).to.be.instanceOf( Error );
  242. expect( err.message ).to.match( /Not supported item type: 'foo'\./ );
  243. } );
  244. it( 'should allow adding and removing items without waiting', async () => {
  245. mainWatchdog = ContextWatchdog.for( Context, {} );
  246. mainWatchdog.add( {
  247. editor1: {
  248. type: 'editor',
  249. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  250. sourceElementOrData: element1,
  251. config: {}
  252. },
  253. } );
  254. mainWatchdog.add( {
  255. editor2: {
  256. type: 'editor',
  257. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  258. sourceElementOrData: element2,
  259. config: {}
  260. },
  261. } );
  262. await mainWatchdog.waitForReady();
  263. expect( mainWatchdog.state ).to.equal( 'ready' );
  264. mainWatchdog.remove( [ 'editor1' ] );
  265. await mainWatchdog.waitForReady();
  266. mainWatchdog.remove( [ 'editor2' ] );
  267. await mainWatchdog.waitForReady();
  268. await mainWatchdog.destroy();
  269. } );
  270. it( 'should not change the input items', async () => {
  271. mainWatchdog = ContextWatchdog.for( Context, {} );
  272. mainWatchdog.add( Object.freeze( {
  273. editor1: {
  274. type: 'editor',
  275. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  276. sourceElementOrData: element1,
  277. config: {}
  278. }
  279. } ) );
  280. mainWatchdog._restart();
  281. await mainWatchdog.waitForReady();
  282. await mainWatchdog.destroy();
  283. } );
  284. describe( 'in case of error handling', () => {
  285. it( 'should restart the whole structure of editors if an error happens inside the `Context`', async () => {
  286. mainWatchdog = ContextWatchdog.for( Context, {} );
  287. mainWatchdog.add( {
  288. editor1: {
  289. type: 'editor',
  290. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  291. sourceElementOrData: element1,
  292. config: {}
  293. }
  294. } );
  295. await mainWatchdog.waitForReady();
  296. const oldContext = mainWatchdog.context;
  297. const restartSpy = sinon.spy();
  298. mainWatchdog.on( 'restart', restartSpy );
  299. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  300. await waitCycle();
  301. sinon.assert.calledOnce( restartSpy );
  302. expect( mainWatchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
  303. expect( mainWatchdog.context ).to.not.equal( oldContext );
  304. await mainWatchdog.destroy();
  305. } );
  306. it( 'should restart only the editor if an error happens inside the editor', async () => {
  307. mainWatchdog = ContextWatchdog.for( Context, {} );
  308. mainWatchdog.add( {
  309. editor1: {
  310. type: 'editor',
  311. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  312. sourceElementOrData: element1,
  313. config: {}
  314. }
  315. } );
  316. await mainWatchdog.waitForReady();
  317. const oldContext = mainWatchdog.context;
  318. const restartSpy = sinon.spy();
  319. const oldEditor = mainWatchdog.getWatchdog( 'editor1' ).editor;
  320. mainWatchdog.on( 'restart', restartSpy );
  321. setTimeout( () => throwCKEditorError( 'foo', oldEditor ) );
  322. await waitCycle();
  323. sinon.assert.notCalled( restartSpy );
  324. expect( mainWatchdog.context ).to.equal( oldContext );
  325. expect( mainWatchdog.getWatchdog( 'editor1' ).editor ).to.not.equal( oldEditor );
  326. expect( mainWatchdog.getWatchdog( 'editor1' ).state ).to.equal( 'ready' );
  327. await mainWatchdog.destroy();
  328. } );
  329. it( 'should restart only the editor if an error happens inside one of the editors', async () => {
  330. mainWatchdog = ContextWatchdog.for( Context, {} );
  331. mainWatchdog.add( {
  332. editor1: {
  333. type: 'editor',
  334. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  335. sourceElementOrData: element1,
  336. config: {}
  337. },
  338. editor2: {
  339. type: 'editor',
  340. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  341. sourceElementOrData: element1,
  342. config: {}
  343. }
  344. } );
  345. await mainWatchdog.waitForReady();
  346. const oldContext = mainWatchdog.context;
  347. const editorWatchdog1 = mainWatchdog.getWatchdog( 'editor1' );
  348. const editorWatchdog2 = mainWatchdog.getWatchdog( 'editor2' );
  349. const oldEditor1 = editorWatchdog1.editor;
  350. const oldEditor2 = editorWatchdog2.editor;
  351. const mainWatchdogRestartSpy = sinon.spy();
  352. const editorWatchdog1RestartSpy = sinon.spy();
  353. const editorWatchdog2RestartSpy = sinon.spy();
  354. mainWatchdog.on( 'restart', mainWatchdogRestartSpy );
  355. editorWatchdog1.on( 'restart', editorWatchdog1RestartSpy );
  356. editorWatchdog2.on( 'restart', editorWatchdog2RestartSpy );
  357. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog1.editor ) );
  358. await waitCycle();
  359. sinon.assert.calledOnce( editorWatchdog1RestartSpy );
  360. sinon.assert.notCalled( mainWatchdogRestartSpy );
  361. sinon.assert.notCalled( editorWatchdog2RestartSpy );
  362. expect( editorWatchdog1.state ).to.equal( 'ready' );
  363. expect( editorWatchdog2.state ).to.equal( 'ready' );
  364. expect( mainWatchdog.state ).to.equal( 'ready' );
  365. expect( oldEditor1 ).to.not.equal( editorWatchdog1.editor );
  366. expect( oldEditor2 ).to.equal( editorWatchdog2.editor );
  367. expect( mainWatchdog.context ).to.equal( oldContext );
  368. await mainWatchdog.destroy();
  369. } );
  370. } );
  371. } );
  372. } );
  373. function throwCKEditorError( name, context ) {
  374. throw new CKEditorError( name, context );
  375. }
  376. function waitCycle() {
  377. return new Promise( res => setTimeout( res ) );
  378. }