我只是在測試帶有RSpec的Goliath API時遇到了奇怪的行爲。我的一個測試是這樣的:當在一個RSpec套件中調用多個with_api()測試時,Goliath會中斷em-synchrony/em-hiredis
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基本上只查找在Redis的一個關鍵與em-synchrony/em-hiredis
幫助這樣的:
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
的東西。
也許巨人沒有停下來,佔據新興市場太久了?
感謝您的幫助,這讓我瘋狂!