Stub FakeTransport compeltely

pull/814/head
Rick Carlino 2018-04-26 17:16:25 -05:00
parent 4bd60324b7
commit 0b5848ef14
6 changed files with 60 additions and 23 deletions

View File

@ -67,7 +67,7 @@ class Device < ApplicationRecord
end
# Send a realtime message to a logged in user.
def tell(message, transport = Transport)
def tell(message)
log = Log.new({ device: self,
message: message,
created_at: Time.now,
@ -75,7 +75,7 @@ class Device < ApplicationRecord
meta: { type: "info" } })
json = LogSerializer.new(log).as_json.to_json
transport.current.amqp_send(json, self.id, "logs")
Transport.current.amqp_send(json, self.id, "logs")
log
end

View File

@ -16,15 +16,14 @@ class Transport
attr_accessor :amqp_adapter, :request_store
def initialize(opts = {})
@amqp_adapter = opts.fetch :amqp_adapter, Transport.default_amqp_adapter
@request_store = opts.fetch :request_store, RequestStore.store
end
def self.current
@current ||= self.new
end
def self.current=(value)
@current = value
end
def connection
@connection ||= @amqp_adapter.new(AMQP_URL, OPTS).start
end

View File

@ -8,7 +8,7 @@ begin
.subscribe(block: true) do |info, _, payl|
LogService.process(info, payl)
end
rescue => Bunny::TCPConnectionFailedForAllHosts
rescue Bunny::TCPConnectionFailedForAllHosts => e
puts "MQTT Broker is unreachable. Waiting 5 seconds..."
sleep 5
retry

View File

@ -26,11 +26,9 @@ describe LogService do
end
it "calls .subscribe() on Transport." do
fakee = FakeLogChan.new
allow(Transport).to receive(:log_channel) { fakee }
expect(fakee.subcribe_calls).to eq(0)
load "lib/log_service.rb"
expect(fakee.subcribe_calls).to eq(1)
arg1 = Transport.current.calls[:subscribe].last[0]
expect(arg1).to eq({block: true})
end
it "creates new messages in the DB when called" do

View File

@ -25,4 +25,15 @@ describe Device do
device.timezone = "America/Chicago"
expect([-5, -6, -7]).to include device.tz_offset_hrs # Remember DST!
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)
expect(log.message).to eq(hello)
expect(chan).to eq("logs")
end
end

View File

@ -21,28 +21,57 @@ require "rspec/rails"
require_relative "./stuff"
require_relative "./fake_sequence"
class FakeBunny
# 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,
:publish,
:queue,
:subscribe,
:subscribe,
:topic,
:set_current_request_id,
:connection,
:log_channel
]
def current_request_id
rand(0..100)
end
def initialize(*)
self.clear!
end
def start(*)
def clear!
@calls = {}
end
def start
self
end
def create_channel(*)
self
def calls
@calls
end
def topic(*)
self
end
def publish(*)
self
MOCKED_METHODS.map do |name|
# Eval is Evil, but this is pretty quick for testing.
eval """
def #{name}(*x)
key = #{name.inspect}
(@calls[key] ||= []).push(x)
@calls[key] = @calls[key].last(10)
self
end
"""
end
end
Transport.default_amqp_adapter = FakeBunny
Transport.current = FakeTransport.new
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }