Class: Hoodoo::TransientStore::Mocks::DalliClient

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/transient_store/mocks/dalli_client.rb

Overview

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.

Constant Summary collapse

@@store =
{}
@@bypass =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bypass(bypass_boolean) ⇒ Object

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



70
71
72
# File 'lib/hoodoo/transient_store/mocks/dalli_client.rb', line 70

def self.bypass( bypass_boolean )
  @@bypass = bypass_boolean
end

.bypass?Boolean

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

(Deprecated, but indefinitely maintained).

Returns:

  • (Boolean)


81
82
83
# File 'lib/hoodoo/transient_store/mocks/dalli_client.rb', line 81

def self.bypass?
  @@bypass
end

.resetObject

Wipe out all saved data.



60
61
62
# File 'lib/hoodoo/transient_store/mocks/dalli_client.rb', line 60

def self.reset
  @@store = {}
end

.storeObject

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.



54
55
56
# File 'lib/hoodoo/transient_store/mocks/dalli_client.rb', line 54

def self.store
  @@store
end

Instance Method Details

#closeObject

Stub for ‘closing’ a connection.



133
# File 'lib/hoodoo/transient_store/mocks/dalli_client.rb', line 133

def close; end

#delete(key) ⇒ Object

Remove data for the given key.



122
123
124
125
126
127
128
129
# 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) ⇒ Object

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

key

Key to look up (see #set).



90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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.



110
111
112
113
114
115
116
117
118
# 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

#statsObject

Mock ‘stats’ health check.



137
138
139
140
141
142
143
144
# 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