-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_helper.rb
More file actions
75 lines (66 loc) · 2.07 KB
/
test_helper.rb
File metadata and controls
75 lines (66 loc) · 2.07 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
ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
require "securerandom"
require "temporal_client"
require "temporalio/client"
require "temporalio/testing"
require "temporalio/worker"
require "workflows/shopping_cart_workflow"
module TestHelper
def with_worker_running
# Create a worker on a random task queue to run our workflow and activities
raise "Task queue already obtained lazily" if TemporalClient.task_queue?
worker = Temporalio::Worker.new(
client: TemporalClient.instance,
task_queue: "tq-#{SecureRandom.uuid}",
activities: [
Workflows::ShoppingCartActivities::FetchProducts,
Workflows::ShoppingCartActivities::PersistCompletedOrder,
# Use our mock
MockApplyPaymentActivity
],
workflows: [ Workflows::ShoppingCartWorkflow ]
)
worker.run do
TemporalClient.task_queue = worker.task_queue
yield worker
ensure
TemporalClient.task_queue = nil
end
end
class MockApplyPaymentActivity < Temporalio::Activity::Definition
activity_name :ApplyPayment
def execute(input)
"mock-payment-capture-id"
end
end
end
module ActiveSupport
class TestCase
include TestHelper
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
parallelize(workers: :number_of_processors, threshold: 1)
parallelize_setup do |_worker|
TemporalClient.instance = Temporalio::Client.connect(
TemporalClient.server_target,
'default',
runtime: Temporalio::Runtime.new,
logger: Rails.logger
)
end
end
end
module ActionDispatch
class IntegrationTest
include TestHelper
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
end
end
# Start a local server, set the global client with that client, and shutdown server on complete
Temporalio::Testing::WorkflowEnvironment.start_local(logger: Rails.logger).tap do |env|
Minitest.after_run { env.shutdown }
TemporalClient.instance = env.client
end