Skip to content

Commit

Permalink
Vider Redis entre chaque spec (#4111)
Browse files Browse the repository at this point in the history
* Vider Redis entre chaque spec

* Use redis-namespace gem to handle parallel_tests

* Define namespace in all envs to easily find app writes

* See resque/redis-namespace#56

* Use a Redis set instead of a list

https://redis.io/docs/data-types/sets/

Could not use command LPOS right now because redis-namespace
does not support it. Besides, using a set is appropriate here.

* Clarify key name

* Add comment for new gem
  • Loading branch information
francois-ferrandis authored Mar 4, 2024
1 parent 1d5cbd9 commit b244d45
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ gem "paper_trail", "< 13.0"
gem "activerecord-postgres_enum"
# A Ruby client library for Redis
gem "redis", "< 5.0"
# Adds a Redis::Namespace class which can be used to namespace calls to Redis.
gem "redis-namespace"

# Devise / auth
# Flexible authentication solution for Rails with Warden
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
redis (4.8.1)
redis-namespace (1.11.0)
redis (>= 4)
regexp_parser (2.8.2)
request_store (1.5.1)
rack (>= 1.4)
Expand Down Expand Up @@ -676,6 +678,7 @@ DEPENDENCIES
rails-erd
rails_autolink
redis (< 5.0)
redis-namespace
rspec-rails
rspec_junit_formatter
rswag-api
Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/rdv/using_waiting_room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
module Rdv::UsingWaitingRoom
extend ActiveSupport::Concern

REDIS_WAITING_ROOM_KEY = "#{Rails.env}:user_in_waiting_room_rdv_id".freeze
REDIS_WAITING_ROOM_KEY = "#{Rails.env}:users_in_waiting_room".freeze

def user_in_waiting_room?
Redis.with_connection do |redis|
status == "unknown" && redis.lpos(REDIS_WAITING_ROOM_KEY, id).present?
status == "unknown" && redis.sismember(REDIS_WAITING_ROOM_KEY, id)
end
end

def set_user_in_waiting_room!
Redis.with_connection do |redis|
redis.lpush(REDIS_WAITING_ROOM_KEY, id)
redis.sadd(REDIS_WAITING_ROOM_KEY, id)
end
end

Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Application < Rails::Application
**redis_settings,
}

config.x.redis_namespace = "app"

config.session_store :cookie_store, key: "_rdv_sp_session"

# Devise layout
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
namespace: "test:cache#{ENV['TEST_ENV_NUMBER']}",
}

config.x.redis_namespace = "test:app:#{ENV['TEST_ENV_NUMBER']}"

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false

Expand Down
7 changes: 4 additions & 3 deletions config/initializers/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

class Redis
def self.with_connection
connection = new(url: Rails.configuration.x.redis_url)
yield(connection)
redis_connection = new(url: Rails.configuration.x.redis_url)
redis_connection = Redis::Namespace.new(Rails.configuration.x.redis_namespace, redis: redis_connection)
yield(redis_connection)
ensure
connection&.close
redis_connection&.close
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
ActionMailer::Base.deliveries.clear
FactoryBot.rewind_sequences
Rails.cache.clear
Redis.with_connection { |redis| redis.del(redis.keys("*")) } # clears custom redis usages
Warden.test_reset!
WebMock.reset!
end
Expand Down

0 comments on commit b244d45

Please sign in to comment.