A platform for community discussion. Free, open, simple.
DEV: Fix isolation bugs in service framework's `each` step (#40311)
Loïc Guitautf92a03593623
diff --git a/lib/service/base/context.rb b/lib/service/base/context.rbindex 71700514fbd..0264b99e0f7 100644--- a/lib/service/base/context.rb+++ b/lib/service/base/context.rb@@ -23,15 +23,16 @@ module Serviceenddef to_h- store.deep_dup+ store.dupenddef with_isolation(persist_keys: [])- @isolated_store = to_h+ isolated_stores << to_hyieldensure- @store.merge!(@isolated_store.slice(*persist_keys, *step_result_keys))- @isolated_store = nil+ isolated_stores.pop.then do |isolated_store|+ store.merge!(isolated_store.slice(*persist_keys, *step_result_keys(isolated_store)))+ endend# @return [Boolean] returns +true+ if the context is set as successful (default)@@ -74,11 +75,15 @@ module Serviceprivatedef store- @isolated_store || @store+ isolated_stores.last || @storeend- def step_result_keys- store.keys.select { it.start_with?("result.") }+ def isolated_stores+ @isolated_stores ||= []+ end++ def step_result_keys(source = store)+ source.keys.select { it.start_with?("result.") }enddef method_missing(method_name, *args, &block)diff --git a/lib/service/base/each_step.rb b/lib/service/base/each_step.rbindex d6141a3fea9..708442d4ef4 100644--- a/lib/service/base/each_step.rb+++ b/lib/service/base/each_step.rb@@ -16,18 +16,18 @@ module Serviceenddef run_step- context.with_isolation(persist_keys: [item_name, :index, *initializers.keys]) do- context.merge!(initializers.transform_values { instance.instance_exec(&it) })- Array- .wrap(context[name])- .tap do |collection|- context[result_key].merge!(total: collection.size, skipped?: collection.empty?)- end- .each_with_index do |item, index|+ context.merge!(initializers.transform_values { instance.instance_exec(&it) })+ Array+ .wrap(context[name])+ .tap do |collection|+ context[result_key].merge!(total: collection.size, skipped?: collection.empty?)+ end+ .each_with_index do |item, index|+ context.with_isolation(persist_keys: [item_name, :index, *initializers.keys]) docontext.merge!(item_name => item, :index => index)steps.each { |step| step.call(instance, context) }end- end+ endendprivatediff --git a/spec/lib/service/base/each_step_spec.rb b/spec/lib/service/base/each_step_spec.rbindex dbc07822c25..beb7c952000 100644--- a/spec/lib/service/base/each_step_spec.rb+++ b/spec/lib/service/base/each_step_spec.rb@@ -132,6 +132,80 @@ RSpec.describe Service::Base::EachStep doendend+ context "with isolation between iterations" do+ let(:service) do+ Class.new do+ include Service::Base++ step :setup++ each :users, persist: { observed_values: -> { [] } } do+ step :track+ end++ private++ def setup+ context[:users] = %i[alice bob]+ end++ def track(user:, observed_values:)+ observed_values << context[:transient]+ context[:transient] = user+ end+ end+ end++ it "does not leak non-persisted variables between iterations" do+ expect(result).to have_attributes(observed_values: [nil, nil])+ end+ end++ context "when the outer context contains an ActiveRecord model" do+ fab!(:user)++ let(:service) do+ Class.new do+ include Service::Base++ model :user+ step :setup_usernames++ each :usernames,+ persist: {+ seen_user_ids: -> { [] },+ user_persisted_states: -> { [] },+ } do+ step :track_user+ end++ private++ def fetch_user(user_id:)+ User.find(user_id)+ end++ def setup_usernames+ context[:usernames] = %i[alice bob]+ end++ def track_user(user:, seen_user_ids:, user_persisted_states:)+ seen_user_ids << user.id+ user_persisted_states << user.persisted?+ end+ end+ end++ let(:dependencies) { { user_id: user.id } }++ it "keeps the original model available inside each iterations" do+ expect(result).to have_attributes(+ seen_user_ids: [user.id, user.id],+ user_persisted_states: [true, true],+ )+ end+ end+context "with persist option" docontext "with a lambda initializer" dolet(:service) do@@ -196,5 +270,45 @@ RSpec.describe Service::Base::EachStep doendendend++ context "when nesting each steps" do+ let(:service) do+ Class.new do+ include Service::Base++ model :groups++ each :groups, persist: { processed: -> { [] } } do+ each :group, as: :item, persist: { processed: -> { context[:processed] } } do+ step :collect_item+ end+ end++ private++ def fetch_groups+ context[:input_groups]+ end++ def collect_item(group:, item:, processed:)+ processed << [group, item]+ end+ end+ end+ let(:dependencies) { { input_groups: [%i[alice bob], %i[charlie dave]] } }++ it { is_expected.to run_successfully }++ it "accumulates results across both levels of iteration" do+ expect(result[:processed]).to eq(+ [+ [%i[alice bob], :alice],+ [%i[alice bob], :bob],+ [%i[charlie dave], :charlie],+ [%i[charlie dave], :dave],+ ],+ )+ end+ endendend