Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Incorrect event name logging when subscribing with :with option #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## HEAD (unreleased)

Authors: Anthony Chao

* fix: Incorrect event name logging with :with option

## 3.0.0 (23rd May 2024)

Authors: maboelnour, kianmeng
Authors: maboelnour, kianmeng

* docs: fix typos and example code

Expand Down
4 changes: 2 additions & 2 deletions lib/wisper/broadcasters/logger_broadcaster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ def initialize(logger, broadcaster)
@broadcaster = broadcaster
end

def broadcast(listener, publisher, event, *args, **kwargs)
def broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
@logger.info("[WISPER] #{name(publisher)} published #{event} to #{name(listener)} with #{args_info(args)} and #{kwargs_info(kwargs)}")
@broadcaster.broadcast(listener, publisher, event, *args, **kwargs)
@broadcaster.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/wisper/broadcasters/send_broadcaster.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Wisper
module Broadcasters
class SendBroadcaster
def broadcast(listener, publisher, event, *args, **kwargs)
listener.public_send(event, *args, **kwargs)
def broadcast(listener, publisher, method_to_call, _event, *args, **kwargs)
listener.public_send(method_to_call, *args, **kwargs)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wisper/registration/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(listener, **options)
def broadcast(event, publisher, *args, **kwargs)
method_to_call = map_event_to_method(event)
if should_broadcast?(event) && listener.respond_to?(method_to_call) && publisher_in_scope?(publisher)
broadcaster.broadcast(listener, publisher, method_to_call, *args, **kwargs)
broadcaster.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end
end

Expand Down
27 changes: 18 additions & 9 deletions spec/lib/wisper/broadcasters/logger_broadcaster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ module Broadcasters
publisher.send(:broadcast, :it_happened, 1, 2, key: 'value')
end
end

context 'with specified method' do
it 'broadcasts the event to the listener' do
publisher.subscribe(listener, :broadcaster => LoggerBroadcaster.new(logger, Wisper::Broadcasters::SendBroadcaster.new), :with => :call)
expect(listener).to receive(:call).with(1, 2, key: 'value')
publisher.send(:broadcast, :it_happened, 1, 2, key: 'value')
end
end
end

describe 'unit tests:' do
Expand All @@ -45,6 +53,7 @@ module Broadcasters
let(:logger) { double('Logger').as_null_object }
let(:broadcaster) { double('Broadcaster').as_null_object }
let(:event) { 'thing_created' }
let(:method_to_call) { 'call' }

subject { LoggerBroadcaster.new(logger, broadcaster) }

Expand All @@ -55,12 +64,12 @@ module Broadcasters

it 'logs published event' do
expect(logger).to receive(:info).with('[WISPER] Publisher#1 published thing_created to Listener#2 with no arguments and no keyword arguments')
subject.broadcast(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end

it 'delegates broadcast to a given broadcaster' do
expect(broadcaster).to receive(:broadcast).with(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, event, *args, **kwargs)
expect(broadcaster).to receive(:broadcast).with(listener, publisher, method_to_call, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end
end

Expand All @@ -70,12 +79,12 @@ module Broadcasters

it 'logs published event and arguments' do
expect(logger).to receive(:info).with("[WISPER] Publisher#1 published thing_created to Listener#2 with Argument#3, Argument#4 and keyword arguments {:x=>:y}")
subject.broadcast(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end

it 'delegates broadcast to a given broadcaster' do
expect(broadcaster).to receive(:broadcast).with(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, event, *args, **kwargs)
expect(broadcaster).to receive(:broadcast).with(listener, publisher, method_to_call, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end

context 'when argument is a hash' do
Expand All @@ -85,7 +94,7 @@ module Broadcasters

it 'logs published event and arguments' do
expect(logger).to receive(:info).with("[WISPER] Publisher#1 published thing_created to Listener#2 with Hash##{hash.object_id}: #{hash.inspect} and keyword arguments {:x=>:y}")
subject.broadcast(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end
end

Expand All @@ -95,7 +104,7 @@ module Broadcasters

it 'logs published event and arguments' do
expect(logger).to receive(:info).with("[WISPER] Publisher#1 published thing_created to Listener#2 with #{number.class.name}##{number.object_id}: 10 and keyword arguments {:x=>:y}")
subject.broadcast(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end
end

Expand All @@ -104,7 +113,7 @@ module Broadcasters

it 'logs published event and arguments' do
expect(logger).to receive(:info).with("[WISPER] Publisher#1 published thing_created to Listener#2 with no arguments and keyword arguments {:x=>:y}")
subject.broadcast(listener, publisher, event, *args, **kwargs)
subject.broadcast(listener, publisher, method_to_call, event, *args, **kwargs)
end
end
end
Expand Down
31 changes: 16 additions & 15 deletions spec/lib/wisper/broadcasters/send_broadcaster_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module Wisper
module Broadcasters
describe SendBroadcaster do
let(:listener) { double('listener') }
let(:event) { 'thing_created' }
let(:listener) { double('listener') }
let(:method_to_call) { 'call' }
let(:event) { 'thing_created' }

describe '#broadcast' do
context 'without arguments' do
it 'sends event to listener without any arguments' do
if RUBY_VERSION < '3.0'
expect(listener).to receive(event).with({})
expect(listener).to receive(method_to_call).with({})
else
expect(listener).to receive(event).with(no_args)
expect(listener).to receive(method_to_call).with(no_args)
end
subject.broadcast(listener, anything, event)
subject.broadcast(listener, anything, method_to_call, event)
end
end

Expand All @@ -21,11 +22,11 @@ module Broadcasters

it 'sends event to listener without any arguments' do
if RUBY_VERSION < '3.0'
expect(listener).to receive(event).with({})
expect(listener).to receive(method_to_call).with({})
else
expect(listener).to receive(event).with(no_args)
expect(listener).to receive(method_to_call).with(no_args)
end
subject.broadcast(listener, anything, event, *args)
subject.broadcast(listener, anything, method_to_call, event, *args)
end
end

Expand All @@ -35,20 +36,20 @@ module Broadcasters

it 'sends event to listener with arguments' do
if RUBY_VERSION < '3.0'
expect(listener).to receive(event).with(1, 2, 3, {})
expect(listener).to receive(method_to_call).with(1, 2, 3, {})
else
expect(listener).to receive(event).with(1, 2, 3)
expect(listener).to receive(method_to_call).with(1, 2, 3)
end
subject.broadcast(listener, anything, event, *args)
subject.broadcast(listener, anything, method_to_call, event, *args)
end
end

context 'with only keyword arguments' do
let(:kwargs) { { key: 'value' } }

it 'sends event to listener with arguments' do
expect(listener).to receive(event).with({key: 'value'})
subject.broadcast(listener, anything, event, **kwargs)
expect(listener).to receive(method_to_call).with({key: 'value'})
subject.broadcast(listener, anything, method_to_call, event, **kwargs)
end
end

Expand All @@ -57,8 +58,8 @@ module Broadcasters
let(:kwargs) { { key: 'value' } }

it 'sends event to listener with arguments' do
expect(listener).to receive(event).with(1,2,3, {key: 'value'})
subject.broadcast(listener, anything, event, *args, **kwargs)
expect(listener).to receive(method_to_call).with(1,2,3, {key: 'value'})
subject.broadcast(listener, anything, method_to_call, event, *args, **kwargs)
end
end
end
Expand Down