Skip to content

Commit

Permalink
Register at_exit for all coverband processes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaum committed Jan 29, 2019
1 parent 6e7afcb commit 3c1be0f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/coverband.rb
Expand Up @@ -5,6 +5,7 @@
require 'redis'

require 'coverband/version'
require 'coverband/at_exit'
require 'coverband/configuration'
require 'coverband/adapters/base'
require 'coverband/adapters/redis_store'
Expand Down Expand Up @@ -52,6 +53,7 @@ def self.configuration

def self.start
Coverband::Collectors::Coverage.instance
AtExit.register
Background.start if configuration.background_reporting_enabled && !RackServerCheck.running?
end

Expand Down
20 changes: 20 additions & 0 deletions lib/coverband/at_exit.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Coverband
class AtExit
@semaphore = Mutex.new

def self.register
return if @at_exit_registered
@semaphore.synchronize do
return if @at_exit_registered
@at_exit_registered = true
at_exit do
::Coverband::Background.stop
Coverband::Collectors::Coverage.instance.report_coverage(true)
Coverband.configuration.logger&.debug('Coverband: Reported coverage before exit')
end
end
end
end
end
5 changes: 0 additions & 5 deletions lib/coverband/integrations/background.rb
Expand Up @@ -29,11 +29,6 @@ def self.start
end
end
end
at_exit do
stop
Coverband::Collectors::Coverage.instance.report_coverage(true)
logger&.debug('Coverband: Reported coverage before exit')
end
end
end
end
5 changes: 3 additions & 2 deletions lib/coverband/integrations/middleware.rb
Expand Up @@ -9,10 +9,11 @@ def initialize(app)
def call(env)
@app.call(env)
ensure
AtExit.register
if Coverband.configuration.background_reporting_enabled
Coverband::Background.start
Background.start
else
Coverband::Collectors::Coverage.instance.report_coverage
Collectors::Coverage.instance.report_coverage
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/unit/at_exit_test.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require File.expand_path('../test_helper', File.dirname(__FILE__))

class AtExitTest < Minitest::Test
test 'only registers once' do
Coverband::AtExit.instance_eval { @at_exit_registered = nil }
Coverband::AtExit.expects(:at_exit).yields.once.returns(true)
2.times { Coverband::AtExit.register }
end
end


1 change: 0 additions & 1 deletion test/unit/background_test.rb
Expand Up @@ -22,7 +22,6 @@ def test_start
Thread.expects(:new).yields.returns(ThreadDouble.new)
Coverband::Background.expects(:loop).yields
Coverband::Background.expects(:sleep).with(30)
Coverband::Background.expects(:at_exit).returns(false)
Coverband::Collectors::Coverage.instance.expects(:report_coverage).once
2.times { Coverband::Background.start }
end
Expand Down

0 comments on commit 3c1be0f

Please sign in to comment.