diff options
Diffstat (limited to 'qa/qa/support/fabrication_tracker.rb')
-rw-r--r-- | qa/qa/support/fabrication_tracker.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/qa/qa/support/fabrication_tracker.rb b/qa/qa/support/fabrication_tracker.rb new file mode 100644 index 00000000000..3238cc5b0db --- /dev/null +++ b/qa/qa/support/fabrication_tracker.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module QA + module Support + # Threadsafe fabrication time tracker + # + # Ongoing fabrication is added to callstack by start_fabrication and taken out by finish_fabrication + # + # Fabrication runtime is saved only for the first fabrication in the stack to properly represent the real time + # fabrications might take as top level fabrication runtime will always include nested fabrications runtime + # + class FabricationTracker + class << self + # Start fabrication and increment ongoing fabrication count + # + # @return [void] + def start_fabrication + Thread.current[:fabrications_ongoing] = 0 unless Thread.current.key?(:fabrications_ongoing) + + Thread.current[:fabrications_ongoing] += 1 + end + + # Finish fabrication and decrement ongoing fabrication count + # + # @return [void] + def finish_fabrication + Thread.current[:fabrications_ongoing] -= 1 + end + + # Save fabrication time if it's first in fabrication stack + # + # @param [Symbol] type + # @param [Symbol] time + # @return [void] + def save_fabrication(type, time) + return unless Thread.current.key?(type) + return unless top_level_fabrication? + + Thread.current[type] += time + end + + private + + # Check if current fabrication is the only one in the stack + # + # @return [Boolean] + def top_level_fabrication? + Thread.current[:fabrications_ongoing] == 1 + end + end + end + end +end |