Skip to content

Commit

Permalink
Test coverage of all code paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 9, 2024
1 parent 7691c0b commit a839e29
Showing 1 changed file with 109 additions and 3 deletions.
112 changes: 109 additions & 3 deletions test/protocol/http2/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ def before
c.dependency.weight = 20
d.dependency.weight = 30

# a __
# / \ \
# b c d
# a
# / | \
# b c d

a.priority = a.priority
server.read_frame
Expand All @@ -236,17 +236,123 @@ def before
d.priority = priority
server.read_frame

# Force the ordered_children to be generated, so that we start triggering insertion logic:
ordered_children = server.dependencies[a.id].ordered_children

priority = b.priority
priority.stream_dependency = a.id
b.priority = priority
server.read_frame

priority = c.priority
priority.stream_dependency = a.id
c.priority = priority
server.read_frame

expect(ordered_children).to be == [b.dependency, c.dependency, d.dependency]
end

it "handles case where index is nil during insertion" do
a, b, c, d = 4.times.collect { client.create_stream }

# Assign weights such that no existing child meets the condition:
b.dependency.weight = 5
c.dependency.weight = 10
d.dependency.weight = 15

# Priority tree setup:
# a
# /|\
# b c d

a.priority = a.priority
server.read_frame

priority = b.priority
priority.stream_dependency = a.id
b.priority = priority
server.read_frame

# Force the ordered_children to be generated, so that we start triggering insertion logic:
ordered_children = server.dependencies[a.id].ordered_children

priority = c.priority
priority.stream_dependency = a.id
c.priority = priority
server.read_frame

priority = d.priority
priority.stream_dependency = a.id
d.priority = priority
server.read_frame

# Insert a new dependency with a weight that is higher than all current children:
new_child = client.create_stream
new_child.dependency.weight = 20 # Greater than the weights of b, c, and d

# Verify that `index` would have been `nil` when finding the insertion point:
index = ordered_children.bsearch_index { |child| child.weight >= new_child.dependency.weight }
expect(index).to be_nil

priority = new_child.priority
priority.stream_dependency = a.id
new_child.priority = priority
server.read_frame

# Check that the new child is inserted at the correct place:
expect(ordered_children).to be(:include?, new_child.dependency)
expect(ordered_children.last).to be == new_child.dependency
end

it "can update the weight of a child on removal" do
a, b, c, d = 4.times.collect { client.create_stream }

# Assign weights such that no existing child meets the condition:
b.dependency.weight = 5
c.dependency.weight = 10
d.dependency.weight = 15

# Priority tree setup:
# a
# /|\
# b c d

a.priority = a.priority
server.read_frame

priority = b.priority
priority.stream_dependency = a.id
b.priority = priority
server.read_frame

# Force the ordered_children to be generated, so that we start triggering insertion logic:
ordered_children = server.dependencies[a.id].ordered_children

priority = c.priority
priority.stream_dependency = a.id
c.priority = priority
server.read_frame

priority = d.priority
priority.stream_dependency = a.id
d.priority = priority
server.read_frame

expect(ordered_children).to be == [b.dependency, c.dependency, d.dependency]

# Check the total weight makes sense:
expect(server.dependencies[a.id].total_weight).to be == 30

# Remove the child with the highest weight:
server.delete(d.id)

# Check that the weight of the remaining children has been updated:
expect(server.dependencies[a.id].total_weight).to be == 15

# Force the deletion logic to clean up `ordered_children`:
server.dependencies[a.id].consume_window(15)

# Check that the child has been removed:
expect(ordered_children).to be == [b.dependency, c.dependency]
end
end

0 comments on commit a839e29

Please sign in to comment.