const assert  = require('assert');
const Helpers = require('./helpers');
const workers = require('../src');


describe("processing", ()=> {

  const processMultiple   = workers.queue('process-multiple');
  const processPromise    = workers.queue('process-promise');
  const processGenerator  = workers.queue('process-generator');

  // Empty all queues.
  before((done)=> workers.reset(done));


  describe("with multiple handlers", ()=> {

    // Count how many steps run
    let steps = 0;
    before(()=> {
      processMultiple.each((job, callback)=> {
        steps++;
        callback();
      });
      processMultiple.each((job, callback)=> {
        steps++;
        callback();
      });
      processMultiple.each((job, callback)=> {
        steps++;
        callback();
      });
    });

    before((done)=> processMultiple.push('bandito', done));
    before((done)=> workers.once(done));

    it("should run all steps", ()=> {
      assert.equal(steps, 3);
    });

  });


  describe("with promises", ()=> {

    // Count how many steps run
    let steps = 0;
    before(()=> {
      processPromise.each((job)=> {
        let promise = new Promise((resolve, reject)=> {
          setTimeout(resolve, 10);
        });
        promise
          .then(()=> steps++)
          .then(()=> steps++)
          .then(()=> steps++);
        return promise;
      });
    });

    before((done)=> processPromise.push('bandito', done));
    before((done)=> workers.once(done));

    it("should run all steps", ()=> {
      assert.equal(steps, 3);
    });

  });


  describe("with generator", ()=> {

    // Count how many steps run
    let steps = 0;
    before(()=> {
      processGenerator.each(function*(job) {
        steps++;
        yield Promise.resolve();
        steps++;
        yield (done)=> done();
        steps++;
      });
    });

    before((done)=> processGenerator.push('bandito', done));
    before((done)=> workers.once(done));

    it("should run all steps", ()=> {
      assert.equal(steps, 3);
    });

  });

});


@@ -474,33 +474,34 @@
     let steps = 
-0
+%5B%5D
 ;%0A    before(()=
@@ -556,34 +556,42 @@
  %7B%0A        steps
-++
+.push('A')
 ;%0A        callba
@@ -658,34 +658,42 @@
  %7B%0A        steps
-++
+.push('B')
 ;%0A        callba
@@ -760,34 +760,42 @@
  %7B%0A        steps
-++
+.push('C')
 ;%0A        callba
@@ -975,35 +975,48 @@
 sert.equal(steps
-, 3
+.join(''), 'ABC'
 );%0A    %7D);%0A%0A  %7D)
@@ -1095,33 +1095,34 @@
     let steps = 
-0
+%5B%5D
 ;%0A    before(()=
@@ -1199,74 +1199,20 @@
 ise(
-(resolve, reject)=%3E %7B%0A          setTimeout(resolve, 10);%0A        %7D
+setImmediate
 );%0A 
@@ -1244,34 +1244,42 @@
 .then(()=%3E steps
-++
+.push('A')
 )%0A          .the
@@ -1290,18 +1290,26 @@
 =%3E steps
-++
+.push('B')
 )%0A      
@@ -1328,18 +1328,26 @@
 =%3E steps
-++
+.push('C')
 );%0A     
@@ -1539,35 +1539,48 @@
 sert.equal(steps
-, 3
+.join(''), 'ABC'
 );%0A    %7D);%0A%0A  %7D)
@@ -1672,9 +1672,10 @@
 s = 
-0
+%5B%5D
 ;%0A  
@@ -1743,32 +1743,25 @@
         
-steps++;%0A       
+var one =
  yield P
@@ -1775,16 +1775,19 @@
 resolve(
+'A'
 );%0A     
@@ -1798,19 +1798,37 @@
 teps
-++;%0A       
+.push(one);%0A        var two =
  yie
@@ -1844,16 +1844,25 @@
 =%3E done(
+null, 'B'
 );%0A     
@@ -1873,10 +1873,151 @@
 teps
-++
+.push(two);%0A        var three = yield* function*() %7B%0A          yield setImmediate;%0A          return 'C';%0A        %7D();%0A        steps.push(three)
 ;%0A  
@@ -2202,11 +2202,24 @@
 teps
-, 3
+.join(''), 'ABC'
 );%0A 

Test nested generators