pull/814/head
Rick Carlino 2018-04-26 17:57:37 -05:00
parent 0b5848ef14
commit cbfc52e396
4 changed files with 42 additions and 46 deletions

View File

@ -25,7 +25,7 @@ class Transport
end
def connection
@connection ||= @amqp_adapter.new(AMQP_URL, OPTS).start
@connection ||= Transport.default_amqp_adapter.new(AMQP_URL, OPTS).start
end
def log_channel
@ -35,7 +35,7 @@ class Transport
.bind("amq.topic", routing_key: "bot.*.logs")
end
def topic
def amqp_topic
@topic ||= self
.connection
.create_channel
@ -49,10 +49,10 @@ class Transport
# We need to hoist the Rack X-Farmbot-Rpc-Id to a global state so that it can
# be used as a unique identifier for AMQP messages.
def current_request_id
request_store[:current_request_id] || "NONE"
RequestStore.store[:current_request_id] || "NONE"
end
def set_current_request_id(uuid)
request_store[:current_request_id] = uuid
RequestStore.store[:current_request_id] = uuid
end
end

View File

@ -26,9 +26,12 @@ describe LogService do
end
it "calls .subscribe() on Transport." do
Transport.current.clear!
load "lib/log_service.rb"
arg1 = Transport.current.calls[:subscribe].last[0]
expect(arg1).to eq({block: true})
arg1 = Transport.current.calls[:subscribe].last[0]
routing_key = Transport.current.calls[:bind].last[1][:routing_key]
expect(arg1).to eq({block: true})
expect(routing_key).to eq("bot.*.logs")
end
it "creates new messages in the DB when called" do

View File

@ -27,13 +27,13 @@ describe Device do
end
it "sends specific users toast messages" do
hello = "Hello!"
log = device.tell(hello)
json, id, chan = Transport.current.calls[:amqp_send].last
json = JSON.parse(json)
expect(id).to eq(device.id)
Transport.current.clear!
hello = "Hello!"
log = device.tell(hello)
json, info = Transport.current.calls[:publish].last
json = JSON.parse(json)
expect(info[:routing_key]).to eq("bot.device_#{device.id}.logs")
expect(log.message).to eq(hello)
expect(chan).to eq("logs")
expect(json["message"]).to eq(hello)
end
end

View File

@ -17,46 +17,22 @@ require "pry"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"
require_relative "./stuff"
require_relative "./fake_sequence"
# This is a stub for BunnyRB because we don't want the test suite to connect to
# AMQP for real.
class FakeTransport
MOCKED_METHODS = [
:amqp_send,
:bind,
:create_channel,
class FakeTransport < Transport
MOCKED_METHODS = [ # Theses are the "real" I/O inducing methods that must be
:bind, # Stubbed out.
:publish,
:queue,
:subscribe,
:subscribe,
:topic,
:set_current_request_id,
:create_channel,
:connection,
:log_channel
# :topic,
]
def current_request_id
rand(0..100)
end
def initialize(*)
self.clear!
end
def clear!
@calls = {}
end
def start
self
end
def calls
@calls
end
# When you call an AMQP I/O method, instead of doing real I/O, it will deposit
# the call into the @calls dictionary for observation.
attr_reader :calls
MOCKED_METHODS.map do |name|
# Eval is Evil, but this is pretty quick for testing.
@ -69,9 +45,26 @@ class FakeTransport
end
"""
end
def initialize(*)
self.clear!
end
def start
self
end
def clear!
@calls = {}
end
end
Transport.current = FakeTransport.new
Transport.default_amqp_adapter = FakeTransport
Transport.current = Transport.default_amqp_adapter.new
require "rspec/rails"
require_relative "./stuff"
require_relative "./fake_sequence"
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }