Skip to content

Commit

Permalink
Merge pull request #23190 from kbrock/sporadic_blacklist_fix
Browse files Browse the repository at this point in the history
Fix sporadic blacklist spec
  • Loading branch information
Fryguy authored Sep 18, 2024
2 parents 5c8e041 + c72f6c3 commit 92e8d0c
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions spec/models/mixins/supports_feature_mixin_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
RSpec.describe SupportsFeatureMixin do
after do
if defined?(@defined_parent_classes)
@defined_parent_classes.each { |klass, children| cleanup_subclass(klass, children) }
end
end

let(:test_class) do
Class.new do
attr_accessor :attr1
Expand Down Expand Up @@ -338,28 +332,31 @@ def initialize(values = {})
private

def define_model(class_name, parent, supporting_values = {})
define_supporting_class(class_name, parent, supporting_values) do |r|
r.table_name = "vms" if parent.ancestors.include?(ActiveRecord::Base)
yield(r) if block_given?
end
define_supporting_class(class_name, parent, supporting_values)
end

def define_subclass(module_name, parent, supports_values = {})
define_supporting_class("#{module_name}::#{parent.name}", parent, supports_values)
end

# these descendants are stored in a cache in active support
# this cleans out those values so future runs do not have bogus classes
# this causes sporadic test failures.
def cleanup_subclass(parent, children)
tracker = ActiveSupport::DescendantsTracker.subclasses(parent)
tracker&.reject! { |child| children.include?(child) }
end

def define_supporting_class(class_name, parent, supports_values = {})
child = Class.new(parent) do
include SupportsFeatureMixin unless parent.respond_to?(:supports?)

# ActiveRecord classes need a table. Use vms table (any would work)
# This also helps when the active record class does not have a name.
self.table_name = "vms" if parent.ancestors.include?(ActiveRecord::Base)

# We sometimes create a temporary Ems child class (e.g.: Vm)
# When the spec finishes and the class is disposed, Ems.subclasses still returns the old class.
# It will go away after a few GC cycles, but before then, it causes BlacklistedEvents spec failures.
# Adding this method to have those specs work despite the bogus class returned from subclasses.
if parent == ExtManagementSystem
def self.default_blacklisted_event_names
[]
end
end

yield(self) if block_given?
supports_values.each do |feature, value|
case value
Expand All @@ -378,8 +375,6 @@ def define_supporting_class(class_name, parent, supports_values = {})
end

stub_const(class_name, child) if class_name
# remember what is subclasses so we can clean up the descendant cache
((@defined_parent_classes ||= {})[parent] ||= []) << child
child
end
end

0 comments on commit 92e8d0c

Please sign in to comment.