gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 11:02:54 24 4
gpt4 key购买 nike

我在使用 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 .. 的内容也根本没有执行。没有 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 的时间有点太长了?

感谢您的帮助,这让我抓狂!

最佳答案

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

我在每次请求之前检查连接,如果它在那里我没有重新建立它。但似乎每次停止 eventmachine 都会关闭连接,所以基本上对于每个新请求都有一个连接超时,但无提示地失败了。

感谢您的宝贵时间!

关于rspec - 当在一个 RSpec 套件中调用多个 with_api() 测试时,Goliath 会破坏 em-synchrony/em-hiredis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com