davsclaus commented on code in PR #28076:
URL: https://github.com/apache/camel/pull/28076#discussion_r4311100469


##########
components/camel-huawei/camel-huaweicloud-obs/src/test/java/org/apache/camel/component/huaweicloud/obs/OBSConsumerObjectThatCannotBeGotTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.camel.component.huaweicloud.obs;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListSet;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.obs.services.ObsClient;
+import com.obs.services.exception.ObsException;
+import com.obs.services.model.ListObjectsRequest;
+import com.obs.services.model.ObjectListing;
+import com.obs.services.model.ObjectMetadata;
+import com.obs.services.model.ObsObject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.ExceptionHandler;
+import org.apache.camel.spi.PollingConsumerPollStrategy;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A listed object that cannot be got (access denied, an archived object that 
is not restored) must not stop the
+ * consumer: the other listed objects are consumed, and the poll fails only 
when none of the listed objects could be
+ * got. The fake bucket lists its keys in order, a page of maxKeys keys at a 
time with the last key of a page as next
+ * marker, and answers the get of a forbidden object with 403.
+ */
+class OBSConsumerObjectThatCannotBeGotTest extends CamelTestSupport {
+
+    private static final String BUCKET = "bucket";
+
+    private final Set<String> objects = new ConcurrentSkipListSet<>();
+    private final Set<String> forbidden = ConcurrentHashMap.newKeySet();
+    private final AtomicInteger failedPolls = new AtomicInteger();
+    private final List<String> handled = new CopyOnWriteArrayList<>();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getRegistry().bind("obsClient", obsClient());
+        context.getRegistry().bind("countPolls", new 
PollingConsumerPollStrategy() {
+            @Override
+            public boolean begin(Consumer consumer, Endpoint endpoint) {
+                return true;
+            }
+
+            @Override
+            public void commit(Consumer consumer, Endpoint endpoint, int 
polledMessages) {
+                // noop
+            }
+
+            @Override
+            public boolean rollback(Consumer consumer, Endpoint endpoint, int 
retryCounter, Exception e) {
+                failedPolls.incrementAndGet();
+                return false;
+            }
+        });
+        context.getRegistry().bind("recordErrors", new ExceptionHandler() {
+            @Override
+            public void handleException(Throwable exception) {
+                handled.add(String.valueOf(exception.getMessage()));
+            }
+
+            @Override
+            public void handleException(String message, Throwable exception) {
+                handled.add(message);
+            }
+
+            @Override
+            public void handleException(String message, Exchange exchange, 
Throwable exception) {
+                handled.add(message);
+            }
+        });
+        return context;
+    }
+
+    @Test
+    void objectThatCannotBeGotDoesNotBlockTheOtherObjects() throws Exception {
+        objects.add("a.txt");
+        objects.add("b.txt");
+        objects.add("c.txt");
+        forbidden.add("b.txt");
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("a.txt", "c.txt");
+
+        
addConsumerRoute("&deleteAfterRead=true&exceptionHandler=#recordErrors");
+
+        mock.assertIsSatisfied();
+        assertEquals(Set.of("b.txt"), Set.copyOf(objects), "a.txt and c.txt 
are consumed and deleted, b.txt stays");

Review Comment:
   CI fails here: `expected: <[b.txt]> but was: <[b.txt, c.txt]>`. With 
`deleteAfterRead=true` the object is deleted when the exchange completes, which 
is after it reached `mock:result`, so `assertIsSatisfied()` can return before 
`c.txt` is deleted. Waiting for the bucket closes the race:
   
   ```suggestion
           await().atMost(20, TimeUnit.SECONDS).untilAsserted(
                   () -> assertEquals(Set.of("b.txt"), Set.copyOf(objects), 
"a.txt and c.txt are consumed and deleted, b.txt stays"));
   ```



##########
components/camel-huawei/camel-huaweicloud-obs/src/test/java/org/apache/camel/component/huaweicloud/obs/OBSConsumerObjectThatCannotBeGotTest.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.camel.component.huaweicloud.obs;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentSkipListSet;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.obs.services.ObsClient;
+import com.obs.services.exception.ObsException;
+import com.obs.services.model.ListObjectsRequest;
+import com.obs.services.model.ObjectListing;
+import com.obs.services.model.ObjectMetadata;
+import com.obs.services.model.ObsObject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.ExceptionHandler;
+import org.apache.camel.spi.PollingConsumerPollStrategy;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A listed object that cannot be got (access denied, an archived object that 
is not restored) must not stop the
+ * consumer: the other listed objects are consumed, and the poll fails only 
when none of the listed objects could be
+ * got. The fake bucket lists its keys in order, a page of maxKeys keys at a 
time with the last key of a page as next
+ * marker, and answers the get of a forbidden object with 403.
+ */
+class OBSConsumerObjectThatCannotBeGotTest extends CamelTestSupport {
+
+    private static final String BUCKET = "bucket";
+
+    private final Set<String> objects = new ConcurrentSkipListSet<>();
+    private final Set<String> forbidden = ConcurrentHashMap.newKeySet();
+    private final AtomicInteger failedPolls = new AtomicInteger();
+    private final List<String> handled = new CopyOnWriteArrayList<>();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getRegistry().bind("obsClient", obsClient());
+        context.getRegistry().bind("countPolls", new 
PollingConsumerPollStrategy() {
+            @Override
+            public boolean begin(Consumer consumer, Endpoint endpoint) {
+                return true;
+            }
+
+            @Override
+            public void commit(Consumer consumer, Endpoint endpoint, int 
polledMessages) {
+                // noop
+            }
+
+            @Override
+            public boolean rollback(Consumer consumer, Endpoint endpoint, int 
retryCounter, Exception e) {
+                failedPolls.incrementAndGet();
+                return false;
+            }
+        });
+        context.getRegistry().bind("recordErrors", new ExceptionHandler() {
+            @Override
+            public void handleException(Throwable exception) {
+                handled.add(String.valueOf(exception.getMessage()));
+            }
+
+            @Override
+            public void handleException(String message, Throwable exception) {
+                handled.add(message);
+            }
+
+            @Override
+            public void handleException(String message, Exchange exchange, 
Throwable exception) {
+                handled.add(message);
+            }
+        });
+        return context;
+    }
+
+    @Test
+    void objectThatCannotBeGotDoesNotBlockTheOtherObjects() throws Exception {
+        objects.add("a.txt");
+        objects.add("b.txt");
+        objects.add("c.txt");
+        forbidden.add("b.txt");
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("a.txt", "c.txt");
+
+        
addConsumerRoute("&deleteAfterRead=true&exceptionHandler=#recordErrors");
+
+        mock.assertIsSatisfied();
+        assertEquals(Set.of("b.txt"), Set.copyOf(objects), "a.txt and c.txt 
are consumed and deleted, b.txt stays");
+        await().atMost(20, TimeUnit.SECONDS).untilAsserted(
+                () -> assertTrue(handled.stream().anyMatch(m -> 
m.contains("b.txt")), "the skipped object is reported"));
+    }
+
+    @Test
+    void objectThatCannotBeGotDoesNotBlockTheObjectsOfItsPage() throws 
Exception {
+        objects.add("a.txt");
+        objects.add("b.txt");
+        objects.add("c.txt");
+        objects.add("d.txt");
+        forbidden.add("a.txt");
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceivedInAnyOrder("b.txt", "c.txt", "d.txt");
+
+        // two objects per page: a.txt shares the first page with b.txt
+        addConsumerRoute("&deleteAfterRead=true&maxMessagesPerPoll=2");
+
+        mock.assertIsSatisfied();
+        assertEquals(Set.of("a.txt"), Set.copyOf(objects), "b.txt, c.txt and 
d.txt are consumed and deleted");

Review Comment:
   Same race as above.
   
   ```suggestion
           await().atMost(20, TimeUnit.SECONDS).untilAsserted(
                   () -> assertEquals(Set.of("a.txt"), Set.copyOf(objects), 
"b.txt, c.txt and d.txt are consumed and deleted"));
   ```



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