Copilot commented on code in PR #11496:
URL: https://github.com/apache/ozone/pull/11496#discussion_r4251222752


##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/util/TestBackgroundService.java:
##########
@@ -164,4 +166,27 @@ public void testSingleWorkerThread() throws 
InterruptedException, TimeoutExcepti
     assertEquals(expFinalValues, IntStream.range(0, 
nTasks).boxed().map(map::get).collect(Collectors.toList()));
     assertTrue(queue.isEmpty());
   }
+
+  @Test
+  public void testServiceKeepsRunningAfterGetTasksThrows() throws 
InterruptedException, TimeoutException {
+    int interval = 100;
+    int serviceTimeout = 3000;
+    AtomicInteger getTasksCalls = new AtomicInteger();
+    Map<Integer, Integer> map = new HashMap<>();
+    queue.add(new TestTask(0, map, new HashMap<>()));
+    backgroundService = new BackgroundService("testGetTasksThrows", interval, 
TimeUnit.MILLISECONDS, 1,
+        serviceTimeout) {
+      @Override
+      public BackgroundTaskQueue getTasks() {
+        if (getTasksCalls.incrementAndGet() == 1) {
+          throw new NoSuchElementException("simulated failure");
+        }
+        return queue;
+      }
+    };
+    backgroundService.start();
+    // The first run throws, the periodic task must not be cancelled and the 
task runs in a later interval.
+    GenericTestUtils.waitFor(() -> map.get(0) == 1, interval / 5, 
serviceTimeout);

Review Comment:
   This wait condition can throw a NullPointerException due to auto-unboxing 
when `map.get(0)` is null (before the task runs). That would fail the test for 
the wrong reason (or make it flaky). Use a null-safe check, e.g. compare 
against `Integer.valueOf(1)` or check for null before unboxing.



##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/util/TestBackgroundService.java:
##########
@@ -25,6 +26,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;

Review Comment:
   The test's intent is to validate that *any* unchecked exception from 
`getTasks()` doesn't cancel future runs. Consider throwing a generic 
`RuntimeException` (or at least asserting behavior for a broader exception 
type) to match the production fix which catches all `RuntimeException`, not 
specifically `NoSuchElementException`.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to