Class: Temporalio::Testing::WorkflowEnvironment
- Inherits:
-
Object
- Object
- Temporalio::Testing::WorkflowEnvironment
- Defined in:
- lib/temporalio/testing/workflow_environment.rb
Overview
Unless using the above mentioned methods with an explicit block, you will need to call
Workflow environment for testing workflows.
Most developers will want to use the start_time_skipping_environment to start a test server process that automatically skips time as needed. Alternatively, start_local_environment may be used for a full, local Temporal server with more features.
#shutdown after you’re finished with this environment.
Instance Attribute Summary collapse
-
#connection ⇒ Temporalio::Connection
readonly
A connection to this environment.
-
#namespace ⇒ String
readonly
A namespace for this environment.
Instance Method Summary collapse
-
#client ⇒ Temporalio::Client
A default client to be used with this environment.
-
#current_time ⇒ Time
Get the current time known to this environment.
-
#initialize(server, connection, namespace) ⇒ WorkflowEnvironment
constructor
private
A new instance of WorkflowEnvironment.
-
#shutdown ⇒ Object
Shut down this environment.
-
#sleep(duration) ⇒ Object
Sleep in this environment.
-
#supports_time_skipping? ⇒ Boolean
Whether this environment supports time skipping.
-
#with_time_skipping { ... } ⇒ any
Unlock time skipping.
Constructor Details
#initialize(server, connection, namespace) ⇒ WorkflowEnvironment
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of WorkflowEnvironment.
24 25 26 27 28 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 24 def initialize(server, connection, namespace) @server = server @connection = connection @namespace = namespace end |
Instance Attribute Details
#connection ⇒ Temporalio::Connection (readonly)
Returns A connection to this environment.
18 19 20 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 18 def connection @connection end |
#namespace ⇒ String (readonly)
Returns A namespace for this environment.
21 22 23 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 21 def namespace @namespace end |
Instance Method Details
#client ⇒ Temporalio::Client
A default client to be used with this environment.
33 34 35 36 37 38 39 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 33 def client @client ||= begin # TODO: Add a workflow interceptor for interpreting assertion error interceptors = [TimeSkippingInterceptor.new(self)] Temporalio::Client.new(connection, namespace, interceptors: interceptors) end end |
#current_time ⇒ Time
Get the current time known to this environment.
For non-time-skipping environments this is simply the system time. For time-skipping environments this is whatever time has been skipped to.
64 65 66 67 68 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 64 def current_time return Time.now unless supports_time_skipping? connection.test_service.get_current_time&.time&.to_time end |
#shutdown ⇒ Object
Shut down this environment.
78 79 80 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 78 def shutdown server.shutdown end |
#sleep(duration) ⇒ Object
Sleep in this environment.
This awaits a regular Kernel.sleep in regular environments, or manually skips time in time-skipping environments.
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 47 def sleep(duration) return Kernel.sleep(duration) unless supports_time_skipping? request = Temporalio::Api::TestService::V1::SleepRequest.new( duration: Google::Protobuf::Duration.new(seconds: duration), ) connection.test_service.unlock_time_skipping_with_sleep(request) nil end |
#supports_time_skipping? ⇒ Boolean
Whether this environment supports time skipping.
73 74 75 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 73 def supports_time_skipping? server.has_test_service? end |
#with_time_skipping { ... } ⇒ any
Unlock time skipping.
This will have no effect in an environment that does not support time-skipping (e.g. Temporalite).
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/temporalio/testing/workflow_environment.rb', line 90 def with_time_skipping return yield unless supports_time_skipping? begin # Unlock to start time skipping, lock again to stop it connection.test_service.unlock_time_skipping( Temporalio::Api::TestService::V1::UnlockTimeSkippingRequest.new, ) yield ensure connection.test_service.lock_time_skipping( Temporalio::Api::TestService::V1::LockTimeSkippingRequest.new, ) end end |