polling_test.js 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var testHelpers = require('test/helpers');
  20. require('utils/polling');
  21. describe('App.Poll', function () {
  22. var poll;
  23. beforeEach(function () {
  24. poll = App.Poll.create();
  25. });
  26. describe('#isCompleted', function () {
  27. var cases = [
  28. {
  29. isError: true,
  30. isSuccess: false,
  31. isCompleted: true,
  32. title: 'error'
  33. },
  34. {
  35. isError: false,
  36. isSuccess: true,
  37. isCompleted: true,
  38. title: 'success'
  39. },
  40. {
  41. isError: false,
  42. isSuccess: false,
  43. isCompleted: false,
  44. title: 'incomplete'
  45. }
  46. ];
  47. cases.forEach(function (item) {
  48. it(item.title, function () {
  49. poll.setProperties({
  50. isError: item.isError,
  51. isSuccess: item.isSuccess
  52. });
  53. expect(poll.get('isCompleted')).to.equal(item.isCompleted);
  54. });
  55. });
  56. });
  57. describe('#showLink', function () {
  58. var cases = [
  59. {
  60. isPolling: true,
  61. isStarted: false,
  62. showLink: true,
  63. title: 'polling'
  64. },
  65. {
  66. isPolling: false,
  67. isStarted: true,
  68. showLink: true,
  69. title: 'started'
  70. },
  71. {
  72. isPolling: false,
  73. isStarted: false,
  74. showLink: false,
  75. title: 'not polling, not started'
  76. }
  77. ];
  78. cases.forEach(function (item) {
  79. it(item.title, function () {
  80. poll.setProperties({
  81. isPolling: item.isPolling,
  82. isStarted: item.isStarted
  83. });
  84. expect(poll.get('showLink')).to.equal(item.showLink);
  85. });
  86. });
  87. });
  88. describe('#start', function () {
  89. var cases = [
  90. {
  91. setRequestIdCallCount: 1,
  92. startPollingCallCount: 0,
  93. title: 'request id not defined'
  94. },
  95. {
  96. requestId: null,
  97. setRequestIdCallCount: 1,
  98. startPollingCallCount: 0,
  99. title: 'request id is null'
  100. },
  101. {
  102. requestId: 0,
  103. setRequestIdCallCount: 0,
  104. startPollingCallCount: 1,
  105. title: 'request id is zero'
  106. },
  107. {
  108. requestId: 1,
  109. setRequestIdCallCount: 0,
  110. startPollingCallCount: 1,
  111. title: 'request id is non-zero'
  112. }
  113. ];
  114. cases.forEach(function (item) {
  115. describe(item.title, function () {
  116. beforeEach(function () {
  117. sinon.stub(poll, 'setRequestId', Em.K);
  118. sinon.stub(poll, 'startPolling', Em.K);
  119. poll.set('requestId', item.requestId);
  120. poll.start();
  121. });
  122. afterEach(function () {
  123. poll.setRequestId.restore();
  124. poll.startPolling.restore();
  125. });
  126. it('set request id', function () {
  127. expect(poll.setRequestId.callCount).to.equal(item.setRequestIdCallCount);
  128. });
  129. it('start polling', function () {
  130. expect(poll.startPolling.callCount).to.equal(item.startPollingCallCount);
  131. });
  132. });
  133. });
  134. });
  135. describe('#setRequestId', function () {
  136. var ajaxObj;
  137. beforeEach(function () {
  138. $.ajax.restore();
  139. sinon.stub($, 'ajax', function (obj) {
  140. return obj;
  141. });
  142. });
  143. it('AJAX request parameters', function () {
  144. var ajaxProps = {
  145. url: '/default',
  146. data: {}
  147. };
  148. poll.setProperties(ajaxProps);
  149. poll.setRequestId();
  150. ajaxObj = $.ajax.firstCall.args[0];
  151. expect(Em.Object.create(ajaxObj).getProperties(['url', 'data'])).to.eql(ajaxProps);
  152. });
  153. describe('#success', function () {
  154. var cases = [
  155. {
  156. data: undefined,
  157. isSuccess: true,
  158. isError: false,
  159. requestId: 1,
  160. doPollingCallCount: 0,
  161. title: 'data not defined'
  162. },
  163. {
  164. data: 'null',
  165. isSuccess: true,
  166. isError: false,
  167. requestId: 1,
  168. doPollingCallCount: 0,
  169. title: 'null data'
  170. },
  171. {
  172. data: '',
  173. isSuccess: true,
  174. isError: false,
  175. requestId: 1,
  176. doPollingCallCount: 0,
  177. title: 'empty data'
  178. },
  179. {
  180. data: '{}',
  181. isSuccess: false,
  182. isError: true,
  183. requestId: undefined,
  184. doPollingCallCount: 1,
  185. title: 'empty object'
  186. },
  187. {
  188. data: '{"Requests":null}',
  189. isSuccess: false,
  190. isError: true,
  191. requestId: null,
  192. doPollingCallCount: 1,
  193. title: 'no requests info'
  194. },
  195. {
  196. data: '{"Requests":{}}',
  197. isSuccess: false,
  198. isError: true,
  199. requestId: undefined,
  200. doPollingCallCount: 1,
  201. title: 'empty requests info'
  202. },
  203. {
  204. data: '{"Requests":{"name":"r0"}}',
  205. isSuccess: false,
  206. isError: true,
  207. requestId: undefined,
  208. doPollingCallCount: 1,
  209. title: 'no request id'
  210. },
  211. {
  212. data: '{"Requests":{"id":0}}',
  213. isSuccess: false,
  214. isError: true,
  215. requestId: 0,
  216. doPollingCallCount: 1,
  217. title: 'request id available'
  218. }
  219. ];
  220. cases.forEach(function (item) {
  221. describe(item.title, function () {
  222. var ajaxObject;
  223. beforeEach(function () {
  224. sinon.stub(poll, 'doPolling', Em.K);
  225. poll.setProperties({
  226. isSuccess: false,
  227. isError: true,
  228. requestId: 1
  229. });
  230. poll.setRequestId();
  231. ajaxObject = $.ajax.firstCall.args[0];
  232. ajaxObject.success(item.data);
  233. });
  234. afterEach(function () {
  235. poll.doPolling.restore();
  236. });
  237. it('isSuccess', function () {
  238. expect(poll.get('isSuccess')).to.equal(item.isSuccess);
  239. });
  240. it('isError', function () {
  241. expect(poll.get('isError')).to.equal(item.isError);
  242. });
  243. it('requestId', function () {
  244. expect(poll.get('requestId')).to.equal(item.requestId);
  245. });
  246. it('doPolling call', function () {
  247. expect(poll.doPolling.callCount).to.equal(item.doPollingCallCount);
  248. });
  249. });
  250. });
  251. });
  252. describe('#error', function () {
  253. beforeEach(function () {
  254. poll.setProperties({
  255. isSuccess: true,
  256. isError: false
  257. });
  258. poll.setRequestId();
  259. ajaxObj = $.ajax.firstCall.args[0];
  260. ajaxObj.error();
  261. });
  262. it('isSuccess', function () {
  263. expect(poll.get('isSuccess')).to.be.false;
  264. });
  265. it('isError', function () {
  266. expect(poll.get('isError')).to.be.true;
  267. });
  268. });
  269. });
  270. describe('#updateTaskLog', function () {
  271. beforeEach(function () {
  272. sinon.stub(poll, 'pollTaskLog', Em.K);
  273. poll.set('currentTaskId', 0);
  274. poll.updateTaskLog(1);
  275. });
  276. afterEach(function () {
  277. poll.pollTaskLog.restore();
  278. });
  279. it('should change task id', function () {
  280. expect(poll.get('currentTaskId')).to.equal(1);
  281. });
  282. it('should poll task log', function () {
  283. expect(poll.pollTaskLog.calledOnce).to.be.true;
  284. });
  285. });
  286. describe('#doPolling', function () {
  287. var cases = [
  288. {
  289. requestId: undefined,
  290. startPollingCallCount: 0,
  291. title: 'request id is undefined'
  292. },
  293. {
  294. requestId: null,
  295. startPollingCallCount: 0,
  296. title: 'request id is null'
  297. },
  298. {
  299. requestId: 0,
  300. startPollingCallCount: 1,
  301. title: 'request id is 0'
  302. },
  303. {
  304. requestId: 1,
  305. startPollingCallCount: 1,
  306. title: 'request id is gra than 0'
  307. }
  308. ];
  309. beforeEach(function () {
  310. sinon.stub(poll, 'startPolling', Em.K);
  311. });
  312. afterEach(function () {
  313. poll.startPolling.restore();
  314. });
  315. cases.forEach(function (item) {
  316. it(item.title, function () {
  317. poll.set('requestId', item.requestId);
  318. poll.doPolling();
  319. expect(poll.startPolling.callCount).to.equal(item.startPollingCallCount);
  320. });
  321. });
  322. });
  323. describe('#pollTaskLog', function () {
  324. var cases = [
  325. {
  326. currentTaskId: undefined,
  327. ajaxCallArguments: undefined,
  328. title: 'current task id is undefined'
  329. },
  330. {
  331. currentTaskId: null,
  332. ajaxCallArguments: undefined,
  333. title: 'current task id is null'
  334. },
  335. {
  336. currentTaskId: 0,
  337. ajaxCallArguments: [{
  338. name: 'background_operations.get_by_task',
  339. data: {
  340. requestId: 0,
  341. taskId: 0
  342. },
  343. success: 'pollTaskLogSuccessCallback'
  344. }],
  345. title: 'current task id is 0'
  346. },
  347. {
  348. currentTaskId: 1,
  349. ajaxCallArguments: [{
  350. name: 'background_operations.get_by_task',
  351. data: {
  352. requestId: 0,
  353. taskId: 1
  354. },
  355. success: 'pollTaskLogSuccessCallback'
  356. }],
  357. title: 'current task id is more than 0'
  358. }
  359. ];
  360. cases.forEach(function (item) {
  361. it(item.title, function () {
  362. poll.setProperties({
  363. requestId: 0,
  364. currentTaskId: item.currentTaskId
  365. });
  366. poll.pollTaskLog();
  367. if (item.ajaxCallArguments) {
  368. item.ajaxCallArguments[0].sender = poll;
  369. }
  370. expect(testHelpers.findAjaxRequest('name', 'background_operations.get_by_task')).to.eql(item.ajaxCallArguments);
  371. });
  372. });
  373. });
  374. describe('#pollTaskLogSuccessCallback', function () {
  375. it('polled data', function () {
  376. poll.set('polledData', [
  377. {
  378. Tasks: {
  379. id: 0
  380. }
  381. },
  382. {
  383. Tasks: {
  384. id: 1
  385. }
  386. },
  387. {
  388. Tasks: {
  389. id: 2
  390. }
  391. }
  392. ]);
  393. poll.pollTaskLogSuccessCallback({
  394. Tasks: {
  395. id: 1,
  396. stdout: 'stdout',
  397. stderr: 'stderr'
  398. }
  399. });
  400. expect(poll.get('polledData').toArray()).to.eql([
  401. {
  402. Tasks: {
  403. id: 0
  404. }
  405. },
  406. {
  407. Tasks: {
  408. id: 1,
  409. stdout: 'stdout',
  410. stderr: 'stderr'
  411. }
  412. },
  413. {
  414. Tasks: {
  415. id: 2
  416. }
  417. }
  418. ]);
  419. });
  420. });
  421. describe('#startPolling', function () {
  422. var cases = [
  423. {
  424. requestId: undefined,
  425. ajaxCallArguments: undefined,
  426. result: false,
  427. pollTaskLogCallCount: 0,
  428. title: 'request id is undefined'
  429. },
  430. {
  431. requestId: null,
  432. ajaxCallArguments: undefined,
  433. result: false,
  434. pollTaskLogCallCount: 0,
  435. title: 'request id is null'
  436. },
  437. {
  438. requestId: 0,
  439. ajaxCallArguments: [{
  440. name: 'background_operations.get_by_request',
  441. data: {
  442. requestId: 0
  443. },
  444. success: 'reloadSuccessCallback',
  445. error: 'reloadErrorCallback'
  446. }],
  447. result: true,
  448. pollTaskLogCallCount: 1,
  449. title: 'request id is 0'
  450. },
  451. {
  452. requestId: 1,
  453. ajaxCallArguments: [{
  454. name: 'background_operations.get_by_request',
  455. data: {
  456. requestId: 1
  457. },
  458. success: 'reloadSuccessCallback',
  459. error: 'reloadErrorCallback'
  460. }],
  461. result: true,
  462. pollTaskLogCallCount: 1,
  463. title: 'request id is more than 0'
  464. }
  465. ];
  466. cases.forEach(function (item) {
  467. var result;
  468. describe(item.title, function () {
  469. beforeEach(function () {
  470. sinon.stub(poll, 'pollTaskLog', Em.K);
  471. poll.set('requestId', item.requestId);
  472. result = poll.startPolling();
  473. if (item.ajaxCallArguments) {
  474. item.ajaxCallArguments[0].sender = poll;
  475. item.ajaxCallArguments[0].data.callback = poll.startPolling;
  476. }
  477. });
  478. afterEach(function () {
  479. poll.pollTaskLog.restore();
  480. });
  481. it('AJAX request', function () {
  482. expect(testHelpers.findAjaxRequest('name', 'background_operations.get_by_request')).to.eql(item.ajaxCallArguments);
  483. });
  484. it('result', function () {
  485. expect(result).to.equal(item.result);
  486. });
  487. it('pollTaskLog', function () {
  488. expect(poll.pollTaskLog.callCount).to.equal(item.pollTaskLogCallCount);
  489. });
  490. });
  491. });
  492. });
  493. describe('#reloadErrorCallback', function () {
  494. var cases = [
  495. {
  496. status: undefined,
  497. isSuccess: false,
  498. isError: false,
  499. title: 'status is undefined'
  500. },
  501. {
  502. status: null,
  503. isSuccess: false,
  504. isError: false,
  505. title: 'status is null'
  506. },
  507. {
  508. status: 0,
  509. isSuccess: false,
  510. isError: false,
  511. title: 'status is 0'
  512. },
  513. {
  514. status: 200,
  515. isSuccess: true,
  516. isError: false,
  517. title: 'success'
  518. },
  519. {
  520. status: 404,
  521. isSuccess: false,
  522. isError: true,
  523. title: 'error'
  524. }
  525. ];
  526. cases.forEach(function (item) {
  527. it(item.title, function () {
  528. poll.setProperties({
  529. isSuccess: item.isSuccess,
  530. isError: false
  531. });
  532. poll.reloadErrorCallback({
  533. status: item.status
  534. }, null, null, null, {});
  535. expect(poll.get('isError')).to.equal(item.isError);
  536. });
  537. });
  538. });
  539. describe('#replacePolledData', function () {
  540. var cases = [
  541. {
  542. currentTaskId: undefined,
  543. data: [],
  544. result: [],
  545. title: 'current task id is undefined'
  546. },
  547. {
  548. currentTaskId: null,
  549. data: [],
  550. result: [],
  551. title: 'current task id is null'
  552. },
  553. {
  554. currentTaskId: 0,
  555. polledData: [
  556. {
  557. Tasks: {
  558. id: 0
  559. }
  560. },
  561. {
  562. Tasks: {
  563. id: 1
  564. }
  565. }
  566. ],
  567. data: [
  568. {
  569. Tasks: {
  570. id: 1
  571. }
  572. }
  573. ],
  574. result: [
  575. {
  576. Tasks: {
  577. id: 1
  578. }
  579. }
  580. ],
  581. title: 'current task id is 0, no corresponding task passed'
  582. },
  583. {
  584. currentTaskId: 1,
  585. polledData: [
  586. {
  587. Tasks: {
  588. id: 0
  589. }
  590. }
  591. ],
  592. data: [
  593. {
  594. Tasks: {
  595. id: 0
  596. }
  597. },
  598. {
  599. Tasks: {
  600. id: 1
  601. }
  602. }
  603. ],
  604. result: [
  605. {
  606. Tasks: {
  607. id: 0
  608. }
  609. },
  610. {
  611. Tasks: {
  612. id: 1
  613. }
  614. }
  615. ],
  616. title: 'current task id is more than 0, no corresponding task set'
  617. },
  618. {
  619. currentTaskId: 2,
  620. polledData: [
  621. {
  622. Tasks: {
  623. id: 0
  624. }
  625. },
  626. {
  627. Tasks: {
  628. id: 2,
  629. stdout: 'stdout',
  630. stderr: 'stderr'
  631. }
  632. }
  633. ],
  634. data: [
  635. {
  636. Tasks: {
  637. id: 0
  638. }
  639. },
  640. {
  641. Tasks: {
  642. id: 2
  643. }
  644. },
  645. {
  646. Tasks: {
  647. id: 3
  648. }
  649. }
  650. ],
  651. result: [
  652. {
  653. Tasks: {
  654. id: 0
  655. }
  656. },
  657. {
  658. Tasks: {
  659. id: 2,
  660. stdout: 'stdout',
  661. stderr: 'stderr'
  662. }
  663. },
  664. {
  665. Tasks: {
  666. id: 3
  667. }
  668. }
  669. ],
  670. title: 'current task id is more than 0, corresponding task set and passed'
  671. },
  672. {
  673. currentTaskId: 3,
  674. polledData: [
  675. {
  676. Tasks: {
  677. id: 0
  678. }
  679. }
  680. ],
  681. data: [
  682. {
  683. Tasks: {
  684. id: 1
  685. }
  686. }
  687. ],
  688. result: [
  689. {
  690. Tasks: {
  691. id: 1
  692. }
  693. }
  694. ],
  695. title: 'current task id is more than 0, corresponding task neither set nor passed'
  696. }
  697. ];
  698. cases.forEach(function (item) {
  699. it(item.title, function () {
  700. poll.setProperties({
  701. currentTaskId: item.currentTaskId,
  702. polledData: item.polledData || null
  703. });
  704. poll.replacePolledData(item.data);
  705. expect(poll.get('polledData').toArray()).to.eql(item.result);
  706. });
  707. });
  708. });
  709. describe('#calculateProgressByTasks', function () {
  710. var cases = [
  711. {
  712. tasksData: [
  713. {
  714. Tasks: {
  715. status: 'QUEUED'
  716. }
  717. },
  718. {
  719. Tasks: {
  720. status: 'QUEUED'
  721. }
  722. }
  723. ],
  724. result: 9,
  725. title: 'all tasks pending'
  726. },
  727. {
  728. tasksData: [
  729. {
  730. Tasks: {
  731. status: 'IN_PROGRESS'
  732. }
  733. },
  734. {
  735. Tasks: {
  736. status: 'IN_PROGRESS'
  737. }
  738. }
  739. ],
  740. result: 35,
  741. title: 'all tasks in progress'
  742. },
  743. {
  744. tasksData: [
  745. {
  746. Tasks: {
  747. status: 'COMPLETED'
  748. }
  749. },
  750. {
  751. Tasks: {
  752. status: 'COMPLETED'
  753. }
  754. }
  755. ],
  756. result: 100,
  757. title: 'all tasks completed'
  758. },
  759. {
  760. tasksData: [
  761. {
  762. Tasks: {
  763. status: 'FAILED'
  764. }
  765. },
  766. {
  767. Tasks: {
  768. status: 'FAILED'
  769. }
  770. }
  771. ],
  772. result: 100,
  773. title: 'all tasks failed'
  774. },
  775. {
  776. tasksData: [
  777. {
  778. Tasks: {
  779. status: 'ABORTED'
  780. }
  781. },
  782. {
  783. Tasks: {
  784. status: 'ABORTED'
  785. }
  786. }
  787. ],
  788. result: 100,
  789. title: 'all tasks aborted'
  790. },
  791. {
  792. tasksData: [
  793. {
  794. Tasks: {
  795. status: 'TIMEDOUT'
  796. }
  797. },
  798. {
  799. Tasks: {
  800. status: 'TIMEDOUT'
  801. }
  802. }
  803. ],
  804. result: 100,
  805. title: 'all tasks timed out'
  806. },
  807. {
  808. tasksData: [
  809. {
  810. Tasks: {
  811. status: 'QUEUED'
  812. }
  813. },
  814. {
  815. Tasks: {
  816. status: 'COMPLETED'
  817. }
  818. }
  819. ],
  820. result: 55,
  821. title: 'pending and finished tasks'
  822. },
  823. {
  824. tasksData: [
  825. {
  826. Tasks: {
  827. status: 'IN_PROGRESS'
  828. }
  829. },
  830. {
  831. Tasks: {
  832. status: 'FAILED'
  833. }
  834. }
  835. ],
  836. result: 68,
  837. title: 'running and finished tasks'
  838. },
  839. {
  840. tasksData: [
  841. {
  842. Tasks: {
  843. status: 'IN_PROGRESS'
  844. }
  845. },
  846. {
  847. Tasks: {
  848. status: 'QUEUED'
  849. }
  850. }
  851. ],
  852. result: 22,
  853. title: 'running and pending tasks'
  854. },
  855. {
  856. tasksData: [
  857. {
  858. Tasks: {
  859. status: 'IN_PROGRESS'
  860. }
  861. },
  862. {
  863. Tasks: {
  864. status: 'QUEUED'
  865. }
  866. },
  867. {
  868. Tasks: {
  869. status: 'ABORTED'
  870. }
  871. }
  872. ],
  873. result: 48,
  874. title: 'running, pending and finished tasks'
  875. }
  876. ];
  877. cases.forEach(function (item) {
  878. it(item.title, function () {
  879. expect(poll.calculateProgressByTasks(item.tasksData)).to.equal(item.result);
  880. });
  881. });
  882. });
  883. describe('#isPollingFinished', function () {
  884. var cases = [
  885. {
  886. polledData: [
  887. {
  888. Tasks: {
  889. status: 'QUEUED'
  890. }
  891. },
  892. {
  893. Tasks: {
  894. status: 'COMPLETED'
  895. }
  896. }
  897. ],
  898. isPollingFinished: false,
  899. isSuccess: false,
  900. isError: false,
  901. title: 'some queued tasks'
  902. },
  903. {
  904. polledData: [
  905. {
  906. Tasks: {
  907. status: 'IN_PROGRESS'
  908. }
  909. },
  910. {
  911. Tasks: {
  912. status: 'COMPLETED'
  913. }
  914. }
  915. ],
  916. isPollingFinished: false,
  917. isSuccess: false,
  918. isError: false,
  919. title: 'some running tasks'
  920. },
  921. {
  922. polledData: [
  923. {
  924. Tasks: {
  925. status: 'PENDING'
  926. }
  927. },
  928. {
  929. Tasks: {
  930. status: 'COMPLETED'
  931. }
  932. }
  933. ],
  934. isPollingFinished: false,
  935. isSuccess: false,
  936. isError: false,
  937. title: 'some pending tasks'
  938. },
  939. {
  940. polledData: [
  941. {
  942. Tasks: {
  943. status: 'FAILED'
  944. }
  945. },
  946. {
  947. Tasks: {
  948. status: 'COMPLETED'
  949. }
  950. }
  951. ],
  952. isPollingFinished: true,
  953. isSuccess: false,
  954. isError: true,
  955. title: 'all tasks finished, some failed'
  956. },
  957. {
  958. polledData: [
  959. {
  960. Tasks: {
  961. status: 'ABORTED'
  962. }
  963. },
  964. {
  965. Tasks: {
  966. status: 'COMPLETED'
  967. }
  968. }
  969. ],
  970. isPollingFinished: true,
  971. isSuccess: false,
  972. isError: true,
  973. title: 'all tasks finished, some aborted'
  974. },
  975. {
  976. polledData: [
  977. {
  978. Tasks: {
  979. status: 'ABORTED'
  980. }
  981. },
  982. {
  983. Tasks: {
  984. status: 'COMPLETED'
  985. }
  986. }
  987. ],
  988. isPollingFinished: true,
  989. isSuccess: false,
  990. isError: true,
  991. title: 'all tasks finished, some timed out'
  992. },
  993. {
  994. polledData: [
  995. {
  996. Tasks: {
  997. status: 'COMPLETED'
  998. }
  999. },
  1000. {
  1001. Tasks: {
  1002. status: 'COMPLETED'
  1003. }
  1004. }
  1005. ],
  1006. isPollingFinished: true,
  1007. isSuccess: true,
  1008. isError: false,
  1009. title: 'all tasks finished successfully'
  1010. }
  1011. ];
  1012. cases.forEach(function (item) {
  1013. describe(item.title, function () {
  1014. var result;
  1015. beforeEach(function () {
  1016. poll.setProperties({
  1017. isSuccess: false,
  1018. isError: false
  1019. });
  1020. result = poll.isPollingFinished(item.polledData);
  1021. });
  1022. it('isPollingFinished', function () {
  1023. expect(result).to.equal(item.isPollingFinished);
  1024. });
  1025. it('isSuccess', function () {
  1026. expect(poll.get('isSuccess')).to.equal(item.isSuccess);
  1027. });
  1028. it('isError', function () {
  1029. expect(poll.get('isError')).to.equal(item.isError);
  1030. });
  1031. });
  1032. });
  1033. });
  1034. describe('#parseInfo', function () {
  1035. var cases = [
  1036. {
  1037. polledData: {
  1038. Requests: {
  1039. id: 1
  1040. }
  1041. },
  1042. replacePolledDataCallCount: 0,
  1043. progress: '0',
  1044. result: false,
  1045. title: 'no corresponding request data'
  1046. },
  1047. {
  1048. polledData: {
  1049. tasks: []
  1050. },
  1051. replacePolledDataCallCount: 1,
  1052. progress: '100',
  1053. result: false,
  1054. title: 'no request id info'
  1055. },
  1056. {
  1057. polledData: {
  1058. Requests: {
  1059. id: 0
  1060. },
  1061. tasks: []
  1062. },
  1063. replacePolledDataCallCount: 1,
  1064. progress: '100',
  1065. result: false,
  1066. title: 'no tasks'
  1067. },
  1068. {
  1069. polledData: {
  1070. Requests: {
  1071. id: 0
  1072. },
  1073. tasks: [
  1074. {
  1075. Tasks: {
  1076. status: 'PENDING'
  1077. }
  1078. },
  1079. {
  1080. Tasks: {
  1081. status: 'COMPLETED'
  1082. }
  1083. }
  1084. ]
  1085. },
  1086. replacePolledDataCallCount: 1,
  1087. progress: '100',
  1088. result: false,
  1089. title: 'not all tasks finished'
  1090. },
  1091. {
  1092. polledData: {
  1093. Requests: {
  1094. id: 0
  1095. },
  1096. tasks: [
  1097. {
  1098. Tasks: {
  1099. status: 'FAILED'
  1100. }
  1101. },
  1102. {
  1103. Tasks: {
  1104. status: 'COMPLETED'
  1105. }
  1106. }
  1107. ]
  1108. },
  1109. replacePolledDataCallCount: 1,
  1110. progress: '100',
  1111. result: true,
  1112. title: 'all tasks finished'
  1113. }
  1114. ];
  1115. cases.forEach(function (item) {
  1116. describe(item.title, function () {
  1117. var result;
  1118. beforeEach(function () {
  1119. sinon.stub(poll, 'replacePolledData', Em.K);
  1120. sinon.stub(poll, 'calculateProgressByTasks').returns(100);
  1121. sinon.stub(poll, 'isPollingFinished', function (data) {
  1122. return data.length > 0 && !(data.someProperty('Tasks.status', 'QUEUED') || data.someProperty('Tasks.status', 'IN_PROGRESS')
  1123. || data.someProperty('Tasks.status', 'PENDING'));
  1124. });
  1125. poll.setProperties({
  1126. requestId: 0,
  1127. progress: '0'
  1128. });
  1129. result = poll.parseInfo(item.polledData);
  1130. });
  1131. afterEach(function () {
  1132. poll.replacePolledData.restore();
  1133. poll.calculateProgressByTasks.restore();
  1134. poll.isPollingFinished.restore();
  1135. });
  1136. it('replacePolledData call', function () {
  1137. expect(poll.replacePolledData.callCount).to.equal(item.replacePolledDataCallCount);
  1138. });
  1139. if (item.replacePolledDataCallCount) {
  1140. it('replacePolledData argument', function () {
  1141. expect(poll.replacePolledData.firstCall.args).to.eql([item.polledData.tasks]);
  1142. });
  1143. }
  1144. it('progress', function () {
  1145. expect(poll.get('progress')).to.equal(item.progress);
  1146. });
  1147. it('result', function () {
  1148. expect(result).to.equal(item.result);
  1149. });
  1150. });
  1151. });
  1152. });
  1153. });