Skip to content

Commit 83a56a3

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 715992a commit 83a56a3

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
@@ -561,6 +561,31 @@
561561
end
562562
end
563563

564+
it "returns the same cached environment object for list and get methods" do
565+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
566+
env = loader.list.find { |e| e.name == :an_environment }
567+
568+
expect(env).to equal(loader.get(:an_environment)) # same object
569+
end
570+
end
571+
572+
it "returns the same cached environment object for multiple list calls" do
573+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
574+
expect(loader.list.first).to equal(loader.list.first) # same object
575+
end
576+
end
577+
578+
it "expires environments and returns a new environment object with the same value" do
579+
Puppet[:environment_timeout] = "0"
580+
581+
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
582+
a = loader.list.first
583+
b = loader.list.first
584+
expect(a).to eq(b) # same value
585+
expect(a).to_not equal(b) # not same object
586+
end
587+
end
588+
564589
it "has search_paths" do
565590
cached_loader_from(:filesystem => [directory_tree], :directory => directory_tree.children.first) do |loader|
566591
expect(loader.search_paths).to eq(["file://#{directory_tree.children.first}"])
@@ -723,6 +748,16 @@
723748
expect(service.created_envs).to eq([:an_environment, :an_environment])
724749
expect(service.evicted_envs).to eq([:an_environment])
725750
end
751+
752+
it "evicts expired environments when listing" do
753+
expect(service).to receive(:expired?).with(:an_environment).and_return(true)
754+
755+
with_environment_loaded(service) do |cached|
756+
cached.list
757+
end
758+
759+
expect(service.evicted_envs).to eq([:an_environment])
760+
end
726761
end
727762

728763
context '#clear' do

0 commit comments

Comments
 (0)