Class: Hoodoo::Services::Session::MockDalliClient
- Inherits:
-
Object
- Object
- Hoodoo::Services::Session::MockDalliClient
- 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
-
.bypass(bypass_boolean) ⇒ Object
Pass
true
to bypass the mock client (subject to the caller reading ::bypass?) to e.g. -
.bypass? ⇒ Boolean
If
true
, bypass this class and use real Dalli::Client; else don’t. -
.reset ⇒ Object
Wipe out all saved data.
-
.store ⇒ Object
For test analysis, return the hash of ‘memcached’ mock data.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Remove data for the given key.
-
#get(key) ⇒ Object
Get the data stored under the given key.
-
#set(key, value, ttl = nil) ⇒ Object
Set data for a given key.
-
#stats ⇒ Object
Mock ‘stats’ health check.
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.
677 678 679 |
# File 'lib/hoodoo/services/services/session.rb', line 677 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
.
686 687 688 |
# File 'lib/hoodoo/services/services/session.rb', line 686 def self.bypass? @@bypass end |
.reset ⇒ Object
Wipe out all saved data.
669 670 671 |
# File 'lib/hoodoo/services/services/session.rb', line 669 def self.reset @@store = {} end |
.store ⇒ Object
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.
663 664 665 |
# File 'lib/hoodoo/services/services/session.rb', line 663 def self.store @@store end |
Instance Method Details
#delete(key) ⇒ Object
Remove data for the given key.
727 728 729 730 731 732 733 734 |
# File 'lib/hoodoo/services/services/session.rb', line 727 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).
695 696 697 698 699 700 701 702 703 |
# File 'lib/hoodoo/services/services/session.rb', line 695 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.
715 716 717 718 719 720 721 722 723 |
# File 'lib/hoodoo/services/services/session.rb', line 715 def set( key, value, ttl = nil ) data = { :expires_at => ttl.nil? ? nil : Time.now.utc + ttl, :value => value } @@store[ key ] = data true end |
#stats ⇒ Object
Mock ‘stats’ health check.
738 739 740 |
# File 'lib/hoodoo/services/services/session.rb', line 738 def stats true end |