We have a Lockable
concern that allows for locks via Redis
module Lockable
extend ActiveSupport::Concern
def redis_lock(key, options = {})
Redis::Lock.new(
key,
expiration: options[:expiration] || 15,
timeout: options[:timeout] || 0.1
).lock { yield if block_given? }
end
end
We use this in a Controller method to ensure concurrent requests are handled correctly.
def create
redis_lock(<generated_key>, timeout: 15) do
# perform_operation
end
render json: <data>, status: :ok
end
When testing this action, I want to test that the correct generated_key
is being sent to Redis to initiate a lock.
I set up an expect for the Redis::Lock but that returns false always presumably because the request to create is sent mid request and not at the end of it.
expect(Redis::Lock).to receive(:create).once