Skip to content

Commit

Permalink
Merge f5b5901 into 64e038f
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaum committed Apr 27, 2019
2 parents 64e038f + f5b5901 commit 7726854
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 54 deletions.
5 changes: 1 addition & 4 deletions lib/coverband/collectors/coverage.rb
Expand Up @@ -46,10 +46,7 @@ def report_coverage(force_report = false)
# when we are in runtime collection mode, which do not have a cache of previous
# coverage to remove the initial stdlib Coverage loading data
###
if ((original_previous_set.nil? && @store.type == Coverband::EAGER_TYPE) ||
(original_previous_set && @store.type != Coverband::EAGER_TYPE))
@store.save_report(files_with_line_usage)
end
@store.save_report(files_with_line_usage)
rescue StandardError => err
if @verbose
@logger&.error 'coverage failed to store'
Expand Down
1 change: 1 addition & 0 deletions lib/coverband/utils/file_path_helper.rb
Expand Up @@ -6,6 +6,7 @@
module Coverband
module Utils
module FilePathHelper
extend self
###
# Takes a full path and converts to a relative path
###
Expand Down
21 changes: 11 additions & 10 deletions lib/coverband/utils/railtie.rb
Expand Up @@ -2,20 +2,21 @@

Coverband.eager_loading_coverage!
module Coverband
class Railtie < Rails::Railtie
initializer 'coverband.configure' do |app|
app.middleware.use Coverband::Middleware
end

config.after_initialize do
module RailsEagerLoad
def eager_load!
Coverband.configuration.logger&.debug('Coverband: set to eager_loading')
Coverband.eager_loading_coverage!
super
ensure
Coverband.report_coverage(true)
Coverband.configuration.logger&.debug('Coverband: reported after_initialize')
Coverband.runtime_coverage!
end
end
Rails::Engine.prepend(RailsEagerLoad)

config.before_initialize do
Coverband.configuration.logger&.debug('Coverband: set to eager_loading')
Coverband.eager_loading_coverage!
class Railtie < Rails::Railtie
initializer 'coverband.configure' do |app|
app.middleware.use Coverband::Middleware
end

rake_tasks do
Expand Down
17 changes: 10 additions & 7 deletions test/integration/full_stack_test.rb
Expand Up @@ -6,7 +6,6 @@
class FullStackTest < Minitest::Test
REDIS_STORAGE_FORMAT_VERSION = Coverband::Adapters::RedisStore::REDIS_STORAGE_FORMAT_VERSION
TEST_RACK_APP = '../fake_app/basic_rack.rb'
RELATIVE_FILE = './fake_app/basic_rack.rb'

def setup
super
Expand All @@ -21,11 +20,10 @@ def setup
end
Coverband.configuration.store.clear!
Coverband.start
Coverband::Collectors::Coverage.instance.runtime!
@rack_file = File.expand_path(TEST_RACK_APP, File.dirname(__FILE__))
require @rack_file
# preload first coverage hit
Coverband::Collectors::Coverage.instance.eager_loading!
@rack_file = require_unique_file 'fake_app/basic_rack.rb'
Coverband::Collectors::Coverage.instance.report_coverage(true)
Coverband::Collectors::Coverage.instance.runtime!
end

test 'call app' do
Expand All @@ -35,13 +33,18 @@ def setup
assert_equal 'Hello Rack!', results.last
sleep(0.2)
expected = [nil, nil, 0, nil, 0, 0, 1, nil, nil]
assert_equal expected, Coverband.configuration.store.coverage[RELATIVE_FILE]['data']
assert_equal expected, Coverband.configuration.store.coverage[@rack_file]['data']

# additional calls increase count by 1
middleware.call(request)
sleep(0.2)
expected = [nil, nil, 0, nil, 0, 0, 2, nil, nil]
assert_equal expected, Coverband.configuration.store.coverage[RELATIVE_FILE]['data']
assert_equal expected, Coverband.configuration.store.coverage[@rack_file]['data']

# class coverage
Coverband.eager_loading_coverage!
Coverband.configuration.store.coverage[@rack_file]['data']
expected = [nil, nil, 1, nil, 1, 1, 0, nil, nil]
end

test 'call app with gem tracking' do
Expand Down
62 changes: 32 additions & 30 deletions test/integration/rails_full_stack_test.rb
Expand Up @@ -54,42 +54,44 @@ def teardown
# as we run it in single test mode via the benchmarks.
# Add new tests below this test
###
test 'memory usage' do
return unless ENV['COVERBAND_MEMORY_TEST']
# we don't want this to run during our standard test suite
# as the below profiler changes the runtime
# and shold only be included for isolated processes
begin
require 'memory_profiler'
if ENV['COVERBAND_MEMORY_TEST']
test 'memory usage' do
return unless ENV['COVERBAND_MEMORY_TEST']
# we don't want this to run during our standard test suite
# as the below profiler changes the runtime
# and shold only be included for isolated processes
begin
require 'memory_profiler'

# warmup
3.times do
visit '/dummy/show'
assert_content('I am no dummy')
Coverband.report_coverage(true)
end

previous_out = $stdout
capture = StringIO.new
$stdout = capture

MemoryProfiler.report do
15.times do
# warmup
3.times do
visit '/dummy/show'
assert_content('I am no dummy')
Coverband.report_coverage(true)
# this is expected to retain memory across requests
# clear it to remove the false positive from test
Coverband::Collectors::Coverage.instance.send(:add_previous_results, nil)
end
end.pretty_print
data = $stdout.string
$stdout = previous_out
if data.match(/retained objects by gem(.*)retained objects by file/m)[0]&.match(/coverband/)
raise 'leaking memory!!!'

previous_out = $stdout
capture = StringIO.new
$stdout = capture

MemoryProfiler.report do
15.times do
visit '/dummy/show'
assert_content('I am no dummy')
Coverband.report_coverage(true)
# this is expected to retain memory across requests
# clear it to remove the false positive from test
Coverband::Collectors::Coverage.instance.send(:add_previous_results, nil)
end
end.pretty_print
data = $stdout.string
$stdout = previous_out
if data.match(/retained objects by gem(.*)retained objects by file/m)[0]&.match(/coverband/)
raise 'leaking memory!!!'
end
ensure
$stdout = previous_out
end
ensure
$stdout = previous_out
end
end
end
7 changes: 4 additions & 3 deletions test/unique_files.rb
Expand Up @@ -6,12 +6,13 @@
UNIQUE_FILES_DIR = "./test/unique_files"

def require_unique_file(file = 'dog.rb')
dir = "#{UNIQUE_FILES_DIR}/#{SecureRandom.uuid}"
FileUtils.mkdir_p(dir)
uuid = SecureRandom.uuid
dir = "#{UNIQUE_FILES_DIR}/#{uuid}"
temp_file = "#{dir}/#{file}"
FileUtils.mkdir_p(Pathname.new(temp_file).dirname.to_s)
File.open(temp_file, 'w'){ |w| w.write(File.read("./test/#{file}")) }
require temp_file
temp_file
Coverband::Utils::FilePathHelper.full_path_to_relative(File.expand_path(temp_file))
end

def remove_unique_files
Expand Down

0 comments on commit 7726854

Please sign in to comment.