gnodet-bot commented on code in PR #14214: URL: https://github.com/apache/maven/pull/14214#discussion_r4757232129
########## impl/maven-core/src/test/java/org/apache/maven/lifecycle/internal/concurrent/BuildPlanWeightsTest.java: ########## @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.lifecycle.internal.concurrent; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests for {@link BuildPlan#computeWeights(Map)}. + */ +class BuildPlanWeightsTest { + + // ------------------------------------------------------------------------- + // Depth-based weights (empty timings) + // ------------------------------------------------------------------------- + + @Test + void singleStepGetsWeightOne() { + BuildStep a = step("compile"); + BuildPlan plan = planOf(a); + + plan.computeWeights(Map.of()); + + assertEquals(1L, a.weight); + } + + @Test + void linearChainPropagatesWeightsCorrectly() { + // a → b → c (c is last, a is first) + BuildStep a = step("compile"); + BuildStep b = step("test"); + BuildStep c = step("package"); + link(a, b); + link(b, c); + BuildPlan plan = planOf(a, b, c); + + plan.computeWeights(Map.of()); + + // c has no successors → weight = 1 + // b depends on c → weight = 1 (self) + 1 (c) = 2 + // a depends on b → weight = 1 (self) + 2 (b) = 3 + assertEquals(1L, c.weight); + assertEquals(2L, b.weight); + assertEquals(3L, a.weight); + } + + @Test + void heavierPathWinsAtBranchPoint() { + // root → left → leaf + // → right (shorter path) + BuildStep root = step("root"); + BuildStep left = step("left"); + BuildStep leaf = step("leaf"); + BuildStep right = step("right"); + link(root, left); + link(left, leaf); + link(root, right); + BuildPlan plan = planOf(root, left, leaf, right); + + plan.computeWeights(Map.of()); + + // leaf: 1, left: 2, right: 1, root: max(2,1)+1 = 3 + assertEquals(1L, leaf.weight); + assertEquals(2L, left.weight); + assertEquals(1L, right.weight); + assertEquals(3L, root.weight); + } + + @Test + void disconnectedStepsEachGetWeightOne() { + BuildStep a = step("compile"); + BuildStep b = step("test"); + BuildPlan plan = planOf(a, b); + + plan.computeWeights(Map.of()); + + assertEquals(1L, a.weight); + assertEquals(1L, b.weight); + } + + // ------------------------------------------------------------------------- + // Timing-based weights (non-empty timings) + // ------------------------------------------------------------------------- + + @Test + void timingDataIsUsedAsStepWeight() { + // a → b, with timing: a=50ms, b=200ms + BuildStep a = step("com.example", "a", "compile"); + BuildStep b = step("com.example", "b", "compile"); + link(a, b); + BuildPlan plan = planOf(a, b); + + Map<String, Long> timings = Map.of( + a.stepId(), 50L, + b.stepId(), 200L); + plan.computeWeights(timings); + + // b: weight = 200 (self, no successor) + // a: weight = 50 (self) + 200 (b) = 250 + assertEquals(200L, b.weight); + assertEquals(250L, a.weight); + } + + @Test + void missingTimingEntryFallsBackToOne() { + BuildStep known = step("com.example", "a", "compile"); + BuildStep unknown = step("com.example", "b", "test"); + BuildPlan plan = planOf(known, unknown); + + Map<String, Long> timings = Map.of(known.stepId(), 100L); + plan.computeWeights(timings); + + assertEquals(100L, known.weight); + assertEquals(1L, unknown.weight); // fallback + } + + @Test + void zeroMillisTimingClampedToOne() { + // A step with 0ms recorded duration should not get weight 0 (would break ordering). + BuildStep a = step("com.example", "a", "compile"); + BuildPlan plan = planOf(a); + + plan.computeWeights(Map.of(a.stepId(), 0L)); + + // Math.max(1L, 0L) = 1 + assertEquals(1L, a.weight); + } + + // ------------------------------------------------------------------------- + // weightSortedNodes() is weight-ordered after computeWeights() + // ------------------------------------------------------------------------- + + @Test + void weightSortedNodesAreOrderedByDescendingWeightAfterComputeWeights() { + // a(weight=3) → b(weight=2) → c(weight=1) + BuildStep a = step("compile"); + BuildStep b = step("test"); + BuildStep c = step("package"); + link(a, b); + link(b, c); + BuildPlan plan = planOf(a, b, c); + + plan.computeWeights(Map.of()); + + // weightSortedNodes() returns heaviest-first; sortedNodes() stays topological. + java.util.List<BuildStep> weighted = plan.weightSortedNodes(); + assertTrue(weighted.get(0).weight >= weighted.get(1).weight, "First step should have weight >= second"); + assertTrue(weighted.get(1).weight >= weighted.get(2).weight, "Second step should have weight >= third"); + assertEquals(a, weighted.get(0), "Heaviest step (root) must come first"); Review Comment: 💡 **Nit (low):** The comment says `sortedNodes() stays topological`, but the test never asserts this — it only checks `weightSortedNodes()` ordering. Consider adding a direct assertion that `sortedNodes()` is unaffected by `computeWeights()`: ```suggestion assertEquals(a, weighted.get(0), "Heaviest step (root) must come first"); // Verify sortedNodes() topological order is unaffected by computeWeights() java.util.List<BuildStep> topo = plan.sortedNodes(); assertEquals(List.of(a, b, c), topo, "sortedNodes() must remain in topological order after computeWeights()"); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
