var expect = require('expect.js');
var Happner = require('..');

describe('g2 - local events', function () {

  var server;

  before('start server', function (done) {
    Happner.create({
      modules: {
        'component1': {
          instance: {
            start: function ($happn, callback) {
              setInterval(function () {


                $happn.localEmit('eventName', {some: 'data'});


              }, 100);
              callback();
            }
          }
        },
        'component2': {
          instance: {
            start: function ($happn, callback) {
              callback();
            },
            awaitLocalEvent: function ($happn, callback) {


              $happn.localEvent.component1.once('eventName', function (data) {
                callback(null, data);
              });


            }
          }
        }
      },
      components: {
        'component1': {
          startMethod: 'start'
        },
        'component2': {
          startMethod: 'start'
        }
      }
    })
      .then(function (_server) {
        server = _server;
      })
      .then(done).catch(done);
  });

  after('stop server', function (done) {
    if (!server) return done();
    server.stop({reconnect: false}, done);
  });

  it('can subscribe to and emit local events from within components', function (done) {

    server.exchange.component2.awaitLocalEvent()
      .then(function (result) {
        expect(result).to.eql({some: 'data'});
      })
      .then(done).catch(done);

  });

  it('can subscribe to local events from mesh', function (done) {

    server.localEvent.component1.once('eventName', function (data) {
      expect(data).to.eql({some: 'data'});
      done();
    });

  });

});

@@ -307,16 +307,32 @@
         
+ this.interval =
  setInte
@@ -471,32 +471,33 @@
 );%0A            %7D
+,
 %0A          %7D%0A   
@@ -495,115 +495,141 @@
     
-%7D%0A        %7D,%0A        'component2': %7B%0A          instance: %7B%0A            start: function ($happn, callback) %7B
+  stop: function ($happn, callback) %7B%0A              clearInterval(this.interval);%0A              callback();%0A            %7D%0A          %7D
 %0A   
@@ -637,40 +637,56 @@
     
+%7D,%0A
       
-callback();%0A            %7D,
+  'component2': %7B%0A          instance: %7B
 %0A   
@@ -991,32 +991,62 @@
 tMethod: 'start'
+,%0A          stopMethod: 'stop'
 %0A        %7D,%0A    
@@ -1069,39 +1069,8 @@
 : %7B%0A
-          startMethod: 'start'%0A
     

stop emitter