Skip to content

Commit

Permalink
Fix sporadic blacklist spec
Browse files Browse the repository at this point in the history
Even though we stub_const for our classes (so they go away)
It isn't for a number of gc cycles until it will get cleared up

in the meantime, it will be returned from Class#subclasses

Solution: punt. let it return from subclasses.

But put a more robust implementation into our temporary ExtManagementSystem
So they behave properly and don't freak out the blacklist specs
  • Loading branch information
kbrock committed Sep 17, 2024
1 parent 5c8e041 commit 9cabad9
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 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,24 @@ 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?)

self.table_name = "vms" if parent.ancestors.include?(ActiveRecord::Base)
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 +368,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 9cabad9

Please sign in to comment.