Skip to content

Commit

Permalink
Add starting fiber assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 24, 2024
1 parent 71f6ebd commit 14794b1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions spec/graphql/dataloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,18 @@ class << self
attr_accessor :starting_fiber_count, :last_spawn_fiber_count, :last_max_fiber_count

def current_fiber_count
count_all_fibers - starting_fiber_count
count_active_fibers - starting_fiber_count
end

def count_all_fibers
def count_active_fibers
GC.start
ObjectSpace.each_object(Fiber).count
end
end

def initialize(*args, **kwargs, &block)
super
FiberCounting.starting_fiber_count = FiberCounting.count_all_fibers
FiberCounting.starting_fiber_count = FiberCounting.count_active_fibers
FiberCounting.last_max_fiber_count = 0
FiberCounting.last_spawn_fiber_count = 0
end
Expand Down Expand Up @@ -1109,17 +1109,21 @@ def self.included(child_class)
fiber_counting_dataloader_class = Class.new(schema.dataloader_class)
fiber_counting_dataloader_class.include(FiberCounting)

_res = schema.execute(query_str, context: { dataloader: fiber_counting_dataloader_class.new })
res = schema.execute(query_str, context: { dataloader: fiber_counting_dataloader_class.new })
assert_nil res.context.dataloader.fiber_limit
assert_equal 1, FiberCounting.starting_fiber_count
assert_equal 12, FiberCounting.last_spawn_fiber_count
assert_equal 9, FiberCounting.last_max_fiber_count

res = schema.execute(query_str, context: { dataloader: fiber_counting_dataloader_class.new(fiber_limit: 4) })
assert_equal 4, res.context.dataloader.fiber_limit
assert_equal 1, FiberCounting.starting_fiber_count
assert_equal 14, FiberCounting.last_spawn_fiber_count
assert_equal 4, FiberCounting.last_max_fiber_count

res = schema.execute(query_str, context: { dataloader: fiber_counting_dataloader_class.new(fiber_limit: 6) })
assert_equal 6, res.context.dataloader.fiber_limit
assert_equal 1, FiberCounting.starting_fiber_count
assert_equal 10, FiberCounting.last_spawn_fiber_count
assert_equal 6, FiberCounting.last_max_fiber_count
end
Expand Down

0 comments on commit 14794b1

Please sign in to comment.