allthingssecurity commented on code in PR #30703:
URL: https://github.com/apache/camel/pull/30703#discussion_r4674580812


##########
core/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerStaleRunTest.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.impl;
+
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.HttpResponseAware;
+import org.apache.camel.support.DefaultScheduledPollConsumerScheduler;
+import org.apache.camel.support.ScheduledPollConsumer;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A poll that is still running when the consumer is stopped and started again 
must not change the state of the
+ * restarted consumer.
+ */
+class ScheduledPollConsumerStaleRunTest extends ContextTestSupport {
+
+    private final CountDownLatch release = new CountDownLatch(1);
+    private Thread poller;
+    private Thread restarter;
+    private StaleConsumer consumer;
+
+    private static void awaitRelease(CountDownLatch latch) {
+        try {
+            if (!latch.await(20, TimeUnit.SECONDS)) {
+                throw new IllegalStateException("Not released");
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IllegalStateException(e);
+        }
+    }
+
+    private static class StaleConsumer extends ScheduledPollConsumer {
+
+        private volatile boolean forceReadyInPoll;
+        private volatile Exception exceptionToThrowOnPoll;
+        // set to hold the next poll (the remote call of the poll is in 
progress)
+        private volatile CountDownLatch pollHeld;
+        // set to hold the next call of addLastErrorDetail (after the 
generation check of the poll)
+        private volatile CountDownLatch detailHeld;
+        private volatile CountDownLatch release;
+
+        StaleConsumer(Endpoint endpoint) {
+            super(endpoint, null);
+        }
+
+        @Override
+        protected int poll() throws Exception {
+            CountDownLatch held = pollHeld;
+            if (held != null) {
+                pollHeld = null;
+                held.countDown();
+                awaitRelease(release);
+            }
+            if (forceReadyInPoll) {
+                // such as camel-aws2-sqs when its receive call returns
+                forceConsumerAsReady();
+            }
+            Exception e = exceptionToThrowOnPoll;
+            if (e != null) {
+                throw e;
+            }
+            return 0;
+        }
+
+        @Override
+        protected void addLastErrorDetail(String key, Object value) {
+            CountDownLatch held = detailHeld;
+            if (held != null) {
+                detailHeld = null;
+                held.countDown();
+                awaitRelease(release);
+            }
+            super.addLastErrorDetail(key, value);
+        }
+
+        Map<String, Object> lastErrorDetails() {
+            return getLastErrorDetails();
+        }
+    }
+
+    // a scheduler that holds its next unscheduleTask() call before it cancels 
the tasks
+    private static class HoldingScheduler extends 
DefaultScheduledPollConsumerScheduler {
+
+        private volatile CountDownLatch unscheduleHeld;
+        private volatile CountDownLatch release;
+
+        @Override
+        public void unscheduleTask() {
+            CountDownLatch held = unscheduleHeld;
+            if (held != null) {
+                unscheduleHeld = null;
+                held.countDown();
+                awaitRelease(release);
+            }
+            super.unscheduleTask();
+        }
+    }
+
+    private static class MyHttpException extends Exception implements 
HttpResponseAware {
+        MyHttpException() {
+            super("Service unavailable");
+        }
+
+        @Override
+        public int getHttpResponseCode() {
+            return 503;
+        }
+
+        @Override
+        public void setHttpResponseCode(int code) {
+            // noop
+        }
+
+        @Override
+        public String getHttpResponseStatus() {
+            return "Service Unavailable";
+        }
+
+        @Override
+        public void setHttpResponseStatus(String status) {
+            // noop
+        }
+    }
+
+    @AfterEach
+    void cleanUp() throws Exception {
+        release.countDown();
+        if (poller != null) {
+            poller.join(10000);
+        }
+        if (restarter != null) {
+            restarter.join(10000);
+        }

Review Comment:
   Applied in 7df24cfd4049: the cleanup now asserts that the poll and restart 
threads ended after each join, with the same messages as joinPoller(), so a 
hang fails the test instead of passing quietly. 
ScheduledPollConsumerStaleRunTest passes 10 runs in a row and the 
ScheduledPoll* tests pass (27 run, 0 failures).
   



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