-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support process.action_mailbox (#152)
- Loading branch information
Showing
9 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module RailsBand | ||
module ActionMailbox | ||
module Event | ||
# A wrapper for the event that is passed to `process.action_mailbox`. | ||
class Process < BaseEvent | ||
def mailbox | ||
@mailbox ||= @event.payload.fetch(:mailbox) | ||
end | ||
|
||
def inbound_email | ||
@inbound_email ||= @event.payload.fetch(:inbound_email) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_band/action_mailbox/event/process' | ||
|
||
module RailsBand | ||
module ActionMailbox | ||
# The custom LogSubscriber for ActionMailbox. | ||
class LogSubscriber < ::ActiveSupport::LogSubscriber | ||
mattr_accessor :consumers | ||
|
||
def process(event) | ||
consumer_of(__method__)&.call(Event::Process.new(event)) | ||
end | ||
|
||
private | ||
|
||
def consumer_of(sub_event) | ||
consumers[:"#{sub_event}.action_mailbox"] || consumers[:action_mailbox] || consumers[:default] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
test/rails_band/action_mailbox/event/action_mailbox_process_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class ActionMailboxProcessTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@event = nil | ||
RailsBand::ActionMailbox::LogSubscriber.consumers = { | ||
'process.action_mailbox': ->(event) { @event = event } | ||
} | ||
@user = User.create!(name: 'foo', email: '[email protected]') | ||
end | ||
|
||
test 'returns name' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_equal 'process.action_mailbox', @event.name | ||
end | ||
|
||
test 'returns time' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Float, @event.time | ||
end | ||
|
||
test 'returns end' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Float, @event.end | ||
end | ||
|
||
test 'returns transaction_id' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of String, @event.transaction_id | ||
end | ||
|
||
test 'returns cpu_time' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Float, @event.cpu_time | ||
end | ||
|
||
test 'returns idle_time' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Float, @event.idle_time | ||
end | ||
|
||
test 'returns allocations' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Integer, @event.allocations | ||
end | ||
|
||
test 'returns duration' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of Float, @event.duration | ||
end | ||
|
||
test 'calls #to_h' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
%i[name time end transaction_id cpu_time idle_time allocations duration mailbox inbound_email].each do |key| | ||
assert_includes @event.to_h, key | ||
end | ||
end | ||
|
||
test 'calls #slice' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_equal({ name: 'process.action_mailbox' }, @event.slice(:name)) | ||
end | ||
|
||
test 'returns an instance of Process' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_instance_of RailsBand::ActionMailbox::Event::Process, @event | ||
end | ||
|
||
test 'returns mailbox' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert @event.mailbox | ||
end | ||
|
||
test 'returns inbound_email' do | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_equal({ id: 3, message_id: 'mid', status: 'processing' }, @event.inbound_email) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class ActionMailboxLogSubscriberTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@mock = Minitest::Mock.new | ||
@mock.expect(:recv, nil) | ||
@user = User.create!(name: 'bo', email: '[email protected]') | ||
end | ||
|
||
test 'use the consumer with the exact event name' do | ||
RailsBand::ActionMailbox::LogSubscriber.consumers = { | ||
'process.action_mailbox': ->(_event) { @mock.recv } | ||
} | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_mock @mock | ||
end | ||
|
||
test 'use the consumer with namespace' do | ||
RailsBand::ActionMailbox::LogSubscriber.consumers = { | ||
action_mailbox: ->(event) { @mock.recv if event.name == 'process.action_mailbox' } | ||
} | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_mock @mock | ||
end | ||
|
||
test 'use the consumer with default' do | ||
RailsBand::ActionMailbox::LogSubscriber.consumers = { | ||
default: ->(event) { @mock.recv if event.name == 'process.action_mailbox' } | ||
} | ||
get "/users/#{@user.id}/mailbox" | ||
|
||
assert_mock @mock | ||
end | ||
|
||
test 'do not use the consumer because the event is not for the target' do | ||
RailsBand::ActionMailbox::LogSubscriber.consumers = { | ||
'unknown.action_mailbox': ->(_event) { @mock.recv } | ||
} | ||
get "/users/#{@user.id}/mailbox" | ||
assert_raises MockExpectationError do | ||
@mock.verify | ||
end | ||
end | ||
end |