Skip to content

Commit 8d569e8

Browse files
committed
(PUP-10942) Cache environments during list
When using versioned code deploys, `list` and `get` could return different modulepaths for the same environment between the time new code was deployed and when the environment was expired. Now the "list" method returns the cached environment (if there is one) or the newly created environment object. In the latter case, we cache the environment, so it can be cleared if/when clear or clear_all are called for that environment.
1 parent 29a5059 commit 8d569e8

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/puppet/environments.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,19 @@ def initialize(loader)
353353

354354
# @!macro loader_list
355355
def list
356-
@loader.list
356+
# Evict all that have expired, in the same way as `get`
357+
clear_all_expired
358+
359+
@loader.list.map do |env|
360+
name = env.name
361+
old_entry = @cache[name]
362+
if old_entry
363+
old_entry.value
364+
else
365+
add_entry(name, entry(env))
366+
env
367+
end
368+
end
357369
end
358370

359371
# @!macro loader_search_paths

spec/unit/environments_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,31 @@
557557
end
558558
end
559559

560+
it "returns the same cached environment object for list and get methods" do
561+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
562+
env = loader.list.find { |e| e.name == :an_environment }
563+
564+
expect(env).to equal(loader.get(:an_environment)) # same object
565+
end
566+
end
567+
568+
it "returns the same cached environment object for multiple list calls" do
569+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
570+
expect(loader.list.first).to equal(loader.list.first) # same object
571+
end
572+
end
573+
574+
it "expires environments and returns a new environment object with the same value" do
575+
Puppet[:environment_timeout] = "0"
576+
577+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
578+
a = loader.list.first
579+
b = loader.list.first
580+
expect(a).to eq(b) # same value
581+
expect(a).to_not equal(b) # not same object
582+
end
583+
end
584+
560585
it "has search_paths" do
561586
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
562587
expect(loader.search_paths).to eq(["file://#{directory_tree.children.first}"])
@@ -719,6 +744,16 @@
719744
expect(service.created_envs).to eq([:an_environment, :an_environment])
720745
expect(service.evicted_envs).to eq([:an_environment])
721746
end
747+
748+
it "evicts expired environments when listing" do
749+
expect(service).to receive(:expired?).with(:an_environment).and_return(true)
750+
751+
with_environment_loaded(service) do |cached|
752+
cached.list
753+
end
754+
755+
expect(service.evicted_envs).to eq([:an_environment])
756+
end
722757
end
723758

724759
context '#clear' do

0 commit comments

Comments
 (0)