t56k
t56k

Reputation: 7001

Faye and Ruby: have Rspec read Faye log or messages

How can I get Rspec or even Ruby to read a Faye message? They come through in Faye's log alright, but I can't seem to connect to Faye through Rspec:

it 'gets Faye message' do
  EM.run do
    client = Faye::Client.new('http://localhost:9292/faye')
    sub = client.subscribe('/documents') do |message|
      puts message
    end

    sub.callback do |message|
      puts message
    end
  end
end

This just hangs. The messages come through in Faye's log. What am I doing wrong?

Upvotes: 0

Views: 305

Answers (2)

t56k
t56k

Reputation: 7001

So I've solved my own problem. I'll post my solution here in the event that it helps someone else.

it 'generates a document process and gets a push response from faye' do
  EM.run do
    client = Faye::Client.new('http://localhost:9292/faye')

    Thread.new { subject.perform(:generate, id) }

    client.subscribe "/documents/#{id}/notify" do |response|
      publication = client.publish("/documents/#{id}/notify", '0')
      publication.callback do
        if response != '0'
          expect(response).to eq(id.to_s)
          EM.stop
        else
          p "FAYE RESPONSE: #{response}" # diagnostic only
        end
      end

      publication.errback { |error| p "FAYE RESPONSE: #{error.inspect}" }
    end
  end
end

My end game was simply to get Rspec to get the Faye messages sent from the subject.perform... process. Mission accomplished. Not the neatest thing in the world, but who cares.

Upvotes: 0

jfornoff
jfornoff

Reputation: 1378

http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine.run (Read the NOTE block)

I'd say the EM.run call blocks (never returns and waits for connections) and that's why your test hangs. Not really seeing what your test is trying to do though, so I can't give you a pointer on how to improve this.

Upvotes: 1

Related Questions