taskManager.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var should = require('should');
  2. var taskManager = require('../../lib/common/manager/taskManager');
  3. // set timeout for test
  4. taskManager.timeout = 100;
  5. var WAIT_TIME = 200;
  6. describe("#taskManager",function(){
  7. it("should add task and execute it",function(done){
  8. var key = 'key-1';
  9. var fn = function(task) {
  10. taskCount++;
  11. task.done();
  12. };
  13. var onTimeout = function() {
  14. should.fail('should not timeout.');
  15. };
  16. var taskCount = 0;
  17. taskManager.addTask(key, fn, onTimeout);
  18. setTimeout(function() {
  19. taskCount.should.equal(1);
  20. done();
  21. }, WAIT_TIME);
  22. });
  23. it("should fire timeout callback if task timeout",function(done){
  24. var key = 'key-1';
  25. var fn = function(task) {
  26. taskCount++;
  27. };
  28. var onTimeout = function() {
  29. timeoutCount++;
  30. };
  31. var taskCount = 0;
  32. var timeoutCount = 0;
  33. taskManager.addTask(key, fn, onTimeout);
  34. setTimeout(function() {
  35. taskCount.should.equal(1);
  36. timeoutCount.should.equal(1);
  37. done();
  38. }, WAIT_TIME);
  39. });
  40. it("should not fire timeout after close the task",function(done){
  41. var key = 'key-1';
  42. var fn = function(task) {
  43. taskCount++;
  44. };
  45. var onTimeout = function() {
  46. timeoutCount++;
  47. };
  48. var taskCount = 0;
  49. var timeoutCount = 0;
  50. taskManager.addTask(key, fn, onTimeout);
  51. process.nextTick(function() {
  52. taskManager.closeQueue(key, true);
  53. setTimeout(function() {
  54. taskCount.should.equal(1);
  55. timeoutCount.should.equal(0);
  56. done();
  57. }, WAIT_TIME);
  58. });
  59. });
  60. it("should be ok to remove a queue not exist",function(){
  61. var key = 'key-n';
  62. taskManager.closeQueue(key, true);
  63. });
  64. });