Skip to content

Commit 8a65fb8

Browse files
committed
(GH-26) Add integration tests for caching of workspace information
This commit adds integeration tests for the caching mechanism used in the workspace.
1 parent c7fdb81 commit 8a65fb8

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

lib/puppet-languageserver/document_store.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def self.initialize_store(options = {})
6161
}
6262
end
6363

64+
# This method is mainly used for testin. It expires the cache
65+
def self.expire_store_information
66+
@doc_mutex.synchronize do
67+
@workspace_info_cache[:expires] = Time.new - 120
68+
end
69+
end
70+
6471
def self.store_root_path
6572
store_details[:root_path]
6673
end
@@ -89,7 +96,7 @@ def self.find_root_path(path)
8996

9097
root_path = nil
9198
directory.ascend do |p|
92-
if p.join('metadata.json').exist? || p.join('Puppetfile').exist?
99+
if file_exist?(p.join('metadata.json')) || file_exist?(p.join('Puppetfile'))
93100
root_path = p.to_s
94101
break
95102
end
@@ -119,8 +126,8 @@ def self.store_details
119126
new_cache[:root_path] = @workspace_path
120127
else
121128
new_cache[:root_path] = root_path
122-
new_cache[:has_metadatajson] = File.exist?(File.join(root_path, 'metadata.json'))
123-
new_cache[:has_puppetfile] = File.exist?(File.join(root_path, 'Puppetfile'))
129+
new_cache[:has_metadatajson] = file_exist?(File.join(root_path, 'metadata.json'))
130+
new_cache[:has_puppetfile] = file_exist?(File.join(root_path, 'Puppetfile'))
124131
end
125132
end
126133
new_cache[:expires] = Time.new + WORKSPACE_CACHE_TTL_SECONDS
@@ -131,5 +138,10 @@ def self.store_details
131138
@workspace_info_cache
132139
end
133140
private_class_method :store_details
141+
142+
def self.file_exist?(path)
143+
File.exist?(path)
144+
end
145+
private_class_method :file_exist?
134146
end
135147
end

spec/languageserver/integration/puppet-languageserver/document_store_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@
4545
end
4646
end
4747

48+
RSpec.shared_examples 'a cached workspace' do
49+
it 'should cache the information' do
50+
expect(subject).to receive(:file_exist?).at_least(:once)
51+
result = PuppetLanguageServer::DocumentStore.store_root_path
52+
# Subsequent calls should be cached
53+
expect(subject).to receive(:file_exist?).exactly(0).times
54+
result = PuppetLanguageServer::DocumentStore.store_root_path
55+
result = PuppetLanguageServer::DocumentStore.store_root_path
56+
result = PuppetLanguageServer::DocumentStore.store_root_path
57+
end
58+
59+
it 'should recache the information when the cache expires' do
60+
result = PuppetLanguageServer::DocumentStore.store_root_path
61+
# Expire the cache
62+
PuppetLanguageServer::DocumentStore.expire_store_information
63+
expect(subject).to receive(:file_exist?).at_least(:once)
64+
result = PuppetLanguageServer::DocumentStore.store_root_path
65+
# Subsequent calls should be cached
66+
expect(subject).to receive(:file_exist?).exactly(0).times
67+
result = PuppetLanguageServer::DocumentStore.store_root_path
68+
result = PuppetLanguageServer::DocumentStore.store_root_path
69+
result = PuppetLanguageServer::DocumentStore.store_root_path
70+
end
71+
end
72+
4873
# Empty or missing workspace
4974
context 'given a workspace option which is nil' do
5075
let(:server_options) { {} }
@@ -54,6 +79,24 @@
5479
end
5580

5681
it_should_behave_like 'an empty workspace', nil
82+
83+
it 'should cache the information' do
84+
expect(subject).to receive(:file_exist?).exactly(0).times
85+
result = PuppetLanguageServer::DocumentStore.store_root_path
86+
result = PuppetLanguageServer::DocumentStore.store_root_path
87+
result = PuppetLanguageServer::DocumentStore.store_root_path
88+
result = PuppetLanguageServer::DocumentStore.store_root_path
89+
end
90+
91+
it 'should not recache the information when the cache expires' do
92+
expect(subject).to receive(:file_exist?).exactly(0).times
93+
result = PuppetLanguageServer::DocumentStore.store_root_path
94+
PuppetLanguageServer::DocumentStore.expire_store_information
95+
result = PuppetLanguageServer::DocumentStore.store_root_path
96+
result = PuppetLanguageServer::DocumentStore.store_root_path
97+
result = PuppetLanguageServer::DocumentStore.store_root_path
98+
result = PuppetLanguageServer::DocumentStore.store_root_path
99+
end
57100
end
58101

59102
context 'given a workspace option with a missing directory' do
@@ -77,6 +120,7 @@
77120
end
78121

79122
it_should_behave_like 'a puppetfile workspace', expected_root
123+
it_should_behave_like 'a cached workspace'
80124
end
81125

82126
context 'given a workspace option which has a parent directory with a puppetfile' do
@@ -90,6 +134,7 @@
90134
end
91135

92136
it_should_behave_like 'a puppetfile workspace', expected_root
137+
it_should_behave_like 'a cached workspace'
93138
end
94139

95140
# Module metadata style workspaces
@@ -103,6 +148,7 @@
103148
end
104149

105150
it_should_behave_like 'a metadata.json workspace', expected_root
151+
it_should_behave_like 'a cached workspace'
106152
end
107153

108154
context 'given a workspace option which has a parent directory with metadata.json' do
@@ -116,5 +162,6 @@
116162
end
117163

118164
it_should_behave_like 'a metadata.json workspace', expected_root
165+
it_should_behave_like 'a cached workspace'
119166
end
120167
end

0 commit comments

Comments
 (0)