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
|
# frozen_string_literal: true
module Gitlab
class Experiment
module Nestable
extend ActiveSupport::Concern
included do
set_callback :run, :around, :manage_nested_stack
end
def nest_experiment(nested_experiment)
instance_exec(nested_experiment, &Configuration.nested_behavior)
end
private
def manage_nested_stack
Stack.push(self)
yield
ensure
Stack.pop
end
class Stack
include Singleton
delegate :pop, :length, :size, :[], to: :stack
class << self
delegate :pop, :push, :length, :size, :[], to: :instance
end
def initialize
@thread_key = "#{self.class};#{object_id}".to_sym
end
def push(instance)
stack.last&.nest_experiment(instance)
stack.push(instance)
end
private
def stack
Thread.current[@thread_key] ||= []
end
end
end
end
end
|