Skip to content

Commit f35ba25

Browse files
committed
added a wait for port to be avaiable to increase reliability of the specs
1 parent e4d305c commit f35ba25

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

spec/fixtures/fast-mcp-ruby/lib/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def content
124124
log_writer = is_silent ? Puma::LogWriter.new(NullWriter.new, NullWriter.new) : Puma::LogWriter.stdio
125125

126126
config = Puma::Configuration.new(log_writer: log_writer) do |user_config|
127-
user_config.bind "tcp://localhost:3006"
127+
user_config.bind "tcp://127.0.0.1:3006"
128128
user_config.app app
129129
end
130130

spec/support/test_server_manager.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
require "socket"
4+
require "timeout"
5+
36
class TestServerManager
47
SERVERS = {
58
stdio: {
@@ -10,18 +13,21 @@ class TestServerManager
1013
http: {
1114
command: "bun",
1215
args: ["spec/fixtures/typescript-mcp/index.ts", "--", "--silent"],
13-
pid_accessor: :http_server_pid
16+
pid_accessor: :http_server_pid,
17+
port: 3005
1418
},
1519
pagination: {
1620
command: "bun",
1721
args: ["spec/fixtures/pagination-server/index.ts", "--", "--silent"],
18-
pid_accessor: :pagination_server_pid
22+
pid_accessor: :pagination_server_pid,
23+
port: 3007
1924
},
2025
sse: {
2126
command: "ruby",
2227
args: ["lib/app.rb", "--silent"],
2328
chdir: "spec/fixtures/fast-mcp-ruby",
24-
pid_accessor: :sse_server_pid
29+
pid_accessor: :sse_server_pid,
30+
port: 3006
2531
}
2632
}.freeze
2733

@@ -35,9 +41,6 @@ def start_server
3541
start_server_type(:stdio)
3642
start_server_type(:http)
3743
start_server_type(:pagination)
38-
39-
# Give servers time to start
40-
sleep 1.0
4144
rescue StandardError => e
4245
puts "Failed to start test server: #{e.message}"
4346
stop_server
@@ -97,6 +100,9 @@ def start_server_type(server_type)
97100
pid = spawn(config[:command], *config[:args], **spawn_options)
98101
Process.detach(pid)
99102
send("#{pid_accessor}=", pid)
103+
104+
# Wait for the server to start, ensure they are ready to start
105+
wait_for_port(config[:port]) if config[:port]
100106
end
101107

102108
def stop_server_type(server_type)
@@ -118,6 +124,17 @@ def stop_server_type(server_type)
118124
end
119125
end
120126

127+
def wait_for_port(port, host = "127.0.0.1", timeout = 15)
128+
Timeout.timeout(timeout) do
129+
loop do
130+
Socket.tcp(host, port, connect_timeout: 1).close
131+
break
132+
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
133+
sleep 0.1
134+
end
135+
end
136+
end
137+
121138
def process_exists?(pid)
122139
Process.kill(0, pid)
123140
true

0 commit comments

Comments
 (0)