Search code examples
ruby-on-railsrubyrspeceventmachinefaye

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?


Solution

  • 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.