Skip to content

Commit 66d68ea

Browse files
authored
Search attributes and UI port on dev server (#220)
Fixes #205 Fixes #206
1 parent 319fe2e commit 66d68ea

File tree

8 files changed

+98
-2
lines changed

8 files changed

+98
-2
lines changed

temporalio/ext/src/testing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ impl EphemeralServer {
6161
.ip(options.member::<String>(id!("ip"))?)
6262
.port(options.member::<Option<u16>>(id!("port"))?)
6363
.db_filename(options.member::<Option<String>>(id!("database_filename"))?)
64-
.ui(options.member(id!("namespace"))?)
64+
.ui(options.member(id!("ui"))?)
65+
.ui_port(options.member::<Option<u16>>(id!("ui_port"))?)
6566
.log((
6667
options.member::<String>(id!("log_format"))?,
6768
options.member::<String>(id!("log_level"))?,

temporalio/lib/temporalio/internal/bridge/testing.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class EphemeralServer
1818
:port, # Optional
1919
:database_filename, # Optional
2020
:ui,
21+
:ui_port, # Optional, should be nil if ui is false
2122
:log_format,
2223
:log_level,
2324
:extra_args,

temporalio/lib/temporalio/search_attributes.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ def length
251251
@raw_hash.length
252252
end
253253

254+
# Check equality.
255+
#
256+
# @param other [SearchAttributes] To compare.
257+
# @return [Boolean] Whether equal.
258+
def ==(other)
259+
other.is_a?(SearchAttributes) && @raw_hash == other._raw_hash
260+
end
261+
254262
alias size length
255263

256264
# Return a new search attributes collection with updates applied.
@@ -280,6 +288,11 @@ def update!(*updates)
280288
end
281289
end
282290

291+
# @!visibility private
292+
def _raw_hash
293+
@raw_hash
294+
end
295+
283296
# @!visibility private
284297
def _to_proto
285298
Api::Common::V1::SearchAttributes.new(indexed_fields: _to_proto_hash)

temporalio/lib/temporalio/testing/workflow_environment.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require 'temporalio/internal/bridge/testing'
1111
require 'temporalio/internal/proto_utils'
1212
require 'temporalio/runtime'
13+
require 'temporalio/search_attributes'
1314
require 'temporalio/version'
1415

1516
module Temporalio
@@ -34,8 +35,10 @@ class WorkflowEnvironment
3435
# @param default_workflow_query_reject_condition [WorkflowQueryRejectCondition, nil] Default rejection condition
3536
# for the client.
3637
# @param ip [String] IP to bind to.
37-
# @param port [Integer, nil] Port to bind on, or +nil+ for random.
38+
# @param port [Integer, nil] Port to bind on, or `nil` for random.
3839
# @param ui [Boolean] If +true+, also starts the UI.
40+
# @param ui_port [Integer, nil] Port to bind on if `ui` is true, or `nil` for random.
41+
# @param search_attributes [Array<SearchAttributes::Key>] Search attributes to make available on start.
3942
# @param runtime [Runtime] Runtime for the server and client.
4043
# @param dev_server_existing_path [String, nil] Existing CLI path to use instead of downloading and caching to
4144
# tmp.
@@ -62,6 +65,8 @@ def self.start_local(
6265
ip: '127.0.0.1',
6366
port: nil,
6467
ui: false, # rubocop:disable Naming/MethodParameterName
68+
ui_port: nil,
69+
search_attributes: [],
6570
runtime: Runtime.default,
6671
dev_server_existing_path: nil,
6772
dev_server_database_filename: nil,
@@ -72,6 +77,15 @@ def self.start_local(
7277
dev_server_extra_args: [],
7378
&
7479
)
80+
# Add search attribute args
81+
unless search_attributes.empty?
82+
dev_server_extra_args += search_attributes.flat_map do |key|
83+
raise 'Search attribute must be Key' unless key.is_a?(SearchAttributes::Key)
84+
85+
['--search-attribute', "#{key.name}=#{SearchAttributes::IndexedValueType::PROTO_NAMES[key.type]}"]
86+
end
87+
end
88+
7589
server_options = Internal::Bridge::Testing::EphemeralServer::StartDevServerOptions.new(
7690
existing_path: dev_server_existing_path,
7791
sdk_name: 'sdk-ruby',
@@ -83,6 +97,7 @@ def self.start_local(
8397
port:,
8498
database_filename: dev_server_database_filename,
8599
ui:,
100+
ui_port: ui ? ui_port : nil,
86101
log_format: dev_server_log_format,
87102
log_level: dev_server_log_level,
88103
extra_args: dev_server_extra_args

temporalio/sig/temporalio/internal/bridge/testing.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Temporalio
1414
attr_accessor port: Integer?
1515
attr_accessor database_filename: String?
1616
attr_accessor ui: bool
17+
attr_accessor ui_port: Integer?
1718
attr_accessor log_format: String
1819
attr_accessor log_level: String
1920
attr_accessor extra_args: Array[String]
@@ -29,6 +30,7 @@ module Temporalio
2930
port: Integer?,
3031
database_filename: String?,
3132
ui: bool,
33+
ui_port: Integer?,
3234
log_format: String,
3335
log_level: String,
3436
extra_args: Array[String]

temporalio/sig/temporalio/search_attributes.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ module Temporalio
5656

5757
def update!: (*Update updates) -> void
5858

59+
def _raw_hash: -> Hash[Key, Object]
60+
5961
def _to_proto: -> untyped
6062

6163
def _to_proto_hash: -> Hash[String, untyped]

temporalio/sig/temporalio/testing/workflow_environment.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module Temporalio
1212
?ip: String,
1313
?port: Integer?,
1414
?ui: bool,
15+
?ui_port: Integer?,
16+
?search_attributes: Array[SearchAttributes::Key],
1517
?runtime: Runtime,
1618
?dev_server_existing_path: String?,
1719
?dev_server_database_filename: String?,
@@ -30,6 +32,8 @@ module Temporalio
3032
?ip: String,
3133
?port: Integer?,
3234
?ui: bool,
35+
?ui_port: Integer?,
36+
?search_attributes: Array[SearchAttributes::Key],
3337
?runtime: Runtime,
3438
?dev_server_existing_path: String?,
3539
?dev_server_database_filename: String?,

temporalio/test/testing/workflow_environment_test.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,63 @@ def test_time_skipping_heartbeat_timeout
131131
end
132132
end
133133
end
134+
135+
def test_start_local_search_attributes
136+
pre = 'ruby-temporal-test-'
137+
attr_boolean = Temporalio::SearchAttributes::Key.new(
138+
"#{pre}boolean", Temporalio::SearchAttributes::IndexedValueType::BOOLEAN
139+
)
140+
attr_float = Temporalio::SearchAttributes::Key.new(
141+
"#{pre}float", Temporalio::SearchAttributes::IndexedValueType::FLOAT
142+
)
143+
attr_integer = Temporalio::SearchAttributes::Key.new(
144+
"#{pre}integer", Temporalio::SearchAttributes::IndexedValueType::INTEGER
145+
)
146+
attr_keyword = Temporalio::SearchAttributes::Key.new(
147+
"#{pre}keyword", Temporalio::SearchAttributes::IndexedValueType::KEYWORD
148+
)
149+
attr_keyword_list = Temporalio::SearchAttributes::Key.new(
150+
"#{pre}keyword-list", Temporalio::SearchAttributes::IndexedValueType::KEYWORD_LIST
151+
)
152+
attr_text = Temporalio::SearchAttributes::Key.new(
153+
"#{pre}text", Temporalio::SearchAttributes::IndexedValueType::TEXT
154+
)
155+
attr_time = Temporalio::SearchAttributes::Key.new(
156+
"#{pre}time", Temporalio::SearchAttributes::IndexedValueType::TIME
157+
)
158+
attrs = Temporalio::SearchAttributes.new(
159+
{
160+
attr_boolean => true,
161+
attr_float => 1.23,
162+
attr_integer => 456,
163+
attr_keyword => 'some keyword',
164+
attr_keyword_list => ['some keyword list 1', 'some keyword list 2'],
165+
attr_text => 'some text',
166+
attr_time => Time.at(Time.now.to_i)
167+
}
168+
)
169+
170+
# Confirm when used in env without SAs it fails
171+
Temporalio::Testing::WorkflowEnvironment.start_local do |env|
172+
err = assert_raises(Temporalio::Error::RPCError) do
173+
env.client.start_workflow(
174+
:some_workflow,
175+
id: "wf-#{SecureRandom.uuid}", task_queue: "tq-#{SecureRandom.uuid}",
176+
search_attributes: attrs
177+
)
178+
end
179+
assert_includes err.message, 'no mapping defined'
180+
end
181+
182+
# Confirm when used in env with SAs it succeeds
183+
Temporalio::Testing::WorkflowEnvironment.start_local(search_attributes: attrs.to_h.keys) do |env|
184+
handle = env.client.start_workflow(
185+
:some_workflow,
186+
id: "wf-#{SecureRandom.uuid}", task_queue: "tq-#{SecureRandom.uuid}",
187+
search_attributes: attrs
188+
)
189+
assert_equal attrs, handle.describe.search_attributes
190+
end
191+
end
134192
end
135193
end

0 commit comments

Comments
 (0)