Skip to content

Commit

Permalink
Merge pull request rails#54371 from byroot/fix-on-rotate-callback
Browse files Browse the repository at this point in the history
Fix `rotate(on_on_rotation:)` and `#on_rotation`
  • Loading branch information
byroot authored Jan 27, 2025
2 parents bcebc09 + f49f68b commit fd4e570
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion activesupport/lib/active_support/messages/rotator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ def initialize(*args, on_rotation: nil, **options)
@on_rotation = on_rotation
end

def rotate(*args, **options)
def rotate(*args, on_rotation: nil, **options)
@on_rotation = on_rotation if on_rotation
fall_back_to build_rotation(*args, **options)
end

def on_rotation(&on_rotation)
@on_rotation = on_rotation
self
end

def fall_back_to(fallback)
@rotations << fallback
self
Expand Down
20 changes: 20 additions & 0 deletions activesupport/test/messages/message_rotator_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def self.load(*); raise Class.new(StandardError); end
assert called
end

test "rotate(on_rotation:) is called on successful rotation" do
called = nil
codec = make_codec(secret("new")).rotate(secret("old"), on_rotation: proc { called = true })
old_codec = make_codec(secret("old"))
old_message = encode(DATA, old_codec)
assert_equal DATA, decode(old_message, codec)
assert called
end

test "rotate().on_rotation is called on successful rotation" do
called = nil
codec = make_codec(secret("new")).rotate(secret("old")).on_rotation do
called = true
end
old_codec = make_codec(secret("old"))
old_message = encode(DATA, old_codec)
assert_equal DATA, decode(old_message, codec)
assert called
end

test "on_rotation is not called when no rotation is necessary" do
called = nil
assert_rotate [secret("same"), on_rotation: proc { called = true }], [secret("same")]
Expand Down

0 comments on commit fd4e570

Please sign in to comment.