Class: Hoodoo::Services::Session::MockDalliClient

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodoo/services/services/session.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::Services::Session::MockDalliClient.new )

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

before :all do # (or ":each")
  Hoodoo::Services::Session::MockDalliClient.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.

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.



662
663
664
# File 'lib/hoodoo/services/services/session.rb', line 662

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.

Returns:

  • (Boolean)


671
672
673
# File 'lib/hoodoo/services/services/session.rb', line 671

def self.bypass?
  @@bypass
end

.resetObject

Wipe out all saved data.



654
655
656
# File 'lib/hoodoo/services/services/session.rb', line 654

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.



648
649
650
# File 'lib/hoodoo/services/services/session.rb', line 648

def self.store
  @@store
end

Instance Method Details

#delete(key) ⇒ Object

Remove data for the given key.



712
713
714
715
716
717
718
719
# File 'lib/hoodoo/services/services/session.rb', line 712

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



680
681
682
683
684
685
686
687
688
# File 'lib/hoodoo/services/services/session.rb', line 680

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.



700
701
702
703
704
705
706
707
708
# File 'lib/hoodoo/services/services/session.rb', line 700

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.



723
724
725
# File 'lib/hoodoo/services/services/session.rb', line 723

def stats
  true
end