Q:

Multiple Event Machine causing one to report undefined method `stop' for nil:NilClass

I have two classes in two separate modules (I know that is not a good use for now :/)
I have something like this:
module MQ
  class Client

    def self.start(opts = {})
      new(opts).start
    end

    def initialize(queue, message)
      @template_message = message
      @queue = queue
    end

    def start
       EventMachine.run do
          #some code to send message via AMQP
          Signal.trap("INT")  { connection.close { EventMachine.stop { exit } }}
          Signal.trap("TERM") { connection.close {EventMachine.stop { exit(0) } }}
          Signal.trap("INFO") { puts "Current active statements: #{statements.keys.inspect}" }
        end
    end

    def stop
      EventMachine.stop
    end
  end
end

And next I have defined Server class:
module Esper
  class Server

    def self.start(opts = {})
      new(opts).start
    end

    def initialize(options)
    end

    def start
       EventMachine.run do
          #some code here to receive messages
          Signal.trap("INT")  { connection.close { EventMachine.stop { exit } }}
          Signal.trap("TERM") { connection.close {EventMachine.stop { exit(0) } }}
          Signal.trap("INFO") { puts "Current active statements: #{statements.keys.inspect}" }
        end
    end

    def stop
      EventMachine.stop
    end
  end
end

Now I have in rspec (and here is the error reporting):
context "matched messages" do

    before :each do 
      @template_message = { }

      @server = Esper::Server.new
      @client = MQ::Client.new("queue_name", @template_message)

    end

    describe "transfer" do
      it "should receive statements" do
        Thread.new do
          @server.start
        end

        Thread.new do
          @client.start
        end

        puts "Sleep for 6 seconds"
        sleep(6.0)

        #some check here

        @server.stop
        @client.stop  # and here it reports when I am trying to access nil class in class client in method stop.
      end

    end

It is reporting in Client class in method stop when trying to call EventMahine.stop
It is saying:
undefined method `stop' for nil:NilClass

Can someone point me where am I wrong and if you have any suggestion how to fix it?

A:

I have had this problem because I was trying to incorporate esper and ampq.
The problem which is showing I overcome by using somewhat different approach. I still don't know why it was giving the nil pointer exception.
My solution was to use a deamon-kit gem for rails. I have created a deamon using this kit.
The deamon is a separate application inside my original application. That way I can test the ampq in it's separate folders and when I want to test the whole application, I can run the deamon from within original application, send data to the deamon and then check returned values in the original application.

