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? }
  endend



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: :okend


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


------------------------------

Since the lock is cleared at the end of the method call, I cannot check for 
the key in redis as a test.


This answer <https://stackoverflow.com/a/32782446/2651076> recommends 
setting up a fake class that matches the structure of Lockable to emulate 
the same behaviour but how do I write a test for it? The method we have 
does not return any value to verify.


<SO link to the same question 
<https://stackoverflow.com/questions/51764538/how-to-test-redis-lock-via-rspec>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/4e12ede9-45b0-41ee-8309-50b9a6bbfe04%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to