Skip to content

Commit ba40778

Browse files
authored
Fix flaky prometheus_updater_spec by cleaning up PROCESS_TYPE env var (#4868)
1 parent 01782fb commit ba40778

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

spec/unit/lib/cloud_controller/runner_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ module VCAP::CloudController
1111

1212
let(:argv) { [] }
1313

14+
# Runner#initialize calls set_process_type_env which sets
15+
# ENV['PROCESS_TYPE'] to 'main'. Normally PROCESS_TYPE is unset in the
16+
# test environment. This leak is harmless today ('main' maps to
17+
# API_PUMA_MAIN, the same default), but it is still pollution that should
18+
# be cleaned up.
19+
around do |example|
20+
original_process_type = ENV.fetch('PROCESS_TYPE', nil)
21+
example.run
22+
ensure
23+
if original_process_type.nil?
24+
ENV.delete('PROCESS_TYPE')
25+
else
26+
ENV['PROCESS_TYPE'] = original_process_type
27+
end
28+
end
29+
1430
before do
1531
allow(Steno).to receive(:init)
1632
allow(CloudController::DependencyLocator.instance).to receive(:routing_api_client).and_return(routing_api_client)

spec/unit/lib/cloud_controller/runners/puma_runner_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ module VCAP::CloudController
3333
PumaRunner.new(test_config, app, logger, periodic_updater, request_logs)
3434
end
3535

36+
# The before_worker_boot callback calls set_process_type_env which sets
37+
# ENV['PROCESS_TYPE'] to 'puma_worker'. Normally PROCESS_TYPE is unset in
38+
# the test environment, allowing ExecutionContext.from_process_type_env to
39+
# fall back to API_PUMA_MAIN via the CC_TEST=true check. This leak is
40+
# harmless today (API_PUMA_WORKER registers all metrics just like
41+
# API_PUMA_MAIN), but it is still pollution that should be cleaned up.
42+
around do |example|
43+
original_process_type = ENV.fetch('PROCESS_TYPE', nil)
44+
example.run
45+
ensure
46+
if original_process_type.nil?
47+
ENV.delete('PROCESS_TYPE')
48+
else
49+
ENV['PROCESS_TYPE'] = original_process_type
50+
end
51+
end
52+
3653
before do
3754
allow(logger).to receive(:info)
3855
allow(CloudController::DependencyLocator).to receive(:instance).and_return(dependency_locator)

spec/unit/lib/sequel/extensions/connection_metrics_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010
let(:prometheus_updater) { spy(VCAP::CloudController::Metrics::PrometheusUpdater) }
1111
let(:thread) { double('Thread') }
1212

13+
# Tests in this file call set_process_type_env which sets ENV['PROCESS_TYPE']
14+
# to 'cc-worker' or 'puma_worker'. Normally PROCESS_TYPE is unset in the
15+
# test environment, allowing ExecutionContext.from_process_type_env to fall
16+
# back to API_PUMA_MAIN via the CC_TEST=true check. The 'cc-worker' value
17+
# is the actual cause of flaky prometheus_updater_spec failures: it maps to
18+
# CC_WORKER, which only registers a subset of metrics, so registry.get()
19+
# returns nil for unregistered metrics. The 'puma_worker' value happens to
20+
# be harmless today (API_PUMA_WORKER registers all metrics), but it is
21+
# still pollution that should be cleaned up.
22+
around do |example|
23+
original_process_type = ENV.fetch('PROCESS_TYPE', nil)
24+
example.run
25+
ensure
26+
if original_process_type.nil?
27+
ENV.delete('PROCESS_TYPE')
28+
else
29+
ENV['PROCESS_TYPE'] = original_process_type
30+
end
31+
end
32+
1333
after do
1434
db.disconnect
1535
end

0 commit comments

Comments
 (0)