Skip to content

Commit 9ce03e9

Browse files
committed
Fix flaky MetricsWebserver test by mocking TCP/Unix listener binding
The test was failing with 'Address already in use' because it was calling add_tcp_listener() for real, which tried to bind to port 9395. If the port was already in use (from a previous test or orphaned process), the test would fail. Solution: Mock add_tcp_listener and add_unix_listener in all test contexts to prevent actual port binding. The mocks still verify the correct methods are called with correct arguments, but don't perform real network operations. This eliminates the EADDRINUSE flake without changing test coverage.
1 parent d83d69e commit 9ce03e9

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

spec/unit/lib/cloud_controller/metrics_webserver_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module VCAP::CloudController
1212
describe '#start' do
1313
it 'configures and starts a Puma server' do
1414
allow(Puma::Server).to receive(:new).and_call_original
15+
allow_any_instance_of(Puma::Server).to receive(:add_tcp_listener)
1516
expect_any_instance_of(Puma::Server).to receive(:run)
1617

1718
metrics_webserver.start(config)
@@ -24,6 +25,7 @@ module VCAP::CloudController
2425

2526
it 'uses a TCP listener' do
2627
expect_any_instance_of(Puma::Server).to receive(:add_tcp_listener).with('127.0.0.1', 9395)
28+
allow_any_instance_of(Puma::Server).to receive(:run)
2729

2830
metrics_webserver.start(config)
2931
end
@@ -36,6 +38,7 @@ module VCAP::CloudController
3638

3739
it 'uses a Unix socket listener' do
3840
expect_any_instance_of(Puma::Server).to receive(:add_unix_listener).with('/tmp/metrics.sock')
41+
allow_any_instance_of(Puma::Server).to receive(:run)
3942

4043
metrics_webserver.start(config)
4144
end

0 commit comments

Comments
 (0)