Skip to content

Commit

Permalink
feat: define the "prepare" lifecycle hook
Browse files Browse the repository at this point in the history
So far we had

    setup
    controller (== run)
    shutdown
    cleanuṕ

The shutdown pair did not exist. Create it in this commit
  • Loading branch information
doudou committed Oct 28, 2024
1 parent 0a85e9d commit 1f89255
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/roby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ def find_registered_app_path(app_name)
# (it is the opposite of setup)
attr_reader :cleanup_handlers

# @return [Array<#call>] list of objects called just before runtime. This is
# called during test setup as well
attr_reader :prepare_handlers

# @return [Array<#call>] list of objects called when the app shuts down,
# that is when the plan is being tore down but before cleanup. This is
# called during test teardown as well
Expand Down Expand Up @@ -912,6 +916,7 @@ def initialize(plan: ExecutablePlan.new)
@require_handlers = []
@clear_models_handlers = []
@cleanup_handlers = []
@prepare_handlers = []
@shutdown_handlers = []
@controllers = []
@action_handlers = []
Expand Down Expand Up @@ -1152,9 +1157,14 @@ def prepare
end
end

run_prepare_blocks
call_plugins(:prepare, self)
end

def run_prepare_blocks
prepare_handlers.each(&:call)
end

# The inverse of #prepare. It gets called either at the end of #run or
# at the end of #setup if there is an error during loading
def shutdown
Expand Down Expand Up @@ -1244,6 +1254,20 @@ def on_clear_models(user: false, &block)
add_lifecyle_hook(clear_models_handlers, block, user: user)
end

# Registers a callback to prepare to run
#
# This is called just before actual execution. Do things here that
# are required only at runtime, and not to load/interpret models
#
# Most operations done here must be undone in a shutdown block
def on_prepare(user: false, &block)
unless block
raise ArgumentError, "missing expected block argument"
end

add_lifecyle_hook(prepare_handlers, block, user: user)
end

# Registers a callback to perform cleanup just after an execution
#
# This is called just after plan teardown. Unlike all the other
Expand Down
8 changes: 8 additions & 0 deletions lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ end
Robot.controller do
end

# Block evaluated right before execution, but before the cleanup
#
# In particular, this is executed at teardown between each tests. Mostly,
# create things here that are needed only during execution. Anything done here
# must be undone in the shutdown block
Robot.prepare do
end

# Block evaluated right after the execution, but before the cleanup
#
# In particular, this is executed at teardown between each tests. Mostly, undo
Expand Down
6 changes: 3 additions & 3 deletions lib/roby/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ def initialize(graph_observer: nil, event_logger: DRoby::NullEventLogger.new)

def create_null_relations
@null_task_relation_graphs, @null_event_relation_graphs =
self.class.instanciate_relation_graphs(graph_observer: graph_observer)
@null_task_relation_graphs.freeze
self.class.instanciate_relation_graphs
@null_task_relation_graphs.each_value(&:freeze)
@null_event_relation_graphs.freeze
@null_task_relation_graphs.freeze
@null_event_relation_graphs.each_value(&:freeze)
@null_event_relation_graphs.freeze
end

def create_relations
Expand Down
4 changes: 4 additions & 0 deletions lib/roby/robot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def self.controller(reset: false, &block)
Roby.app.controller(reset: reset, user: true, &block)
end

def self.prepare(&block)
Roby.app.on_prepare(user: true, &block)
end

def self.shutdown(&block)
Roby.app.on_shutdown(user: true, &block)
end
Expand Down
1 change: 1 addition & 0 deletions lib/roby/test/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def setup
end
register_plan(plan)

app.run_prepare_blocks
super
end

Expand Down

0 comments on commit 1f89255

Please sign in to comment.