How to test Redis Lock via Rspec?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test Redis Lock via Rspec?

Saurav Kothari

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



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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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 | Threaded
Open this post in threaded view
|

Re: How to test Redis Lock via Rspec?

Hassan Schroeder-2
On Thu, Aug 9, 2018 at 3:32 AM, Saurav Kothari <[hidden email]> wrote:

> We have a Lockable concern that allows for locks via Redis

> When testing this action, I want to test that the correct generated_key is
> being sent to Redis to initiate a lock.

What is the definition of "correct" here? Wouldn't it be easier to just
test the "correctness" of the producer of that key?

--
Hassan Schroeder ------------------------ [hidden email]
twitter: @hassan
Consulting Availability : Silicon Valley or remote

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yB5zxqeLOOHXJjt8ARcnGrqG3HRag4r9mPQX9J25T%3D0xQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.