diff options
Diffstat (limited to 'spec/lib/gitlab/memory/jemalloc_spec.rb')
-rw-r--r-- | spec/lib/gitlab/memory/jemalloc_spec.rb | 49 |
1 files changed, 18 insertions, 31 deletions
diff --git a/spec/lib/gitlab/memory/jemalloc_spec.rb b/spec/lib/gitlab/memory/jemalloc_spec.rb index 414d6017534..8cce2278f8e 100644 --- a/spec/lib/gitlab/memory/jemalloc_spec.rb +++ b/spec/lib/gitlab/memory/jemalloc_spec.rb @@ -1,15 +1,14 @@ # frozen_string_literal: true require 'fast_spec_helper' -require 'tmpdir' +require 'tempfile' RSpec.describe Gitlab::Memory::Jemalloc do - let(:outdir) { Dir.mktmpdir } - let(:tmp_outdir) { Dir.mktmpdir } + let(:outfile) { Tempfile.new } after do - FileUtils.rm_f(outdir) - FileUtils.rm_f(tmp_outdir) + outfile.close + outfile.unlink end context 'when jemalloc is loaded' do @@ -31,12 +30,11 @@ RSpec.describe Gitlab::Memory::Jemalloc do describe '.dump_stats' do it 'writes stats JSON file' do - file_path = described_class.dump_stats(path: outdir, tmp_dir: tmp_outdir, format: format) + described_class.dump_stats(outfile, format: format) - file = Dir.entries(outdir).find { |e| e.match(/jemalloc_stats\.#{$$}\.\d+\.json$/) } - expect(file).not_to be_nil - expect(file_path).to eq(File.join(outdir, file)) - expect(File.read(file_path)).to eq(output) + outfile.rewind + + expect(outfile.read).to eq(output) end end end @@ -56,23 +54,12 @@ RSpec.describe Gitlab::Memory::Jemalloc do end describe '.dump_stats' do - shared_examples 'writes stats text file' do |filename_label, filename_pattern| - it do - described_class.dump_stats( - path: outdir, tmp_dir: tmp_outdir, format: format, filename_label: filename_label) - - file = Dir.entries(outdir).find { |e| e.match(filename_pattern) } - expect(file).not_to be_nil - expect(File.read(File.join(outdir, file))).to eq(output) - end - end + it 'writes stats text file' do + described_class.dump_stats(outfile, format: format) - context 'when custom filename label is passed' do - include_examples 'writes stats text file', 'puma_0', /jemalloc_stats\.#{$$}\.puma_0\.\d+\.txt$/ - end + outfile.rewind - context 'when custom filename label is not passed' do - include_examples 'writes stats text file', nil, /jemalloc_stats\.#{$$}\.\d+\.txt$/ + expect(outfile.read).to eq(output) end end end @@ -91,7 +78,7 @@ RSpec.describe Gitlab::Memory::Jemalloc do describe '.dump_stats' do it 'raises an error' do expect do - described_class.dump_stats(path: outdir, tmp_dir: tmp_outdir, format: format) + described_class.dump_stats(outfile, format: format) end.to raise_error(/format must be one of/) end end @@ -104,18 +91,18 @@ RSpec.describe Gitlab::Memory::Jemalloc do end describe '.stats' do - it 'returns nil' do - expect(described_class.stats).to be_nil + it 'returns empty string' do + expect(described_class.stats).to be_empty end end describe '.dump_stats' do it 'does nothing' do - stub_env('LD_PRELOAD', nil) + described_class.dump_stats(outfile) - described_class.dump_stats(path: outdir, tmp_dir: tmp_outdir) + outfile.rewind - expect(Dir.empty?(outdir)).to be(true) + expect(outfile.read).to be_empty end end end |