Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spec operations that rely on Memcached?

We have a Rails application that we test with RSpec. We want to spec operations that rely on Memcached. What is the best practice to do so?

I thought of doing this by stubbing all calls to Rails.cache. Is this a good idea?

As per @Pan Thomakos suggestion, I'm adding some additional details about one of the scenarios I'm trying to test:

We have the concept of accounts in our system, therefore on every request we retrieve the current user and the current account. Because there are not many accounts in the system, we keep them all in cache and retrieve them from there.

def self.find_by_slug(slug)  
  Rails.cache.fetch(Account.cache_key_for_slug(slug), :expires_in => 1.day) { super }  
end

For this reason, caching in this case isn't just a nice to have behavior, but the expected behavior and the thing I want to test. Therefore turning off caching won't do.

like image 919
arikfr Avatar asked May 11 '11 18:05

arikfr


1 Answers

Test without stubbing IMHO!

The sequence would look like this:

  1. Cache.flush # or equivalent
  2. Cache.get(slug).shouldbe null # test cache is empty
  3. Method.find_by_slug(slug).should == 'some value' # test that method words
  4. Cache.get(slug).should == 'some value' # test that cache has value.

Personally, I believe if you have the resources on hand then stubbing SHOULD NOT be used. If you do not have the resources on hand (IE a 3rd party service) then stubbing SHOULD BE used.

The problem with stubbing, is that if you changed the code that you are stubbing, then you won't know if it breaks.

An example in this case would be if you switched from the standard memcache gem, to Dahli?, or some other memcache gem which handed cache misses by returning false, null or some other value differently. I mean really! Cache.set("my_key", false)! :)

A example for switching, would be to leave the ASCII protocol and move to the faster binary protocol.

Memcache is a cheap resource, you can set it up with 1 meg of ram to do this testing. I would even go as far as to say you can do the same for mysql. Anything bigger than mysql, then I would start leaning towards stubbing as the cost to "setup" those resources becomes significant. YMMV.

-daniel

like image 183
Daniel Avatar answered Nov 15 '22 07:11

Daniel