Class: Temporalio::Testing::WorkflowEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/testing/workflow_environment.rb

Overview

Test environment with a Temporal server for running workflows and more.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WorkflowEnvironment

Create workflow environment to an existing server with the given client.

Parameters:

  • client (Client)

    Client to existing server.



210
211
212
# File 'lib/temporalio/testing/workflow_environment.rb', line 210

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientClient (readonly)

Returns Client for the server.

Returns:

  • (Client)

    Client for the server.



20
21
22
# File 'lib/temporalio/testing/workflow_environment.rb', line 20

def client
  @client
end

Class Method Details

.start_local(namespace: 'default', data_converter: Converters::DataConverter.default, interceptors: [], logger: Logger.new($stdout, level: Logger::WARN), default_workflow_query_reject_condition: nil, ip: '127.0.0.1', port: nil, ui: false, runtime: Runtime.default, dev_server_existing_path: nil, dev_server_database_filename: nil, dev_server_log_format: 'pretty', dev_server_log_level: 'warn', dev_server_download_version: 'default', dev_server_download_dest_dir: nil, dev_server_extra_args: []) {|environment| ... } ⇒ WorkflowEnvironment, Object

Start a local dev server. This is a full Temporal dev server from the CLI that by default downloaded to tmp if not already present. The dev server is run as a child process. All options that start with dev_server_ are for this specific implementation and therefore are not stable and may be changed as the underlying implementation changes.

If a block is given it is passed the environment and the environment is shut down after. If a block is not given, the environment is returned and #shutdown needs to be called manually.

Parameters:

  • namespace (String) (defaults to: 'default')

    Namespace for the server.

  • data_converter (Converters::DataConverter) (defaults to: Converters::DataConverter.default)

    Data converter for the client.

  • interceptors (Array<Client::Interceptor>) (defaults to: [])

    Interceptors for the client.

  • logger (Logger) (defaults to: Logger.new($stdout, level: Logger::WARN))

    Logger for the client.

  • default_workflow_query_reject_condition (WorkflowQueryRejectCondition, nil) (defaults to: nil)

    Default rejection condition for the client.

  • ip (String) (defaults to: '127.0.0.1')

    IP to bind to.

  • port (Integer, nil) (defaults to: nil)

    Port to bind on, or nil for random.

  • ui (Boolean) (defaults to: false)

    If true, also starts the UI.

  • runtime (Runtime) (defaults to: Runtime.default)

    Runtime for the server and client.

  • dev_server_existing_path (String, nil) (defaults to: nil)

    Existing CLI path to use instead of downloading and caching to tmp.

  • dev_server_database_filename (String, nil) (defaults to: nil)

    Persistent SQLite filename to use across local server runs. Default of nil means in-memory only.

  • dev_server_log_format (String) (defaults to: 'pretty')

    Log format for CLI dev server.

  • dev_server_log_level (String) (defaults to: 'warn')

    Log level for CLI dev server.

  • dev_server_download_version (String) (defaults to: 'default')

    Version of dev server to download and cache.

  • dev_server_download_dest_dir (String, nil) (defaults to: nil)

    Where to download. Defaults to tmp.

  • dev_server_extra_args (Array<String>) (defaults to: [])

    Any extra arguments for the CLI dev server.

Yields:

  • (environment)

    If a block is given, it is called with the environment and upon complete the environment is shutdown.

Yield Parameters:

Returns:

  • (WorkflowEnvironment, Object)

    Started local server environment with client if there was no block given, or block result if block was given.



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
# File 'lib/temporalio/testing/workflow_environment.rb', line 56

def self.start_local(
  namespace: 'default',
  data_converter: Converters::DataConverter.default,
  interceptors: [],
  logger: Logger.new($stdout, level: Logger::WARN),
  default_workflow_query_reject_condition: nil,
  ip: '127.0.0.1',
  port: nil,
  ui: false, # rubocop:disable Naming/MethodParameterName
  runtime: Runtime.default,
  dev_server_existing_path: nil,
  dev_server_database_filename: nil,
  dev_server_log_format: 'pretty',
  dev_server_log_level: 'warn',
  dev_server_download_version: 'default',
  dev_server_download_dest_dir: nil,
  dev_server_extra_args: [],
  &
)
  server_options = Internal::Bridge::Testing::EphemeralServer::StartDevServerOptions.new(
    existing_path: dev_server_existing_path,
    sdk_name: 'sdk-ruby',
    sdk_version: VERSION,
    download_version: dev_server_download_version,
    download_dest_dir: dev_server_download_dest_dir,
    namespace:,
    ip:,
    port:,
    database_filename: dev_server_database_filename,
    ui:,
    log_format: dev_server_log_format,
    log_level: dev_server_log_level,
    extra_args: dev_server_extra_args
  )
  _with_core_server(
    core_server: Internal::Bridge::Testing::EphemeralServer.start_dev_server(
      runtime._core_runtime, server_options
    ),
    namespace:,
    data_converter:,
    interceptors:,
    logger:,
    default_workflow_query_reject_condition:,
    runtime:,
    supports_time_skipping: false,
    & # steep:ignore
  )
end

.start_time_skipping(data_converter: Converters::DataConverter.default, interceptors: [], logger: Logger.new($stdout, level: Logger::WARN), default_workflow_query_reject_condition: nil, port: nil, runtime: Runtime.default, test_server_existing_path: nil, test_server_download_version: 'default', test_server_download_dest_dir: nil, test_server_extra_args: []) {|environment| ... } ⇒ WorkflowEnvironment, Object

Start a time-skipping test server. This server can skip time but may not have all of the Temporal features of the start_local form. By default, the server is downloaded to tmp if not already present. The test server is run as a child process. All options that start with test_server_ are for this specific implementation and therefore are not stable and may be changed as the underlying implementation changes.

If a block is given it is passed the environment and the environment is shut down after. If a block is not given, the environment is returned and #shutdown needs to be called manually.

Parameters:

  • data_converter (Converters::DataConverter) (defaults to: Converters::DataConverter.default)

    Data converter for the client.

  • interceptors (Array<Client::Interceptor>) (defaults to: [])

    Interceptors for the client.

  • logger (Logger) (defaults to: Logger.new($stdout, level: Logger::WARN))

    Logger for the client.

  • default_workflow_query_reject_condition (WorkflowQueryRejectCondition, nil) (defaults to: nil)

    Default rejection condition for the client.

  • port (Integer, nil) (defaults to: nil)

    Port to bind on, or nil for random.

  • runtime (Runtime) (defaults to: Runtime.default)

    Runtime for the server and client.

  • test_server_existing_path (String, nil) (defaults to: nil)

    Existing CLI path to use instead of downloading and caching to tmp.

  • test_server_download_version (String) (defaults to: 'default')

    Version of test server to download and cache.

  • test_server_download_dest_dir (String, nil) (defaults to: nil)

    Where to download. Defaults to tmp.

  • test_server_extra_args (Array<String>) (defaults to: [])

    Any extra arguments for the test server.

Yields:

  • (environment)

    If a block is given, it is called with the environment and upon complete the environment is shutdown.

Yield Parameters:

Returns:

  • (WorkflowEnvironment, Object)

    Started local server environment with client if there was no block given, or block result if block was given.



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
# File 'lib/temporalio/testing/workflow_environment.rb', line 132

def self.start_time_skipping(
  data_converter: Converters::DataConverter.default,
  interceptors: [],
  logger: Logger.new($stdout, level: Logger::WARN),
  default_workflow_query_reject_condition: nil,
  port: nil,
  runtime: Runtime.default,
  test_server_existing_path: nil,
  test_server_download_version: 'default',
  test_server_download_dest_dir: nil,
  test_server_extra_args: [],
  &
)
  server_options = Internal::Bridge::Testing::EphemeralServer::StartTestServerOptions.new(
    existing_path: test_server_existing_path,
    sdk_name: 'sdk-ruby',
    sdk_version: VERSION,
    download_version: test_server_download_version,
    download_dest_dir: test_server_download_dest_dir,
    port:,
    extra_args: test_server_extra_args
  )
  _with_core_server(
    core_server: Internal::Bridge::Testing::EphemeralServer.start_test_server(
      runtime._core_runtime, server_options
    ),
    namespace: 'default',
    data_converter:,
    interceptors:,
    logger:,
    default_workflow_query_reject_condition:,
    runtime:,
    supports_time_skipping: true,
    & # steep:ignore
  )
end

Instance Method Details

#auto_time_skipping_disabled { ... } ⇒ Object

Run a block with automatic time skipping disabled. This just runs the block for environments that don’t support time skipping.

Yields:

  • Block to run.

Returns:

  • (Object)

    Result of the block.



249
250
251
252
253
# File 'lib/temporalio/testing/workflow_environment.rb', line 249

def auto_time_skipping_disabled(&)
  raise 'Block required' unless block_given?

  yield
end

#current_timeTime

Current time of the environment.

If this server supports time skipping, this will be the current time as known to the environment. If it does not, this is a standard Time.now.

Returns:

  • (Time)

    Current time.



240
241
242
# File 'lib/temporalio/testing/workflow_environment.rb', line 240

def current_time
  Time.now
end

#shutdownObject

Shutdown this workflow environment.



215
216
217
# File 'lib/temporalio/testing/workflow_environment.rb', line 215

def shutdown
  # Do nothing by default
end

#sleep(duration) ⇒ Object

Advanced time.

If this server supports time skipping, this will immediately advance time and return. If it does not, this is a standard sleep.

Parameters:

  • duration (Float)

    Duration seconds.



230
231
232
# File 'lib/temporalio/testing/workflow_environment.rb', line 230

def sleep(duration)
  Kernel.sleep(duration)
end

#supports_time_skipping?Boolean

Returns Whether this environment supports time skipping.

Returns:

  • (Boolean)

    Whether this environment supports time skipping.



220
221
222
# File 'lib/temporalio/testing/workflow_environment.rb', line 220

def supports_time_skipping?
  false
end