Skip to content

Commit 42ffda3

Browse files
committed
Prefetch all collection items globally before tests
Instead of making 3 GraphQL API calls per collection (~300 total for 100 collections), collect all repo and user references across all collections upfront, deduplicate them, and batch the GraphQL queries. This reduces API round-trips from ~300 to ~10-15 (depending on total unique items), significantly improving test suite performance while maintaining the same validation coverage.
1 parent 67774fb commit 42ffda3

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

test/collections_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
end
138138

139139
it "fails if a user, organization, or repository has been renamed or removed" do
140+
prefetch_all_collection_items!
141+
140142
errors = []
141143
repos_to_check = []
142144
users_to_check = []
@@ -151,10 +153,6 @@
151153
end
152154
end
153155

154-
cache_repos_exist_check!(repos_to_check)
155-
cache_users_exist_check!(users_to_check)
156-
cache_orgs_exist_check!(users_not_found_from(users_to_check))
157-
158156
repos_to_check.each do |repo|
159157
repo_result = client.repository(repo)
160158
current_name_with_owner = repo_result&.full_name

test/collections_test_helper.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,43 @@ def annotate_collection_item_error(collection, string, error_message)
106106
def possible_image_file_names_for_collection(collection)
107107
COLLECTION_IMAGE_EXTENSIONS.map { |ext| "#{collection}#{ext}" }
108108
end
109+
110+
GRAPHQL_BATCH_SIZE = 100
111+
112+
def prefetch_all_collection_items!
113+
return if @_prefetched
114+
115+
all_repos = []
116+
all_users = []
117+
118+
collections.each do |collection|
119+
items_for_collection(collection)&.each do |item|
120+
if item.match?(USERNAME_AND_REPO_REGEX)
121+
all_repos << item
122+
elsif item.match?(USERNAME_REGEX)
123+
all_users << item
124+
end
125+
end
126+
end
127+
128+
all_repos.uniq!
129+
all_users.uniq!
130+
131+
# Batch repos in chunks to stay within GraphQL query limits
132+
all_repos.each_slice(GRAPHQL_BATCH_SIZE) do |batch|
133+
cache_repos_exist_check!(batch)
134+
end
135+
136+
# Batch users in chunks
137+
all_users.each_slice(GRAPHQL_BATCH_SIZE) do |batch|
138+
cache_users_exist_check!(batch)
139+
end
140+
141+
# Check orgs for users not found
142+
not_found_users = users_not_found_from(all_users)
143+
not_found_users.each_slice(GRAPHQL_BATCH_SIZE) do |batch|
144+
cache_orgs_exist_check!(batch)
145+
end
146+
147+
@_prefetched = true
148+
end

0 commit comments

Comments
 (0)