class Hoodoo::TransientStore::Mocks::DalliClient

Mock known uses of Dalli::Client with test implementations. Use explicitly, or as an RSpec implicit mock via something like this:

allow( Dalli::Client ).to(
  receive( :new ).
  and_return( Hoodoo::TransientStore::Mocks::DalliClient.new )
)

…whenever you need to stub out real Memcached. You will probably want to add:

before :all do # (or ":each")
  Hoodoo::TransientStore::Mocks::DalliClient.reset()
end

…to “clean out Memcached” before or between tests. You can check the contents of mock Memcached by examining ::store‘s hash of data.

The test coverage for Hoodoo::TransientStore selects this backend in passing. Generally speaking you should favour Hoodoo::TransientStore over hard-coding to a storage engine available by the Hoodoo abstraction and, as a result, may never need this mock class at all.

Public Class Methods

bypass( bypass_boolean ) click to toggle source

Pass true to bypass the mock client (subject to the caller reading ::bypass?) to e.g. get test code coverage on real Memcached. Pass false otherwise.

(Deprecated, but indefinitely maintained).

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 70
def self.bypass( bypass_boolean )
  @@bypass = bypass_boolean
end
bypass?() click to toggle source

If true, bypass this class and use real Dalli::Client; else don’t. Default return value is false.

(Deprecated, but indefinitely maintained).

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 81
def self.bypass?
  @@bypass
end
reset() click to toggle source

Wipe out all saved data.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 60
def self.reset
  @@store = {}
end
store() click to toggle source

For test analysis, return the hash of ‘memcached’ mock data.

Entries are referenced by the key you used to originally store them; values are hashes with “:expires_at” giving an expiry time or “nil” and “:value” giving your stored value.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 54
def self.store
  @@store
end

Public Instance Methods

close() click to toggle source

Stub for ‘closing’ a connection.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 133
def close; end
delete( key ) click to toggle source

Remove data for the given key.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 122
def delete( key )
  if @@store.has_key?( key )
    @@store.delete( key )
    true
  else
    false
  end
end
get( key ) click to toggle source

Get the data stored under the given key. Returns nil if not found / expired.

key

Key to look up (see set).

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 90
def get( key )
  data = @@store[ key ]
  return nil if data.nil?

  expires_at = data[ :expires_at ]
  return nil unless expires_at.nil? || Time.now < expires_at

  return data[ :value ]
end
set( key, value, ttl = nil ) click to toggle source

Set data for a given key.

key

Key under which to store data.

value

Data to store.

ttl

(Optional) time-to-live (‘live’ as in living, not as in ‘live TV’) - a value in seconds, after which the data is considered expired. If omitted, the data does not expire.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 110
def set( key, value, ttl = nil )
  data = {
    :expires_at => ttl.nil? ? nil : Time.now.utc + ttl,
    :value      => value
  }

  @@store[ key ] = data
  true
end
stats() click to toggle source

Mock ‘stats’ health check.

# File lib/hoodoo/transient_store/mocks/dalli_client.rb, line 137
def stats

  # Should really be a Hash, but kept as 'true' in case any existing
  # client depends on this; it isn't too important either way.
  #
  true

end