1

RSpec を使用して Goliath API をテストしているときに、奇妙な動作が発生しました。私のテストの1つは次のようになります。

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

em-synchrony/em-hiredisAPI は基本的に、次のような助けを借りて、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 秒間停止します。2 番目の 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

second の内容async do ..もまったく実行されません。goliath をロードしないと、非常によく似たテストが完全に実行されます。

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ます。

ゴライアスは止まらず、EM を占有する時間が長すぎるのではないでしょうか?

助けてくれてありがとう、これは私を夢中にさせています!

4

1 に答える 1

0

さて、私は解決策を見つけました。

すべてのリクエストの直前に接続を確認しましたが、接続があった場合は再確立しませんでした。しかし、eventmachine を停止するたびに接続が閉じられるように見えるため、基本的に新しいリクエストごとに、サイレントに失敗する接続タイムアウトが発生しました。

御時間ありがとうございます!

于 2012-07-14T14:34:24.257 に答える