From 4b303bcc1bd9cd5bede04eaeb70207f471a38a6b Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Thu, 27 Mar 2014 13:23:06 -0400 Subject: [PATCH] Add Invocation.invoke so we can access the result Up until now, discarding the result was fine. Now that issue trackers return urls to the created tickets, we need to preserve the result and return it from any invocations. --- lib/cc/service/invocation.rb | 11 +++++++++-- test/invocation_test.rb | 20 ++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/cc/service/invocation.rb b/lib/cc/service/invocation.rb index 7c724bc..f643c0e 100644 --- a/lib/cc/service/invocation.rb +++ b/lib/cc/service/invocation.rb @@ -10,6 +10,8 @@ class CC::Service::Invocation error_handling: WithErrorHandling, } + attr_reader :result + # Build a chain of invocation wrappers which eventually calls receive # on the given service, then execute that chain. # @@ -17,7 +19,7 @@ class CC::Service::Invocation # # Usage: # - # CC::Service::Invocation.new(service) do |i| + # CC::Service::Invocation.invoke(service) do |i| # i.with :retries, 3 # i.with :metrics, $statsd # i.with :error_handling, Rails.logger @@ -28,12 +30,17 @@ class CC::Service::Invocation # metrics collector, then up again to the error handling. If the order # were reversed, the error handling middleware would prevent the other # middleware from seeing any exceptions at all. + def self.invoke(service, &block) + instance = new(service, &block) + instance.result + end + def initialize(service) @chain = InvocationChain.new { service.receive } yield(self) if block_given? - @chain.call + @result = @chain.call end def with(middleware, *args) diff --git a/test/invocation_test.rb b/test/invocation_test.rb index 1d89bc7..90e8d4a 100644 --- a/test/invocation_test.rb +++ b/test/invocation_test.rb @@ -2,11 +2,12 @@ class TestInvocation < Test::Unit::TestCase def test_success - service = FakeService.new + service = FakeService.new(:some_result) - CC::Service::Invocation.new(service) + result = CC::Service::Invocation.invoke(service) assert_equal 1, service.receive_count + assert_equal :some_result, result end def test_retries @@ -15,7 +16,7 @@ def test_retries error_occurred = false begin - CC::Service::Invocation.new(service) do |i| + CC::Service::Invocation.invoke(service) do |i| i.with :retries, 3 end rescue @@ -29,7 +30,7 @@ def test_retries def test_metrics statsd = FakeStatsd.new - CC::Service::Invocation.new(FakeService.new) do |i| + CC::Service::Invocation.invoke(FakeService.new) do |i| i.with :metrics, statsd, "a_prefix" end @@ -44,7 +45,7 @@ def test_metrics_on_errors error_occurred = false begin - CC::Service::Invocation.new(service) do |i| + CC::Service::Invocation.invoke(service) do |i| i.with :metrics, statsd, "a_prefix" end rescue @@ -61,7 +62,7 @@ def test_error_handling service = FakeService.new service.raise_on_receive = true - CC::Service::Invocation.new(service) do |i| + CC::Service::Invocation.invoke(service) do |i| i.with :error_handling, logger, "a_prefix" end @@ -74,7 +75,7 @@ def test_multiple_middleware service.raise_on_receive = true logger = FakeLogger.new - CC::Service::Invocation.new(service) do |i| + CC::Service::Invocation.invoke(service) do |i| i.with :retries, 3 i.with :error_handling, logger end @@ -89,7 +90,8 @@ class FakeService attr_reader :receive_count attr_accessor :raise_on_receive - def initialize + def initialize(result = nil) + @result = result @receive_count = 0 end @@ -99,6 +101,8 @@ def receive if @raise_on_receive raise "Boom" end + + @result end end