From 1f89255d19c37bdadeb9175c313502d58035f761 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 23 Nov 2023 22:00:01 -0300 Subject: [PATCH] feat: define the "prepare" lifecycle hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So far we had setup controller (== run) shutdown cleanuṕ The shutdown pair did not exist. Create it in this commit --- lib/roby/app.rb | 24 +++++++++++++++++++ .../gen/roby_app/config/robots/robot.rb.erb | 8 +++++++ lib/roby/plan.rb | 6 ++--- lib/roby/robot.rb | 4 ++++ lib/roby/test/spec.rb | 1 + 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/roby/app.rb b/lib/roby/app.rb index 805a96077..5e9dabbed 100644 --- a/lib/roby/app.rb +++ b/lib/roby/app.rb @@ -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 @@ -912,6 +916,7 @@ def initialize(plan: ExecutablePlan.new) @require_handlers = [] @clear_models_handlers = [] @cleanup_handlers = [] + @prepare_handlers = [] @shutdown_handlers = [] @controllers = [] @action_handlers = [] @@ -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 @@ -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 diff --git a/lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb b/lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb index f3387e7d8..880f4da92 100644 --- a/lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb +++ b/lib/roby/cli/gen/roby_app/config/robots/robot.rb.erb @@ -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 diff --git a/lib/roby/plan.rb b/lib/roby/plan.rb index 406deeb5a..3ba2382f7 100644 --- a/lib/roby/plan.rb +++ b/lib/roby/plan.rb @@ -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 diff --git a/lib/roby/robot.rb b/lib/roby/robot.rb index 9d84f4c9b..6aec23dea 100644 --- a/lib/roby/robot.rb +++ b/lib/roby/robot.rb @@ -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 diff --git a/lib/roby/test/spec.rb b/lib/roby/test/spec.rb index 7435f16b2..d32173e34 100644 --- a/lib/roby/test/spec.rb +++ b/lib/roby/test/spec.rb @@ -73,6 +73,7 @@ def setup end register_plan(plan) + app.run_prepare_blocks super end