当在一个RSpec套件中调用多个with_api()测试时,Goliath会破坏em-synchrony / em-hiredis

时间:2012-07-14 03:37:15

标签: rspec redis eventmachine fibers goliath

我在使用RSpec测试Goliath API时遇到了奇怪的行为。我的一个测试看起来像这样:

require 'helper'

describe Scales::Dispatch do

  it "should return a 404 if resource was not found" do
    with_api(Scales::Server) do
      get_request(:path => '/') do |client|
        client.response_header.http_status.should == 404
      end
    end
  end

  it "should return a resource" do
    Scales::Storage::Sync.set "/existing", "some content"

    with_api(Scales::Server) do
      get_request(:path => '/existing') do |client|
        client.response_header.http_status.should == 200
        client.response.should == "some content"
      end
    end

    Scales::Storage::Sync.del "/existing"
  end

end

API基本上只是在em-synchrony/em-hiredis的帮助下在redis中查找关键字:

module Scales
  module Lookup
    class << self

      def request(env)
        response = Storage::Async.get(path(env))
        response.nil? ? render_not_found : render(response)
      end

      private

      def path(env)
        env["REQUEST_URI"]
      end

      def render_not_found
        [404, {}, ""]
      end

      def render(response)
        [200, {}, response]
      end

    end
  end
end

两个测试都单独运行,但不能一起运行。执行第一个后,整个系统停止大约10秒钟。然后调用第二个with_api但是从不执行get_request - 我认为它正在某种超时运行。

我在另一个非常类似的测试中发现了相同的行为,它正在推送和弹出这样的队列:

describe Scales::Queue::Async do

  [Scales::Queue::Async::Request, Scales::Queue::Async::Response].each do |queue|
    context queue.name.split("::").last do

      it "should place a few jobs" do
        async do
          queue.add "job 1"
          queue.add "job 2"
          queue.add "job 3"
        end
      end

      it "should take them out blocking" do
        async do
          queue.pop.should == "job 1"
          queue.pop.should == "job 2"
          queue.pop.should == "job 3"
        end
      end

    end
  end

end

第二个async do ..的内容也根本没有执行。没有巨型加载,一个非常相似的测试运行完美:

require 'eventmachine'
require 'em-synchrony'
require 'em-synchrony/em-hiredis'

module Helpers

  def async
    if EM.reactor_running?
      yield
    else
      out = nil
      EM.synchrony do
        out = yield
        EM.stop
      end
      out
    end
  end

end

RSpec.configure do |config|
  config.include Helpers
  config.treat_symbols_as_metadata_keys_with_true_values = true
end

describe "em-synchrony/em-hiredis" do

  it "should lpush a job" do
    async do
      redis = EM::Hiredis.connect
      redis.lpush("a_queue", "job1")
    end
  end

  it "should block pop a job" do
    async do
      redis = EM::Hiredis.connect
      redis.brpop("a_queue", 0).last.should == "job1"
    end
  end

end

上一个任务的async do ..是相同的RSpec助手。

我整天都在疯狂地搜索,但对我来说这没有任何意义。因为最后一次测试运行得很好,我猜它既不是em-synchrony也不是em-synchrony/em-hiredis

也许goliath没有停止,占用EM有点太长了?

感谢您的帮助,这让我疯了!

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。

我在每个请求之前检查了连接,如果它在那里我没有重新建立它。但是看起来每次停止事件机都会关闭连接,所以基本上每个新请求都会出现一个静默失败的连接超时。

谢谢你的时间!