seq-queue-test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. var should = require('should');
  2. var SeqQueue = require('../lib/seq-queue');
  3. var timeout = 1000;
  4. describe('seq-queue', function() {
  5. describe('#createQueue', function() {
  6. it('should return a seq-queue instance with init properties', function() {
  7. var queue = SeqQueue.createQueue(timeout);
  8. should.exist(queue);
  9. queue.should.have.property('timeout', timeout);
  10. queue.should.have.property('status', SeqQueue.IDLE);
  11. });
  12. });
  13. describe('#push' , function() {
  14. it('should change the queue status from idle to busy and invoke the task at once when task finish when queue idle', function(done) {
  15. var queue = SeqQueue.createQueue(timeout);
  16. queue.should.have.property('status', SeqQueue.IDLE);
  17. queue.push(function(task) {
  18. should.exist(task);
  19. task.done();
  20. queue.should.have.property('status', SeqQueue.IDLE);
  21. done();
  22. });
  23. queue.should.have.property('status', SeqQueue.BUSY);
  24. });
  25. it('should keep the status busy and keep the new task wait until the former tasks finish when queue busy', function(done) {
  26. var queue = SeqQueue.createQueue(timeout);
  27. var formerTaskFinished = false;
  28. //add first task
  29. queue.push(function(task) {
  30. formerTaskFinished = true;
  31. task.done();
  32. });
  33. queue.should.have.property('status', SeqQueue.BUSY);
  34. //add second task
  35. queue.push(function(task) {
  36. formerTaskFinished.should.be.true;
  37. queue.should.have.property('status', SeqQueue.BUSY);
  38. task.done();
  39. queue.should.have.property('status', SeqQueue.IDLE);
  40. done();
  41. });
  42. queue.should.have.property('status', SeqQueue.BUSY);
  43. });
  44. it('should ok if the task call done() directly', function(done) {
  45. var queue = SeqQueue.createQueue();
  46. var taskCount = 0;
  47. queue.push(function(task) {
  48. taskCount++;
  49. task.done();
  50. });
  51. queue.push(function(task) {
  52. taskCount++;
  53. task.done();
  54. });
  55. setTimeout(function() {
  56. taskCount.should.equal(2);
  57. done();
  58. }, 500);
  59. });
  60. });
  61. describe('#close', function() {
  62. it('should not accept new request but should execute the rest task in queue when close gracefully', function(done) {
  63. var queue = SeqQueue.createQueue(timeout);
  64. var closedEventCount = 0;
  65. var drainedEventCount = 0;
  66. queue.on('closed', function() {
  67. closedEventCount++;
  68. });
  69. queue.on('drained', function() {
  70. drainedEventCount++;
  71. });
  72. var executedTaskCount = 0;
  73. queue.push(function(task) {
  74. executedTaskCount++;
  75. task.done();
  76. }).should.be.true;
  77. queue.close(false);
  78. queue.should.have.property('status', SeqQueue.CLOSED);
  79. queue.push(function(task) {
  80. // never should be executed
  81. executedTaskCount++;
  82. task.done();
  83. }).should.be.false;
  84. // wait all task finished
  85. setTimeout(function() {
  86. executedTaskCount.should.equal(1);
  87. closedEventCount.should.equal(1);
  88. drainedEventCount.should.equal(1);
  89. done();
  90. }, 1000);
  91. });
  92. it('should not execute any task and emit a drained event when close forcefully', function(done) {
  93. var queue = SeqQueue.createQueue(timeout);
  94. var drainedEventCount = 0;
  95. queue.on('drained', function() {
  96. drainedEventCount++;
  97. });
  98. var executedTaskCount = 0;
  99. queue.push(function(task) {
  100. //never should be executed
  101. executedTaskCount++;
  102. task.done();
  103. }).should.be.true;
  104. queue.close(true);
  105. queue.should.have.property('status', SeqQueue.DRAINED);
  106. // wait all task finished
  107. setTimeout(function() {
  108. executedTaskCount.should.equal(0);
  109. drainedEventCount.should.equal(1);
  110. done();
  111. }, 1000);
  112. });
  113. });
  114. describe('#timeout', function() {
  115. it('should emit timeout event and execute the next task when a task timeout by default', function(done) {
  116. var queue = SeqQueue.createQueue();
  117. var executedTaskCount = 0;
  118. var timeoutCount = 0;
  119. var onTimeoutCount = 0;
  120. //add timeout listener
  121. queue.on('timeout', function(task) {
  122. task.should.be.a('object');
  123. task.fn.should.be.a('function');
  124. timeoutCount++;
  125. });
  126. queue.push(function(task) {
  127. executedTaskCount++;
  128. //no task.done() invoke to cause a timeout
  129. }, function() {
  130. onTimeoutCount++;
  131. }).should.be.true;
  132. queue.push(function(task) {
  133. executedTaskCount++;
  134. task.done();
  135. }).should.be.true;
  136. setTimeout(function() {
  137. //wait all task finish
  138. executedTaskCount.should.be.equal(2);
  139. timeoutCount.should.be.equal(1);
  140. onTimeoutCount.should.be.equal(1);
  141. done();
  142. }, 4000); //default timeout is 3s
  143. });
  144. it('should return false when invoke task.done() if task has already timeout', function(done) {
  145. var queue = SeqQueue.createQueue();
  146. var executedTaskCount = 0;
  147. var timeoutCount = 0;
  148. var timeout = 1000;
  149. //add timeout listener
  150. queue.on('timeout', function(task) {
  151. task.should.be.a('object');
  152. task.fn.should.be.a('function');
  153. timeoutCount++;
  154. });
  155. queue.push(function(task) {
  156. executedTaskCount++;
  157. task.done().should.be.true;
  158. }).should.be.true;
  159. queue.push(function(task) {
  160. //sleep to make a timeout
  161. setTimeout(function() {
  162. executedTaskCount++;
  163. task.done().should.be.false;
  164. }, timeout + 1000);
  165. }, null, timeout).should.be.true;
  166. setTimeout(function() {
  167. //wait all task finish
  168. executedTaskCount.should.be.equal(2);
  169. timeoutCount.should.be.equal(1);
  170. done();
  171. }, 4000);
  172. });
  173. it('should never timeout after close forcefully', function(done) {
  174. var queue = SeqQueue.createQueue(timeout);
  175. var timeoutCount = 0;
  176. //add timeout listener
  177. queue.on('timeout', function(task) {
  178. //should never enter here
  179. timeoutCount++;
  180. });
  181. queue.push(function(task) {
  182. //no task.done() invoke to cause a timeout
  183. }).should.be.true;
  184. queue.close(true);
  185. setTimeout(function() {
  186. //wait all task finish
  187. timeoutCount.should.be.equal(0);
  188. done();
  189. }, timeout * 2);
  190. });
  191. it('should use the global timeout value by default', function(done) {
  192. var globalTimeout = timeout + 100;
  193. var queue = SeqQueue.createQueue(globalTimeout);
  194. //add timeout listener
  195. queue.on('timeout', function(task) {
  196. (Date.now() - start).should.not.be.below(globalTimeout);
  197. done();
  198. });
  199. queue.push(function(task) {
  200. //no task.done() invoke to cause a timeout
  201. }).should.be.true;
  202. var start = Date.now();
  203. });
  204. it('should use the timeout value in #push if it was assigned', function(done) {
  205. var localTimeout = timeout / 2;
  206. var queue = SeqQueue.createQueue(timeout);
  207. //add timeout listener
  208. queue.on('timeout', function(task) {
  209. var diff = Date.now() - start;
  210. diff.should.not.be.below(localTimeout);
  211. diff.should.not.be.above(timeout);
  212. done();
  213. });
  214. queue.push(function(task) {
  215. //no task.done() invoke to cause a timeout
  216. }, null, localTimeout).should.be.true;
  217. var start = Date.now();
  218. });
  219. });
  220. describe('#error', function() {
  221. it('should emit an error event and invoke next task when a task throws an event', function(done) {
  222. var queue = SeqQueue.createQueue();
  223. var errorCount = 0;
  224. var taskCount = 0;
  225. //add timeout listener
  226. queue.on('error', function(err, task) {
  227. errorCount++;
  228. should.exist(err);
  229. should.exist(task);
  230. });
  231. queue.push(function(task) {
  232. taskCount++;
  233. throw new Error('some error');
  234. }).should.be.true;
  235. queue.push(function(task) {
  236. taskCount++;
  237. task.done();
  238. });
  239. setTimeout(function() {
  240. taskCount.should.equal(2);
  241. errorCount.should.equal(1);
  242. done();
  243. }, 500);
  244. });
  245. it('should be ok when task throw a error after done was invoked', function(done) {
  246. var queue = SeqQueue.createQueue();
  247. var errorCount = 0;
  248. var taskCount = 0;
  249. //add timeout listener
  250. queue.on('error', function(err, task) {
  251. errorCount++;
  252. should.exist(err);
  253. should.exist(task);
  254. });
  255. queue.push(function(task) {
  256. taskCount++;
  257. task.done();
  258. throw new Error('some error');
  259. }).should.be.true;
  260. queue.push(function(task) {
  261. taskCount++;
  262. task.done();
  263. });
  264. setTimeout(function() {
  265. taskCount.should.equal(2);
  266. errorCount.should.equal(1);
  267. done();
  268. }, 500);
  269. });
  270. });
  271. });