我只是在用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在em-synchrony/em-hiredis的帮助下,API基本上只是在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 ..的内容也根本不执行。在没有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。
也许goliath没有停止,占用EM的时间有点太长了?
谢谢你的帮助,这快把我逼疯了!
发布于 2012-07-14 22:34:24
好吧,我找到解决方案了。
我在每次请求之前都会检查连接,如果连接在那里,我就不会重新建立它。但是,每次停止eventmachine都会关闭连接,因此基本上每个新请求都会有一个连接超时,并以静默方式失败。
耽误您时间,实在对不起!
https://stackoverflow.com/questions/11480805
复制相似问题