Skip to content

Commit 1f89255

Browse files
committed
feat: define the "prepare" lifecycle hook
So far we had setup controller (== run) shutdown cleanuṕ The shutdown pair did not exist. Create it in this commit
1 parent 0a85e9d commit 1f89255

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

lib/roby/app.rb

+24
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ def find_registered_app_path(app_name)
531531
# (it is the opposite of setup)
532532
attr_reader :cleanup_handlers
533533

534+
# @return [Array<#call>] list of objects called just before runtime. This is
535+
# called during test setup as well
536+
attr_reader :prepare_handlers
537+
534538
# @return [Array<#call>] list of objects called when the app shuts down,
535539
# that is when the plan is being tore down but before cleanup. This is
536540
# called during test teardown as well
@@ -912,6 +916,7 @@ def initialize(plan: ExecutablePlan.new)
912916
@require_handlers = []
913917
@clear_models_handlers = []
914918
@cleanup_handlers = []
919+
@prepare_handlers = []
915920
@shutdown_handlers = []
916921
@controllers = []
917922
@action_handlers = []
@@ -1152,9 +1157,14 @@ def prepare
11521157
end
11531158
end
11541159

1160+
run_prepare_blocks
11551161
call_plugins(:prepare, self)
11561162
end
11571163

1164+
def run_prepare_blocks
1165+
prepare_handlers.each(&:call)
1166+
end
1167+
11581168
# The inverse of #prepare. It gets called either at the end of #run or
11591169
# at the end of #setup if there is an error during loading
11601170
def shutdown
@@ -1244,6 +1254,20 @@ def on_clear_models(user: false, &block)
12441254
add_lifecyle_hook(clear_models_handlers, block, user: user)
12451255
end
12461256

1257+
# Registers a callback to prepare to run
1258+
#
1259+
# This is called just before actual execution. Do things here that
1260+
# are required only at runtime, and not to load/interpret models
1261+
#
1262+
# Most operations done here must be undone in a shutdown block
1263+
def on_prepare(user: false, &block)
1264+
unless block
1265+
raise ArgumentError, "missing expected block argument"
1266+
end
1267+
1268+
add_lifecyle_hook(prepare_handlers, block, user: user)
1269+
end
1270+
12471271
# Registers a callback to perform cleanup just after an execution
12481272
#
12491273
# This is called just after plan teardown. Unlike all the other

lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ end
4848
Robot.controller do
4949
end
5050

51+
# Block evaluated right before execution, but before the cleanup
52+
#
53+
# In particular, this is executed at teardown between each tests. Mostly,
54+
# create things here that are needed only during execution. Anything done here
55+
# must be undone in the shutdown block
56+
Robot.prepare do
57+
end
58+
5159
# Block evaluated right after the execution, but before the cleanup
5260
#
5361
# In particular, this is executed at teardown between each tests. Mostly, undo

lib/roby/plan.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ def initialize(graph_observer: nil, event_logger: DRoby::NullEventLogger.new)
110110

111111
def create_null_relations
112112
@null_task_relation_graphs, @null_event_relation_graphs =
113-
self.class.instanciate_relation_graphs(graph_observer: graph_observer)
114-
@null_task_relation_graphs.freeze
113+
self.class.instanciate_relation_graphs
115114
@null_task_relation_graphs.each_value(&:freeze)
116-
@null_event_relation_graphs.freeze
115+
@null_task_relation_graphs.freeze
117116
@null_event_relation_graphs.each_value(&:freeze)
117+
@null_event_relation_graphs.freeze
118118
end
119119

120120
def create_relations

lib/roby/robot.rb

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def self.controller(reset: false, &block)
9494
Roby.app.controller(reset: reset, user: true, &block)
9595
end
9696

97+
def self.prepare(&block)
98+
Roby.app.on_prepare(user: true, &block)
99+
end
100+
97101
def self.shutdown(&block)
98102
Roby.app.on_shutdown(user: true, &block)
99103
end

lib/roby/test/spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def setup
7373
end
7474
register_plan(plan)
7575

76+
app.run_prepare_blocks
7677
super
7778
end
7879

0 commit comments

Comments
 (0)