Skip to content

Commit 5b6b5a3

Browse files
author
Glyn Normington
committed
Issuing warnings using logger
Fix the corresponding testcase to clear buildpack.log before each test and check the log for the required output. [#54192868]
1 parent 0ef8178 commit 5b6b5a3

4 files changed

Lines changed: 23 additions & 8 deletions

File tree

lib/java_buildpack/diagnostics/common.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ def self.get_diagnostic_directory(app_dir)
2828
File.join(app_dir, DIAGNOSTICS_DIRECTORY)
2929
end
3030

31+
def self.get_buildpack_log(app_dir)
32+
File.join(get_diagnostic_directory(app_dir), LOG_FILE_NAME)
33+
end
34+
3135
end

lib/java_buildpack/jre/memory/weight_balancing_memory_heuristic.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'java_buildpack/diagnostics/logger_factory'
1718
require 'java_buildpack/jre'
1819
require 'java_buildpack/jre/memory/memory_limit'
1920
require 'java_buildpack/jre/memory/memory_size'
@@ -34,6 +35,7 @@ class WeightBalancingMemoryHeuristic
3435
# @param [Array<String>] valid_heuristics the valid heuristics keys
3536
# @param [Hash<String, String>] java_opts a mapping from a memory type to a +JAVA_OPTS+ option
3637
def initialize(sizes, heuristics, valid_sizes, valid_heuristics, java_opts)
38+
@logger = JavaBuildpack::Diagnostics::LoggerFactory.get_logger
3739
validate 'size', valid_sizes, sizes.keys
3840
validate 'heuristic', valid_heuristics, heuristics.keys
3941

@@ -112,7 +114,7 @@ def create_memory_buckets(sizes, heuristics, memory_limit)
112114
def issue_memory_wastage_warning(buckets)
113115
native_bucket = buckets['native']
114116
if native_bucket && native_bucket.size > native_bucket.default_size * NATIVE_MEMORY_WARNING_FACTOR
115-
$stderr.puts "-----> WARNING: there is #{NATIVE_MEMORY_WARNING_FACTOR} times more spare native memory than the default, so configured Java memory may be too small."
117+
@logger.warn "There is #{NATIVE_MEMORY_WARNING_FACTOR} times more spare native memory than the default, so configured Java memory may be too small."
116118
end
117119
end
118120

@@ -137,7 +139,7 @@ def issue_close_to_default_warnings(buckets, heuristics, memory_limit)
137139
factor = ((actual_size - default_size) / default_size).abs
138140
end
139141
if (default_size == MemorySize::ZERO && actual_size == MemorySize::ZERO) || factor < CLOSE_TO_DEFAULT_FACTOR
140-
$stderr.puts "-----> WARNING: the configured value #{actual_size} of memory size #{type} is close to the default value #{default_size}. Consider deleting the configured value and taking the default."
142+
@logger.warn "The configured value #{actual_size} of memory size #{type} is close to the default value #{default_size}. Consider deleting the configured value and taking the default."
141143
end
142144
end
143145
end

spec/java_buildpack/jre/memory/weight_balancing_memory_heuristic_spec.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18+
require 'java_buildpack/diagnostics/common'
19+
require 'java_buildpack/diagnostics/logger_factory'
1820
require 'java_buildpack/jre/memory/weight_balancing_memory_heuristic'
1921

2022
module JavaBuildpack::Jre
@@ -44,7 +46,9 @@ module JavaBuildpack::Jre
4446
VALID_SIZES = %w(heap permgen stack)
4547

4648
before do
47-
$stderr = StringIO.new
49+
File.delete(JavaBuildpack::Diagnostics.get_buildpack_log Dir.tmpdir)
50+
JavaBuildpack::Diagnostics::LoggerFactory.send :close # suppress warnings
51+
JavaBuildpack::Diagnostics::LoggerFactory.create_logger Dir.tmpdir
4852
end
4953

5054
it 'should fail if a memory limit is negative' do
@@ -182,28 +186,28 @@ module JavaBuildpack::Jre
182186
expect(output).to include('-Xmx1M')
183187
expect(output).to include('-XX:MaxPermSize=1M')
184188
expect(output).to include('-Xss2M')
185-
expect($stderr.string).to match(/WARNING:/)
189+
expect(buildpack_log_contents).to match(/WARN/)
186190
end
187191
end
188192

189193
it 'should issue a warning when the specified stack size is close to the default' do
190194
with_memory_limit('4096m') do
191195
WeightBalancingMemoryHeuristic.new({ 'stack' => '1025k' }, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
192-
expect($stderr.string).to match(/WARNING:.*close to the default/)
196+
expect(buildpack_log_contents).to match(/WARN.*is close to the default/)
193197
end
194198
end
195199

196200
it 'should issue a warning when the specified maximum heap size is close to the default' do
197201
with_memory_limit('4096m') do
198202
WeightBalancingMemoryHeuristic.new({ 'heap' => '2049m' }, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
199-
expect($stderr.string).to match(/WARNING:.*close to the default/)
203+
expect(buildpack_log_contents).to match(/WARN.*is close to the default/)
200204
end
201205
end
202206

203207
it 'should issue a warning when the specified maximum permgen size is close to the default' do
204208
with_memory_limit('4096m') do
205209
WeightBalancingMemoryHeuristic.new({ 'permgen' => '1339m' }, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
206-
expect($stderr.string).to match(/WARNING:.*close to the default/)
210+
expect(buildpack_log_contents).to match(/WARN.*is close to the default/)
207211
end
208212
end
209213

@@ -229,6 +233,10 @@ def with_memory_limit(memory_limit)
229233
ENV['MEMORY_LIMIT'] = previous_value
230234
end
231235

236+
def buildpack_log_contents
237+
File.read(JavaBuildpack::Diagnostics.get_buildpack_log(Dir.tmpdir))
238+
end
239+
232240
end
233241

234242
end

spec/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
require 'java_buildpack/diagnostics/common'
3434
require 'java_buildpack/diagnostics/logger_factory'
3535
tmpdir = Dir.tmpdir
36-
FileUtils.rm_rf File.join(tmpdir, JavaBuildpack::Diagnostics::DIAGNOSTICS_DIRECTORY)
36+
diagnostics_directory = File.join(tmpdir, JavaBuildpack::Diagnostics::DIAGNOSTICS_DIRECTORY)
37+
FileUtils.rm_rf diagnostics_directory
3738
JavaBuildpack::Diagnostics::LoggerFactory.create_logger tmpdir

0 commit comments

Comments
 (0)