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

Default to EMERGENCY_BANNER_REDIS_URL in Emergency Banner's Redis #3445

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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 lib/emergency_banner/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Display
class << self
def client
@client ||= Redis.new(
url: ENV.fetch("EMERGENCY_BANNER_REDIS_URL", ENV["REDIS_URL"]),
reconnect_attempts: [
15,
30,
Expand Down
55 changes: 55 additions & 0 deletions test/unit/emergency_banner/display_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@
@banner = EmergencyBanner::Display.new
end

context ".client" do
context "when the EMERGENCY_BANNER_REDIS_URL environment variable has been set" do
should "use that value as the URL for the Redis client" do
mock_env("EMERGENCY_BANNER_REDIS_URL" => "redis://emergency-banner") do
EmergencyBanner::Display.instance_variable_set(:@client, nil)

Redis.expects(:new).with(
url: "redis://emergency-banner",
reconnect_attempts: [
15,
30,
45,
60,
],
)

EmergencyBanner::Display.client
end
end
end

context "when the EMERGENCY_BANNER_REDIS_URL environment variable has not been set" do
should "use the default REDIS_URL as the URL for the Redis client" do
mock_env({
"EMERGENCY_BANNER_REDIS_URL" => nil,
"REDIS_URL" => "redis://my-redis-url",
}) do
EmergencyBanner::Display.instance_variable_set(:@client, nil)

Redis.expects(:new).with(
url: "redis://my-redis-url",
reconnect_attempts: [
15,
30,
45,
60,
],
)

EmergencyBanner::Display.client
end
end
end
end

context "#enabled?" do
should "return enabled is false when redis connection times out and send an error notification" do
err = Redis::CannotConnectError.new("Timed out connecting to Redis")
Expand Down Expand Up @@ -155,5 +200,15 @@
assert_nil @banner.link_text
end
end

def mock_env(partial_env_hash)
old_env = ENV.to_hash
ENV.update partial_env_hash
begin
yield
ensure
ENV.replace old_env
end
end
end
# rubocop:enable Rails/RefuteMethods
Loading