-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.rb
More file actions
202 lines (174 loc) · 6.19 KB
/
testing.rb
File metadata and controls
202 lines (174 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# typed: false
# frozen_string_literal: true
require 'restate'
require 'socket'
require 'net/http'
require 'json'
require 'uri'
require 'testcontainers'
module Restate
# Test harness for running Restate services with a real Restate server.
# Opt-in via `require "restate/testing"` (not loaded by default).
#
# Requires Docker and the testcontainers-core gem.
#
# Block-based (recommended):
# Restate::Testing.start(Greeter, Counter) do |env|
# env.ingress_url # => "http://localhost:32771"
# env.admin_url # => "http://localhost:32772"
# end
#
# Manual lifecycle (for RSpec before/after hooks):
# harness = Restate::Testing::RestateTestHarness.new(Greeter, Counter)
# harness.start
# harness.ingress_url
# harness.stop
module Testing
# Starts a Restate test environment with the given services.
# When a block is given, stops automatically on block exit.
# Without a block, returns the harness for manual lifecycle management.
def self.start(*services, **options)
harness = RestateTestHarness.new(*services, **options)
harness.start
if block_given?
begin
yield harness
ensure
harness.stop
end
else
harness
end
end
# Manages the lifecycle of an SDK server and a Restate container for testing.
class RestateTestHarness
attr_reader :ingress_url, :admin_url
# @param services [Array<Class>] Service classes to register.
# @param restate_image [String] Docker image for Restate server.
# @param always_replay [Boolean] Force replay on every suspension point.
# @param disable_retries [Boolean] Disable Restate retry policy.
# @yield [Endpoint] Optional block to configure the endpoint (e.g. add middleware).
def initialize(*services,
restate_image: 'docker.io/restatedev/restate:latest',
always_replay: false,
disable_retries: false,
&configure)
@services = services
@restate_image = restate_image
@always_replay = always_replay
@disable_retries = disable_retries
@configure = configure
@server_thread = nil
@container = nil
@port = nil
@ingress_url = nil
@admin_url = nil
end
def start
@port = find_free_port
endpoint = Restate.endpoint(*@services)
@configure&.call(endpoint)
rack_app = endpoint.app
start_sdk_server(rack_app)
wait_for_tcp(@port)
start_restate_container
register_sdk
self
rescue StandardError => e
stop
raise e
end
def stop
stop_restate_container
stop_sdk_server
end
private
def find_free_port
server = TCPServer.new('0.0.0.0', 0)
port = server.addr[1]
server.close
port
end
def start_sdk_server(rack_app)
require 'async'
require 'async/http/endpoint'
require 'falcon/server'
port = @port
ready = Queue.new
@server_thread = Thread.new do
Async do
endpoint = Async::HTTP::Endpoint.parse("http://0.0.0.0:#{port}")
middleware = Falcon::Server.middleware(rack_app, cache: false)
server = Falcon::Server.new(middleware, endpoint)
ready.push(true)
server.run
end
end
ready.pop
end
def wait_for_tcp(port, timeout: 10)
deadline = Time.now + timeout
loop do
TCPSocket.new('127.0.0.1', port).close
return
rescue Errno::ECONNREFUSED, Errno::ECONNRESET
raise "SDK server failed to start on port #{port} within #{timeout}s" if Time.now > deadline
sleep 0.1
end
end
def start_restate_container
env = {
'RESTATE_LOG_FILTER' => 'restate=info',
'RESTATE_BOOTSTRAP_NUM_PARTITIONS' => '1',
'RESTATE_DEFAULT_NUM_PARTITIONS' => '1',
'RESTATE_SHUTDOWN_TIMEOUT' => '10s',
'RESTATE_ROCKSDB_TOTAL_MEMORY_SIZE' => '32 MB',
'RESTATE_WORKER__INVOKER__IN_MEMORY_QUEUE_LENGTH_LIMIT' => '64',
'RESTATE_WORKER__INVOKER__INACTIVITY_TIMEOUT' => @always_replay ? '0s' : '10m',
'RESTATE_WORKER__INVOKER__ABORT_TIMEOUT' => '10m'
}
env['RESTATE_WORKER__INVOKER__RETRY_POLICY__TYPE'] = 'none' if @disable_retries
@container = RestateContainer.new(@restate_image)
@container.with_exposed_ports(8080, 9070)
@container.with_env(env)
@container.start
@container.wait_for_http(path: '/restate/health', container_port: 8080, status: 200, timeout: 30)
@container.wait_for_http(path: '/health', container_port: 9070, status: 200, timeout: 30)
@ingress_url = "http://#{@container.host}:#{@container.mapped_port(8080)}"
@admin_url = "http://#{@container.host}:#{@container.mapped_port(9070)}"
end
def register_sdk
uri = URI("#{@admin_url}/deployments")
sdk_url = "http://host.docker.internal:#{@port}"
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = JSON.generate({ uri: sdk_url })
response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) }
return if response.code.start_with?('2')
raise "Failed to register SDK at #{sdk_url}: #{response.code} #{response.body}"
end
def stop_restate_container
return unless @container
@container.stop!
@container.remove(force: true)
rescue StandardError
# Ignore cleanup errors
end
def stop_sdk_server
@server_thread&.kill
@server_thread&.join(5)
end
end
# Testcontainers::DockerContainer subclass that adds ExtraHosts support
# so the Restate container can reach the host-bound SDK server.
class RestateContainer < Testcontainers::DockerContainer
private
def _container_create_options
options = super
options['HostConfig'] ||= {}
options['HostConfig']['ExtraHosts'] = ['host.docker.internal:host-gateway']
options
end
end
end
end