Skip to content

Commit

Permalink
Add a test for #flat in Scope#future_resolve_sideloads
Browse files Browse the repository at this point in the history
This proves that #flat is required to flatten the promise returned by
sideload.future_resolve that is itself called from within a promise.
  • Loading branch information
MattFenelon committed Sep 14, 2024
1 parent 1b201b1 commit 8f3bd82
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions spec/scope_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec_helper"
require "byebug"

RSpec.describe Graphiti::Scope do
let(:object) { double.as_null_object }
Expand Down Expand Up @@ -198,22 +199,37 @@
self.default_page_size = 1
end.new
end
let(:departments_sideload) { double("departments", shared_remote?: false, name: :departments) }
let(:department_resource) do
Class.new(PORO::DepartmentResource) do
self.default_page_size = 1
end.new
end
let(:department_sideload) { double("department", shared_remote?: false, name: :department) }
let(:position_results) { double("positions").as_null_object }

before do
allow(position_resource).to receive(:resolve) { results }
allow(position_resource.class).to receive(:sideload).with(:department) { departments_sideload }
allow(departments_sideload).to receive(:future_resolve) { Concurrent::Promises.future {} }
allow(position_resource).to receive(:resolve) { position_results }
allow(position_resource.class).to receive(:sideload).with(:department) { department_sideload }
allow(department_resource).to receive(:resolve) { double("department").as_null_object }
allow(department_resource.class).to receive(:sideload).with(:positions) { department_positions_sideload }

# make resolve just load the sideloads
allow(sideload).to receive(:future_resolve) do |_results, q, _parent_resource|
described_class.new(double.as_null_object, position_resource, q).future_resolve
end

allow(department_sideload).to receive(:future_resolve) do |_results, q, _parent_resource|
described_class.new(double.as_null_object, department_resource, q).future_resolve
end
end

it "does not deadlock" do
expect { instance.resolve_sideloads(results) }.not_to raise_error
end

it "flattens the nested sideload promises" do
expect(instance.resolve_sideloads(results)).to contain_exactly(position_results)
end
end

context "parent thread locals" do
Expand Down

0 comments on commit 8f3bd82

Please sign in to comment.