Class: Temporalio::Testing::WorkflowEnvironment

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

Overview

Note:

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

Instance Method Summary collapse

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

#connectionTemporalio::Connection (readonly)

Returns A connection to this environment.

Returns:



18
19
20
# File 'lib/temporalio/testing/workflow_environment.rb', line 18

def connection
  @connection
end

#namespaceString (readonly)

Returns A namespace for this environment.

Returns:

  • (String)

    A namespace for this environment.



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

def namespace
  @namespace
end

Instance Method Details

#clientTemporalio::Client

A default client to be used with this environment.

Returns:



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_timeTime

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.

Returns:

  • (Time)


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

#shutdownObject

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.

Parameters:

  • duration (Integer)

    Amount of time to sleep.



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.

Returns:

  • (Boolean)


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).

Yields:

  • A block to be called once time skipping has been unlocked.

Returns:

  • (any)

    The return value of the block.



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