This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new 12a49de9f1 Make JdbcExampleTest robust to reruns and command 
registration race (#2787)
12a49de9f1 is described below

commit 12a49de9f15d254f6d342211048199124cedd52d
Author: JB Onofré <[email protected]>
AuthorDate: Sat Aug 1 07:28:07 2026 +0200

    Make JdbcExampleTest robust to reruns and command registration race (#2787)
    
    The test hard-coded the booking id (1) and asserted on a shared literal
    flight code (AF520). The example stores bookings in an AUTO_INCREMENT
    table shared across runs of the same Karaf instance, so when a transient
    CommandNotFoundException on booking:remove triggered the whole-method
    @Retry, the rerun's booking:add got id 2 while id 1's row lingered,
    making booking:remove 1 + assertContainsNot("AF520") fail deterministically.
    
    Use a unique flight code per run and resolve the actual auto-incremented
    id from the booking:list output instead of assuming 1, so leftover rows
    no longer disturb the assertions and the retry can genuinely recover.
---
 .../karaf/itests/examples/JdbcExampleTest.java     | 37 ++++++++++++++++++----
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
index 6128b463dd..3e7c1939ff 100644
--- 
a/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
+++ 
b/itests/test/src/test/java/org/apache/karaf/itests/examples/JdbcExampleTest.java
@@ -37,21 +37,46 @@ public class JdbcExampleTest extends BaseTest {
         // install the karaf-jdbc-example feature
         installAndAssertFeature("karaf-jdbc-example");
 
+        // use a unique flight code so the assertions are not disturbed by 
bookings possibly
+        // left over by a previous (retried) run: the example stores bookings 
in an AUTO_INCREMENT
+        // table that is shared across runs of the same Karaf instance
+        String flight = "AF" + System.currentTimeMillis();
+
         // add booking
-        executeCommand("booking:add Foo AF520");
+        executeCommand("booking:add Foo " + flight);
         // list booking
         String bookings = executeCommand("booking:list");
         System.out.println(bookings);
-        assertContains("AF520", bookings);
+        assertContains(flight, bookings);
+
+        // resolve the actual (auto-incremented) id instead of assuming it is 1
+        long id = bookingId(bookings, flight);
+
         // get booking
-        String booking = executeCommand("booking:get 1");
+        String booking = executeCommand("booking:get " + id);
         System.out.println(booking);
-        assertContains("AF520", booking);
+        assertContains(flight, booking);
         // remove booking
-        executeCommand("booking:remove 1");
+        executeCommand("booking:remove " + id);
         bookings = executeCommand("booking:list");
         System.out.println(bookings);
-        assertContainsNot("AF520", bookings);
+        assertContainsNot(flight, bookings);
+    }
+
+    /**
+     * Extracts the booking id (first column) of the row matching the given 
flight code from the
+     * {@code booking:list} shell table output.
+     */
+    private long bookingId(String listOutput, String flight) {
+        for (String line : listOutput.split("\\r?\\n")) {
+            if (line.contains(flight)) {
+                String id = line.split("\\|")[0].trim();
+                if (id.matches("\\d+")) {
+                    return Long.parseLong(id);
+                }
+            }
+        }
+        throw new IllegalStateException("No booking found for flight " + 
flight + " in:\n" + listOutput);
     }
 
 }

Reply via email to