Skip to content

Commit b244d45

Browse files
Vider Redis entre chaque spec (#4111)
* 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
1 parent 1d5cbd9 commit b244d45

File tree

7 files changed

+17
-6
lines changed

7 files changed

+17
-6
lines changed

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ gem "paper_trail", "< 13.0"
5353
gem "activerecord-postgres_enum"
5454
# A Ruby client library for Redis
5555
gem "redis", "< 5.0"
56+
# Adds a Redis::Namespace class which can be used to namespace calls to Redis.
57+
gem "redis-namespace"
5658

5759
# Devise / auth
5860
# Flexible authentication solution for Rails with Warden

Gemfile.lock

+3
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ GEM
443443
rb-inotify (0.10.1)
444444
ffi (~> 1.0)
445445
redis (4.8.1)
446+
redis-namespace (1.11.0)
447+
redis (>= 4)
446448
regexp_parser (2.8.2)
447449
request_store (1.5.1)
448450
rack (>= 1.4)
@@ -676,6 +678,7 @@ DEPENDENCIES
676678
rails-erd
677679
rails_autolink
678680
redis (< 5.0)
681+
redis-namespace
679682
rspec-rails
680683
rspec_junit_formatter
681684
rswag-api

app/models/concerns/rdv/using_waiting_room.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
module Rdv::UsingWaitingRoom
44
extend ActiveSupport::Concern
55

6-
REDIS_WAITING_ROOM_KEY = "#{Rails.env}:user_in_waiting_room_rdv_id".freeze
6+
REDIS_WAITING_ROOM_KEY = "#{Rails.env}:users_in_waiting_room".freeze
77

88
def user_in_waiting_room?
99
Redis.with_connection do |redis|
10-
status == "unknown" && redis.lpos(REDIS_WAITING_ROOM_KEY, id).present?
10+
status == "unknown" && redis.sismember(REDIS_WAITING_ROOM_KEY, id)
1111
end
1212
end
1313

1414
def set_user_in_waiting_room!
1515
Redis.with_connection do |redis|
16-
redis.lpush(REDIS_WAITING_ROOM_KEY, id)
16+
redis.sadd(REDIS_WAITING_ROOM_KEY, id)
1717
end
1818
end
1919

config/application.rb

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Application < Rails::Application
5959
**redis_settings,
6060
}
6161

62+
config.x.redis_namespace = "app"
63+
6264
config.session_store :cookie_store, key: "_rdv_sp_session"
6365

6466
# Devise layout

config/environments/test.rb

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
namespace: "test:cache#{ENV['TEST_ENV_NUMBER']}",
3737
}
3838

39+
config.x.redis_namespace = "test:app:#{ENV['TEST_ENV_NUMBER']}"
40+
3941
# Raise exceptions instead of rendering exception templates.
4042
config.action_dispatch.show_exceptions = false
4143

config/initializers/redis.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
class Redis
44
def self.with_connection
5-
connection = new(url: Rails.configuration.x.redis_url)
6-
yield(connection)
5+
redis_connection = new(url: Rails.configuration.x.redis_url)
6+
redis_connection = Redis::Namespace.new(Rails.configuration.x.redis_namespace, redis: redis_connection)
7+
yield(redis_connection)
78
ensure
8-
connection&.close
9+
redis_connection&.close
910
end
1011
end

spec/rails_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
ActionMailer::Base.deliveries.clear
112112
FactoryBot.rewind_sequences
113113
Rails.cache.clear
114+
Redis.with_connection { |redis| redis.del(redis.keys("*")) } # clears custom redis usages
114115
Warden.test_reset!
115116
WebMock.reset!
116117
end

0 commit comments

Comments
 (0)