Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/cc/service/invocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ 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.
#
# Order is important. Each call to #with, wraps the last.
#
# 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
Expand All @@ -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)
Expand Down
20 changes: 12 additions & 8 deletions test/invocation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -99,6 +101,8 @@ def receive
if @raise_on_receive
raise "Boom"
end

@result
end
end

Expand Down