Skip to content

Commit

Permalink
Lint and fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
baelter committed Nov 27, 2019
1 parent 5f279f1 commit 94a6652
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 45 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ source "https://rubygems.org"
gemspec

group :test do
gem "codeclimate-test-reporter", "~> 1.0.0"
gem "simplecov", require: false
end
25 changes: 11 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ PATH
GEM
remote: https://rubygems.org/
specs:
amq-protocol (2.1.0)
amq-protocol (2.3.0)
ansi (1.5.0)
builder (3.2.3)
bunny (2.6.3)
amq-protocol (>= 2.0.1)
bunny-mock (1.5.0)
bunny (2.14.3)
amq-protocol (~> 2.3, >= 2.3.0)
bunny-mock (1.7.0)
bunny (>= 1.7)
codeclimate-test-reporter (1.0.5)
simplecov
docile (1.1.5)
json (2.0.3)
minitest (5.10.1)
minitest-reporters (1.1.14)
json (2.2.0)
minitest (5.13.0)
minitest-reporters (1.4.2)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
rake (12.0.0)
ruby-progressbar (1.8.1)
rake (13.0.1)
ruby-progressbar (1.10.1)
simplecov (0.13.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
simplecov-html (0.10.2)

PLATFORMS
ruby
Expand All @@ -39,11 +37,10 @@ DEPENDENCIES
amqp_actors!
bundler
bunny-mock
codeclimate-test-reporter (~> 1.0.0)
minitest
minitest-reporters
rake
simplecov

BUNDLED WITH
1.13.7
1.17.3
7 changes: 3 additions & 4 deletions amqp_actors.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'amqp_actors/version'

Expand All @@ -24,13 +23,13 @@ Gem::Specification.new do |spec|
.reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "bin"
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = %w(lib)
spec.require_paths = %w[lib]

spec.add_runtime_dependency "bunny"

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "bunny-mock"
spec.add_development_dependency "minitest"
spec.add_development_dependency "minitest-reporters"
spec.add_development_dependency "rake"
end
6 changes: 5 additions & 1 deletion lib/amqp_actors/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def inherited(subclass)

def act(&block)
raise ArgumentError, 'act must recieve a block' unless block_given?

@act_block = block
end

Expand All @@ -29,11 +30,13 @@ def push(msg)
raise ArgumentError, "Illegal message type, expected #{@message_type}"
end
raise NotConfigured, 'you must provide an act block' unless @act_block

@backend_instance&.push(msg) unless @backend_instance&.closed? && @running
end

def backend(clazz, &blk)
raise ArgumentError, "Backend must implement :start and :stop" unless valid_backend? clazz

@backend = clazz
@backend_block = blk
@backend_instance&.stop
Expand Down Expand Up @@ -83,6 +86,7 @@ def helpers(&block)

def valid_types?(value)
return true if @message_type.nil?

if @message_type.is_a?(Hash) && value.respond_to?(:all?)
key_value = @message_type.flatten
key_value.size == 2 && value.is_a?(key_value.first) &&
Expand All @@ -96,7 +100,7 @@ def valid_types?(value)
end

def valid_backend?(clazz)
require_methods = %i(start stop)
require_methods = %i[start stop]
require_methods & clazz.instance_methods == require_methods
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/amqp_actors/backend/amqp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def configure(cfg)

def push_to(rks, msg, cfg = {})
raise NotConfigured, "Can't .push_to without configured amqp_[pub_]url" unless @@pub_url

unless @publisher
conn = @@connections[@@pub_url] ||= @@client.new(@@pub_url)
conn.start
Expand All @@ -30,14 +31,15 @@ def initialize(type, &blk)
@type = type
@pub_url = @@pub_url
@sub_url = @@sub_url
@client = @@client || Bunny
@client = @@client ||  Bunny
@content_type = @@content_type

instance_eval(&blk) if block_given?

if @pub_url.nil? || @sub_url.nil?
raise NotConfigured, 'use AmqpQueues.configure or AmqpActor#backend to provide a amqp url'
end

@pub_conn = @@connections[@pub_url] ||= @client.new(@pub_url)
@sub_conn = @@connections[@sub_url] ||= @client.new(@sub_url)
end
Expand Down Expand Up @@ -222,7 +224,7 @@ def subscribe(qname = nil)
@type.new.push(parse(body, headers))
end
@chan.acknowledge(delivery.delivery_tag, false)
rescue => e
rescue StandardError => e
print "[ERROR] #{e.inspect} #{e.message}\n #{e.backtrace.join("\n ")}\n"
sleep 1
@chan.reject(delivery.delivery_tag, true)
Expand Down Expand Up @@ -295,6 +297,7 @@ def self.register(content_type)
def self.content_handler(content_type_sym)
content_type = @@content_types.find { |ct| ct.name.end_with? content_type_sym.to_s }
raise NotConfigured, "Unknown content_type=#{content_type}. register?" unless content_type

content_type
end

Expand All @@ -312,7 +315,7 @@ class ContentType
class << self
def can_handle?(data)
decode(encode(data)) && true
rescue
rescue StandardError
false
end

Expand Down
1 change: 0 additions & 1 deletion lib/amqp_actors/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def self.start(cfg = {})
def self.stop
@running = false
@actors.each(&:die)
@default_backend.stop if @default_backend
@default_backend = nil
@actors = Set.new
end
Expand Down
24 changes: 12 additions & 12 deletions spec/actor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MessageActor < AmqpActors::TestActor

expected = 'test\nsad'
MessageActor.push(expected)
MessageActor.output.must_equal(expected)
expect(MessageActor.output).must_equal(expected)
end

it 'should die' do
Expand All @@ -32,7 +32,7 @@ class DieActor < AmqpActors::TestActor
DieActor.push(1)
DieActor.output

DieActor.backend_instance.running_threads.must_equal(0)
expect(DieActor.backend_instance.running_threads).must_equal(0)
end

it 'should raise for wrong message type' do
Expand All @@ -41,19 +41,19 @@ class TypedTestActor < AmqpActors::TestActor
act {}
end

proc { TypedTestActor.push(1) }.must_raise(ArgumentError)
expect(proc { TypedTestActor.push(1) }).must_raise(ArgumentError)
end

it 'A backend must implement :start and :stop' do
class TestBackend; end

proc do
expect(proc do
class CustomBackendActor < AmqpActors::TestActor
backend TestBackend

act {}
end
end.must_raise(ArgumentError)
end).must_raise(ArgumentError)
end

it 'should get and set thread_count' do
Expand All @@ -65,7 +65,7 @@ class ThreadCountActor < AmqpActors::TestActor
end

ThreadCountActor.push('whatever')
ThreadCountActor.output.must_equal(2)
expect(ThreadCountActor.output).must_equal(2)
end

it 'should be able to use helpers' do
Expand All @@ -81,7 +81,7 @@ def sum(a, b)
end

HelpersActor.push(1)
HelpersActor.output.must_equal(2)
expect(HelpersActor.output).must_equal(2)
end

it 'should handle collections message type' do
Expand All @@ -93,8 +93,8 @@ class CollectionsActor < AmqpActors::TestActor
end

CollectionsActor.push(["1"])
CollectionsActor.output.must_equal(["1"])
proc { CollectionsActor.push("hej") }.must_raise(ArgumentError)
expect(CollectionsActor.output).must_equal(["1"])
expect(proc { CollectionsActor.push("hej") }).must_raise(ArgumentError)
end

it 'should return inbox size' do
Expand All @@ -104,10 +104,10 @@ class ImboxSizeActor < AmqpActors::TestActor
end
end

ImboxSizeActor.inbox_size.must_equal 0
expect(ImboxSizeActor.inbox_size).must_equal 0
ImboxSizeActor.push(1)
ImboxSizeActor.inbox_size.must_equal 1
expect(ImboxSizeActor.inbox_size).must_equal 1
ImboxSizeActor.output
ImboxSizeActor.inbox_size.must_equal 0
expect(ImboxSizeActor.inbox_size).must_equal 0
end
end
10 changes: 5 additions & 5 deletions spec/amqp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AmqpActor < AmqpActors::TestActor

expected = 'test'
AmqpActor.push(expected)
AmqpActor.output.must_equal(expected)
expect(AmqpActor.output).must_equal(expected)
end

describe :content_handler do
Expand All @@ -45,7 +45,7 @@ class SerializeActor < AmqpActors::TestActor

expected = OpenStruct.new(a: 'a')
SerializeActor.push(expected)
SerializeActor.output.must_equal(expected)
expect(SerializeActor.output).must_equal(expected)
end

it 'should handle :json' do
Expand All @@ -61,7 +61,7 @@ class JsonActor < AmqpActors::TestActor

expected = { a: 'a' }
JsonActor.push(expected)
JsonActor.output.must_equal(expected)
expect(JsonActor.output).must_equal(expected)
end

it 'should handle :plain' do
Expand All @@ -77,15 +77,15 @@ class PlainActor < AmqpActors::TestActor

expected = 'expected'
PlainActor.push(expected)
PlainActor.output.must_equal(expected)
expect(PlainActor.output).must_equal(expected)
end
end

it 'should raise if not configured' do
class NotConfiguredActor < AmqpActors::Actor
backend AmqpActors::AmqpQueues
end
proc { NotConfiguredActor.push(1) }.must_raise(AmqpActors::NotConfigured)
expect(proc { NotConfiguredActor.push(1) }).must_raise(AmqpActors::NotConfigured)
end

it 'should pust_to rks' do
Expand Down
10 changes: 6 additions & 4 deletions spec/system_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require_relative 'spec_helper'

describe AmqpActors::System do
it 'should start/stop MemoryQueues' do
class StartMemoryActor < AmqpActors::TestActor; end
AmqpActors::System.start
AmqpActors::System.running?.must_equal(true)
expect(AmqpActors::System.running?).must_equal(true)
AmqpActors::System.stop
AmqpActors::System.running?.must_equal(false)
expect(AmqpActors::System.running?).must_equal(false)
end

it 'should start/stop AmqpQueues' do
AmqpActors::System.start(
default_backend: AmqpActors::AmqpQueues.configure(client: BunnyMock, amqp_url: '')
)
class StartAmqpActor < AmqpActors::Actor; end
AmqpActors::System.running?.must_equal(true)
expect(AmqpActors::System.running?).must_equal(true)
AmqpActors::System.stop
AmqpActors::System.running?.must_equal(false)
expect(AmqpActors::System.running?).must_equal(false)
end
end

0 comments on commit 94a6652

Please sign in to comment.