From 94a6652f4b48d712e75bf6e4e8eb28281dcc9e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20B=C3=A4lter?= Date: Wed, 27 Nov 2019 13:56:47 +0100 Subject: [PATCH] Lint and fix specs --- Gemfile | 1 - Gemfile.lock | 25 +++++++++++-------------- amqp_actors.gemspec | 7 +++---- lib/amqp_actors/actor.rb | 6 +++++- lib/amqp_actors/backend/amqp.rb | 9 ++++++--- lib/amqp_actors/system.rb | 1 - spec/actor_spec.rb | 24 ++++++++++++------------ spec/amqp_spec.rb | 10 +++++----- spec/system_spec.rb | 10 ++++++---- 9 files changed, 48 insertions(+), 45 deletions(-) diff --git a/Gemfile b/Gemfile index d1fe401..c4675c3 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,5 @@ source "https://rubygems.org" gemspec group :test do - gem "codeclimate-test-reporter", "~> 1.0.0" gem "simplecov", require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 43c2273..d71382f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -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 diff --git a/amqp_actors.gemspec b/amqp_actors.gemspec index d4fab72..14fa534 100644 --- a/amqp_actors.gemspec +++ b/amqp_actors.gemspec @@ -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' @@ -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 diff --git a/lib/amqp_actors/actor.rb b/lib/amqp_actors/actor.rb index a80e430..d1f22ec 100644 --- a/lib/amqp_actors/actor.rb +++ b/lib/amqp_actors/actor.rb @@ -21,6 +21,7 @@ def inherited(subclass) def act(&block) raise ArgumentError, 'act must recieve a block' unless block_given? + @act_block = block end @@ -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 @@ -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) && @@ -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 diff --git a/lib/amqp_actors/backend/amqp.rb b/lib/amqp_actors/backend/amqp.rb index 98f6868..08d09f7 100644 --- a/lib/amqp_actors/backend/amqp.rb +++ b/lib/amqp_actors/backend/amqp.rb @@ -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 @@ -30,7 +31,7 @@ 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? @@ -38,6 +39,7 @@ def initialize(type, &blk) 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 @@ -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) @@ -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 @@ -312,7 +315,7 @@ class ContentType class << self def can_handle?(data) decode(encode(data)) && true - rescue + rescue StandardError false end diff --git a/lib/amqp_actors/system.rb b/lib/amqp_actors/system.rb index d9956b7..e8dcb02 100644 --- a/lib/amqp_actors/system.rb +++ b/lib/amqp_actors/system.rb @@ -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 diff --git a/spec/actor_spec.rb b/spec/actor_spec.rb index e3b1360..6e5297b 100644 --- a/spec/actor_spec.rb +++ b/spec/actor_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/amqp_spec.rb b/spec/amqp_spec.rb index 763d6f7..5cdcc4b 100644 --- a/spec/amqp_spec.rb +++ b/spec/amqp_spec.rb @@ -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 @@ -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 @@ -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 @@ -77,7 +77,7 @@ class PlainActor < AmqpActors::TestActor expected = 'expected' PlainActor.push(expected) - PlainActor.output.must_equal(expected) + expect(PlainActor.output).must_equal(expected) end end @@ -85,7 +85,7 @@ class PlainActor < AmqpActors::TestActor 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 diff --git a/spec/system_spec.rb b/spec/system_spec.rb index c27bcd6..533703a 100644 --- a/spec/system_spec.rb +++ b/spec/system_spec.rb @@ -1,10 +1,12 @@ +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 @@ -12,8 +14,8 @@ class StartMemoryActor < AmqpActors::TestActor; end 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