File tree Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Expand file tree Collapse file tree 2 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ # rubocop:disable Style/Documentation
4+ module Puppet ; end
5+ module Puppet ::ResourceApi ; end
6+ # rubocop:enable Style/Documentation
7+
8+ # This class provides a simple caching mechanism to support minimizing get()
9+ # calls into a provider.
10+ class Puppet ::ResourceApi ::ProviderGetCache
11+ def initialize
12+ clear
13+ end
14+
15+ def clear
16+ @cache = { }
17+ @cached_all = false
18+ end
19+
20+ def all
21+ raise 'all method called, but cached_all not called' unless @cached_all
22+ @cache . values
23+ end
24+
25+ def add ( key , value )
26+ @cache [ key ] = value
27+ end
28+
29+ def get ( key )
30+ @cache [ key ]
31+ end
32+
33+ def cached_all
34+ @cached_all = true
35+ end
36+
37+ def cached_all?
38+ @cached_all
39+ end
40+ end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require 'spec_helper'
4+
5+ RSpec . describe Puppet ::ResourceApi ::ProviderGetCache do
6+ subject ( :cache ) { described_class . new }
7+
8+ before ( :each ) do
9+ cache . add ( :a , 'a' )
10+ cache . add ( :b , 'b' )
11+ end
12+
13+ describe '#add/#get' do
14+ it 'sets and retrieves values from the cache' do
15+ expect ( cache . get ( :a ) ) . to eq 'a'
16+ end
17+ end
18+
19+ describe '#all' do
20+ it 'raises an error when cached_all has not been called' do
21+ expect { cache . all } . to raise_error ( %r{cached_all not called} )
22+ end
23+
24+ it 'returns all values in cache when cached_all has been called' do
25+ cache . cached_all
26+ expect ( cache . all ) . to eq %w[ a b ]
27+ end
28+ end
29+
30+ describe '#cached_all?' do
31+ it 'returns false when cached_all has not been called' do
32+ expect ( cache . cached_all? ) . to be false
33+ end
34+
35+ it 'returns true when cached_all has been called' do
36+ cache . cached_all
37+ expect ( cache . cached_all? ) . to be true
38+ end
39+ end
40+
41+ describe '#clear' do
42+ it 'clears the cache' do
43+ cache . clear
44+ expect ( cache . get ( :a ) ) . to be_nil
45+ end
46+
47+ it 'resets the cached_all flag' do
48+ cache . cached_all
49+ cache . clear
50+ expect ( cache . cached_all? ) . to be false
51+ end
52+ end
53+ end
You can’t perform that action at this time.
0 commit comments