Skip to content

Commit

Permalink
Raise an error when subscribing in the future
Browse files Browse the repository at this point in the history
When client code tries to subscribe to a channel using a `last_id` that doesn't exist on that channel, we raise an exception.

See ae0d7cd#commitcomment-31544627 for details.
  • Loading branch information
benlangfeld committed Dec 4, 2018
1 parent 2d59bcf commit e62f532
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-12-03 22:29:14 +0000 using RuboCop version 0.60.0.
# on 2018-12-04 18:23:24 +0000 using RuboCop version 0.60.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -86,12 +86,12 @@ Metrics/BlockLength:
Metrics/BlockNesting:
Max: 4

# Offense count: 1
# Offense count: 9
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 315

# Offense count: 17
# Offense count: 18
Metrics/CyclomaticComplexity:
Max: 30

Expand All @@ -103,7 +103,7 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 395
Max: 399

# Offense count: 17
Metrics/PerceivedComplexity:
Expand Down Expand Up @@ -329,7 +329,7 @@ Style/IfInsideElse:
Exclude:
- 'lib/message_bus.rb'

# Offense count: 22
# Offense count: 23
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
Expand Down Expand Up @@ -540,7 +540,7 @@ Style/SpecialGlobalVars:
- 'message_bus.gemspec'
- 'spec/spec_helper.rb'

# Offense count: 707
# Offense count: 708
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Expand Down Expand Up @@ -648,7 +648,7 @@ Style/ZeroLengthPredicate:
- 'lib/message_bus/rack/middleware.rb'
- 'spec/lib/message_bus_spec.rb'

# Offense count: 218
# Offense count: 219
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand Down
7 changes: 6 additions & 1 deletion lib/message_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,12 @@ def subscribe_impl(channel, site_id, last_id, &blk)
raise MessageBus::BusDestroyed if @destroyed

if last_id >= 0
backlog(channel, last_id, site_id, inclusive: true).each do |m|
existing_messages = backlog(channel, last_id, site_id, inclusive: true)
if existing_messages.empty?
raise ArgumentError, "You tried to subscribe starting from a message that doesn't exist yet."
end

existing_messages.each do |m|
yield m
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/message_bus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
data1.must_equal ['bananana', "it's so fluffy"]
data2.must_equal ['banana', 'bananana', "it's so fluffy"]
data3.must_equal ['banana', 'bananana', "it's so fluffy"]

lambda do
@bus.subscribe("/minion", 4) do |_msg| # This last_id doesn't exist yet
raise "Don't expect any delivery here"
end
end.must_raise ArgumentError
end

it "can transmit client_ids" do
Expand Down

0 comments on commit e62f532

Please sign in to comment.