我在 rSpec 测试中连接到 AMQP 时遇到问题。我有这样的代码:
Module Rabbit
Class Client
def start
EventMachine.run do
connection = AMQP.connect(Settings_object) #it holds host, username and password information
channel = AMQP::Channel.new(connection)
channel.queue("queue_name", :durable => true)
channel.default_exchange.publish("A test message", :routing_key => "queue_name")
end
end
end
Module Esper
Class Server
def start
EventMachine.run do
connection = AMQP.connect(Settings_object) #it holds host, username and password information
=begin
Some code to subscribe to queues
=end
end
end
end
我的问题是当我运行 rspec 时:
@client = Rabbit::Client.new
@server = Esper::Server.new
Thread.new do
@client.start
end
Thread.new do
@server.start
end
起初客户端可以连接到AMQP,而服务器没有,但是当我第二次运行它时,客户端无法连接到服务器。我无法克服这个问题。当我第二次运行它时,我看不出为什么客户端会停止连接?