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

In topic routing, wildcards should match blank values and * should only match one word. #37

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions bunny-mock.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'term-ansicolor', '~> 1.3.0'
s.add_development_dependency 'tins', '= 1.6.0'
s.add_development_dependency 'coveralls'
s.add_development_dependency 'pry'

s.files = `git ls-files`.split "\n"
s.test_files = `git ls-files -- spec/*`.split "\n"
Expand Down
6 changes: 3 additions & 3 deletions lib/bunny_mock/exchanges/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def deliver(payload, opts, key)
# @private
def route_to_regex(key)
key = key.gsub('.', '\.')
key = key.gsub(SINGLE_WILDCARD, '(?:\w+)')
key = key.gsub(MULTI_WILDCARD, '\w+\.?')
key = key.gsub(SINGLE_WILDCARD, '(?:\w*)')
key = key.gsub(MULTI_WILDCARD, '[\w\.]*\.?')

Regexp.new(key)
Regexp.new("^#{key}$")
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/bunny_mock/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Queue
def initialize(channel, name = '', opts = {})
# Store creation information
@channel = channel
@name = name
@name = name == '' ? random_name : name
@opts = opts

# Store messages
Expand Down Expand Up @@ -277,5 +277,10 @@ def store_acknowledgement(response, args)
@channel.acknowledged_state[:pending][delivery_tag] = response
end
end

def random_name
letters = ('a' .. 'z').to_a
(1 .. 15).map { |i| letters[rand(letters.size)] }.join('')
end
end
end
37 changes: 35 additions & 2 deletions spec/unit/bunny_mock/exchanges/topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,45 @@
expect(@third.message_count).to eql 0
end

it 'should deliver for mixed wildcards' do
it 'should deliver for mixed wildcards, matching * to a single word only' do
@source.publish 'Test', routing_key: 'queue.category.sub.third'

expect(@first.message_count).to eql 1
expect(@second.message_count).to eql 1
expect(@second.message_count).to eql 0
expect(@third.message_count).to eql 1
end

it 'should allow wildcards to match blank values' do
@source.publish 'Test', routing_key: 'queue..sub'

expect(@first.message_count).to eql 1
expect(@second.message_count).to eql 1
expect(@third.message_count).to eql 0
end

it 'should deliver to the correct queue' do
company_queue = @channel.queue
company_queue.bind(@source, routing_key: '*.company.*.*')
@source.publish('Test', routing_key: '123.company.444.245')
expect(company_queue.message_count).to eql 1
end

it 'should not deliver to the wrong queue' do
company_queue = @channel.queue
company_queue.bind(@source, routing_key: '*.company.*.*')
@source.publish('Test', routing_key: '.user.create.188')
expect(company_queue.message_count).to eql 0
end

it 'should not deliver to the wrong queue when there is another subscription' do
company_queue = @channel.queue
company_queue.bind(@source, routing_key: '*.company.*.*')

user_queue = @channel.queue
user_queue.bind(@source, routing_key: '*.user.*.*')

@source.publish('Test', routing_key: '.user.create.188')
expect(company_queue.message_count).to eql 0
end
end
end