interval.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env node
  2. var assert = require('assert');
  3. var qjob = require('../qjobs');
  4. // maximum number of jobs executed in parallels
  5. var maxConcurrency = 5;
  6. // delay between each group of maxConcurrency jobs done
  7. var interval = 1000;
  8. var q = new qjob({
  9. maxConcurrency:maxConcurrency,
  10. interval:interval
  11. });
  12. // number of total jobs
  13. var maxJobs = 20;
  14. // tests dedicated variables
  15. var testExecutedJobs = 0;
  16. var testNbSleep = 0;
  17. // warning, if you change maxConcurrency, maxJobs
  18. // or interval variable, you will have to review
  19. // the testMaxNbSleep value
  20. var testMaxNbSleep = 4;
  21. var myjob = function(args,next) {
  22. setTimeout(function() {
  23. testExecutedJobs++;
  24. next();
  25. },args[1]);
  26. }
  27. // Let's add 10 job and add them to the queue
  28. for (var i = 0; i<maxJobs; i++) {
  29. q.add(myjob,['test'+i,Math.random()*1000]);
  30. }
  31. q.on('end',function() {
  32. assert.equal(testExecutedJobs, maxJobs);
  33. assert.equal(testNbSleep, testMaxNbSleep);
  34. //console.log('Done');
  35. });
  36. q.on('jobStart',function(args) {
  37. //console.log(args[0]+' wait for '+args[1]+' ms');
  38. });
  39. q.on('sleep',function() {
  40. testNbSleep++;
  41. //console.log('zzZZzzzz for '+interval+'ms',testNbSleep);
  42. });
  43. q.on('continu',function() {
  44. //console.log('WAKE !');
  45. });
  46. q.run();