gnodet-bot commented on code in PR #13845:
URL: https://github.com/apache/maven/pull/13845#discussion_r4477672292


##########
impl/maven-cli/src/test/java/org/apache/maven/cling/transfer/Slf4jMavenTransferListenerTest.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.cling.transfer;
+
+import java.io.File;
+import java.time.Clock;
+import java.time.ZoneOffset;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.maven.api.MonotonicClock;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.transfer.TransferEvent;
+import org.eclipse.aether.transfer.TransferResource;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.slf4j.Logger;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+class Slf4jMavenTransferListenerTest {
+
+    @Test
+    void subSecondTransferReportsItsRate() {
+        TransferResource resource = resourceStartedMillisAgo(250);
+        TransferEvent event = new TransferEvent.Builder(new 
DefaultRepositorySystemSession(h -> false), resource)
+                .setType(TransferEvent.EventType.SUCCEEDED)
+                .setTransferredBytes(7_900)
+                .build();
+        Logger logger = mock(Logger.class);

Review Comment:
   🐛 **[Critical] Test has a timing-sensitive ordering bug.**
   
   `resourceStartedMillisAgo(250)` on line 43 snapshots `MonotonicClock.now()` 
as the transfer start time. However, `mock(Logger.class)` on line 48 — which 
comes _after_ — triggers Mockito classloading (~1s on a cold JVM). By the time 
`transferSucceeded` calls `MonotonicClock.now()` again inside 
`bytesPerSecond()`, ~1.3s has elapsed instead of ~250ms. The computed rate 
drops to ~5.8 kB/s, below the 7.9 kB/s floor, causing a spurious assertion 
failure.
   
   **Fix:** move `mock(Logger.class)` _before_ `resourceStartedMillisAgo(250)` 
so classloading happens before the clock snapshot:
   
   ```suggestion
           Logger logger = mock(Logger.class);
           TransferResource resource = resourceStartedMillisAgo(250);
           TransferEvent event = new TransferEvent.Builder(new 
DefaultRepositorySystemSession(h -> false), resource)
                   .setType(TransferEvent.EventType.SUCCEEDED)
                   .setTransferredBytes(7_900)
                   .build();
   ```



##########
impl/maven-cli/src/main/java/org/apache/maven/cling/transfer/AbstractMavenTransferListener.java:
##########
@@ -44,6 +46,23 @@ protected 
AbstractMavenTransferListener(MessageBuilderFactory messageBuilderFact
         this.out = out;
     }
 
+    /**
+     * Returns the average rate of a transfer, measured in fractional seconds 
so that transfers shorter than a second
+     * get a rate too.
+     *
+     * @param start when the transfer started
+     * @param transferredBytes the number of bytes transferred so far
+     * @return the rate in bytes per second, or empty if no time has elapsed
+     */
+    static OptionalDouble bytesPerSecond(Instant start, long transferredBytes) 
{

Review Comment:
   💡 **[Medium] Consider an `Instant`-accepting overload for testability.**
   
   `bytesPerSecond(Instant start, long transferredBytes)` hardcodes 
`MonotonicClock.now()` as the end time, making every test that exercises this 
path dependent on real wall-clock elapsed time. Even with the mock-ordering 
fix, the test remains inherently fragile under load (slow CI, debugging pauses, 
etc.).
   
   A second overload accepting both `start` and `end`:
   ```java
   static OptionalDouble bytesPerSecond(Instant start, Instant end, long 
transferredBytes) {
       long nanos = Duration.between(start, end).toNanos();
       if (nanos <= 0) return OptionalDouble.empty();
       return OptionalDouble.of(transferredBytes / (nanos / (double) 
TimeUnit.SECONDS.toNanos(1)));
   }
   static OptionalDouble bytesPerSecond(Instant start, long transferredBytes) {
       return bytesPerSecond(start, MonotonicClock.now(), transferredBytes);
   }
   ```
   Tests can then pin both endpoints and completely eliminate the wall-clock 
dependency.



-- 
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]

Reply via email to