Skip to content

Commit e782cb9

Browse files
authored
[Fix] Specs failing to shutdown the mock server properly (#66)
* Move wait_for_server into a shared helper * Run mock server without forking the current process * Fix Rubocop
1 parent 69c8600 commit e782cb9

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

spec/integration/client_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'temporal/error/failure'
55
require 'temporal/error/workflow_failure'
66
require 'temporal/interceptor/client'
7+
require 'support/helpers/test_rpc'
78

89
class TestInterceptor < Temporal::Interceptor::Client
910
attr_reader :results
@@ -57,18 +58,9 @@ def terminate_workflow(input)
5758
let(:id) { SecureRandom.uuid }
5859
let(:workflow) { 'kitchen_sink' }
5960

60-
def wait_for_server(url)
61-
10.times do
62-
Temporal::Connection.new("http://#{url}")
63-
break
64-
rescue Temporal::Bridge::Error
65-
sleep(0.5) # delay before retrying
66-
end
67-
end
68-
6961
before(:all) do
7062
@server_pid = fork { exec("#{support_path}/go_server/main #{port} #{namespace}") }
71-
wait_for_server(url)
63+
Helpers::TestRPC.wait("http://#{url}", 10, 0.5)
7264

7365
@worker_pid = fork { exec("#{support_path}/go_worker/main #{url} #{namespace} #{task_queue}") }
7466
end

spec/integration/connection_spec.rb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
require 'grpc'
22
require 'temporal/connection'
3+
require 'support/helpers/test_rpc'
34
require 'support/mock_server'
45

56
describe Temporal::Connection do
67
mock_address = '0.0.0.0:4444'.freeze
78

8-
def wait_for_mock_server(address, attempts, interval = 1)
9-
request = Temporal::Api::WorkflowService::V1::GetSystemInfoRequest.new
10-
attempts.times do |i|
11-
connection = described_class.new("http://#{address}")
12-
connection.get_system_info(request)
13-
break
14-
rescue StandardError => e
15-
puts "Error connecting to a mock server: #{e}. Attempt #{i + 1} / #{attempts}"
16-
raise if i + 1 == attempts # re-raise upon exhausting attempts
17-
18-
sleep interval
19-
end
20-
end
21-
229
subject { described_class.new("http://#{mock_address}") }
2310

2411
# TODO: For some reason the Bridge doesn't play well with the server in the same
2512
# process throwing SegFaults in cases. Needs further investigation
2613
before(:all) do
27-
@pid = fork { MockServer.run(mock_address) }
28-
wait_for_mock_server(mock_address, 10)
14+
@pid = fork { exec('bundle exec ruby spec/support/mock_server.rb') }
15+
Helpers::TestRPC.wait("http://#{mock_address}", 10)
2916
end
3017
after(:all) { Process.kill('QUIT', @pid) }
3118

spec/support/helpers/test_rpc.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'temporal/connection'
2+
3+
module Helpers
4+
module TestRPC
5+
def self.wait(address, max_attempts, interval = 1)
6+
request = Temporal::Api::WorkflowService::V1::GetSystemInfoRequest.new
7+
max_attempts.times do |i|
8+
connection = Temporal::Connection.new(address)
9+
connection.get_system_info(request)
10+
break
11+
rescue StandardError => e
12+
puts "Error connecting to a server: #{e}. Attempt #{i + 1} / #{max_attempts}"
13+
raise if i + 1 == max_attempts # re-raise upon exhausting attempts
14+
15+
sleep interval
16+
end
17+
end
18+
end
19+
end

spec/support/mock_server.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
if __FILE__ == $PROGRAM_NAME
2+
$LOAD_PATH << File.expand_path('..', File.dirname(__FILE__))
3+
$LOAD_PATH << File.expand_path('../../lib/gen', File.dirname(__FILE__))
4+
end
5+
16
require 'grpc'
27
require 'support/grpc/temporal/api/workflowservice/v1/service_services_pb'
38

@@ -16,3 +21,7 @@ def self.run(address)
1621
end
1722
end
1823
end
24+
25+
if __FILE__ == $PROGRAM_NAME
26+
MockServer.run('0.0.0.0:4444')
27+
end

0 commit comments

Comments
 (0)