exceptionfactory commented on code in PR #9812:
URL: https://github.com/apache/nifi/pull/9812#discussion_r2011130703


##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/test/java/org/apache/nifi/processors/aws/kinesis/stream/record/TestAbstractKinesisRecordProcessor.java:
##########
@@ -72,7 +73,7 @@ public void setUp() {
 
         // default test fixture will try operations twice with very little 
wait in between
         fixture = new AbstractKinesisRecordProcessor(processSessionFactory, 
runner.getLogger(), "kinesis-test",
-                "endpoint-prefix", null, 10_000L, 1L, 2, DATE_TIME_FORMATTER) {
+                "endpoint-prefix", null, 10_000L, 1L, 2, DATE_TIME_FORMATTER, 
NoopRecordProcessorBlocker.create()) {

Review Comment:
   Can the `StandardRecordProcessorBlocker` be used here? It seems like the 
Noop instance is not required for standard behavior in this case.



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/test/java/org/apache/nifi/processors/aws/kinesis/stream/pause/TestStandardRecordProcessorBlocker.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.nifi.processors.aws.kinesis.stream.pause;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.function.Supplier;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestStandardRecordProcessorBlocker {
+
+    @Test
+    public void testBlockAndUnblock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+
+        recordProcessorBlocker.block();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitAwaited();
+        assertTrue(thread.isAlive());
+
+        recordProcessorBlocker.unblock();
+        blockerInspector.awaitFinished();
+    }
+
+    @Test
+    public void testNoBlock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+
+        recordProcessorBlocker.unblock();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitFinished();
+    }
+
+    @Test
+    public void testBlock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+        recordProcessorBlocker.block();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitAwaited();
+        assertTrue(thread.isAlive());
+
+        recordProcessorBlocker.unblock();
+    }
+
+    private static Runnable createRunnableWithInspector(final 
StandardRecordProcessorBlocker recordProcessorBlocker, TestThreadInspector 
blockerInspector) {
+        return () -> {
+            try {
+                blockerInspector.onPauseAwaited();
+                recordProcessorBlocker.await();
+            } catch (final InterruptedException e) {
+                throw new RuntimeException(e);
+            } finally {
+                blockerInspector.onPauseFinished();
+            }
+        };
+    }
+
+    private static class TestThreadInspector {
+        private boolean onPauseAwaited = false;
+        private boolean onPauseFinished = false;
+
+        public void onPauseAwaited() {
+            onPauseAwaited = true;
+        }
+
+        public void onPauseFinished() {
+            onPauseFinished = true;
+        }

Review Comment:
   It looks like these variables and methods should be updated to reflect the 
`Blocker` naming.



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/test/java/org/apache/nifi/processors/aws/kinesis/stream/pause/TestStandardRecordProcessorBlocker.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.nifi.processors.aws.kinesis.stream.pause;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.function.Supplier;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestStandardRecordProcessorBlocker {
+
+    @Test
+    public void testBlockAndUnblock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+
+        recordProcessorBlocker.block();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitAwaited();
+        assertTrue(thread.isAlive());
+
+        recordProcessorBlocker.unblock();
+        blockerInspector.awaitFinished();
+    }
+
+    @Test
+    public void testNoBlock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+
+        recordProcessorBlocker.unblock();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitFinished();
+    }
+
+    @Test
+    public void testBlock() {
+        final TestThreadInspector blockerInspector = new TestThreadInspector();
+
+        final StandardRecordProcessorBlocker recordProcessorBlocker = 
StandardRecordProcessorBlocker.create();
+        recordProcessorBlocker.block();
+        final Thread thread = new 
Thread(createRunnableWithInspector(recordProcessorBlocker, blockerInspector));
+        thread.start();
+
+        blockerInspector.awaitAwaited();
+        assertTrue(thread.isAlive());
+
+        recordProcessorBlocker.unblock();
+    }
+
+    private static Runnable createRunnableWithInspector(final 
StandardRecordProcessorBlocker recordProcessorBlocker, TestThreadInspector 
blockerInspector) {
+        return () -> {
+            try {
+                blockerInspector.onPauseAwaited();
+                recordProcessorBlocker.await();
+            } catch (final InterruptedException e) {
+                throw new RuntimeException(e);
+            } finally {
+                blockerInspector.onPauseFinished();
+            }
+        };
+    }
+
+    private static class TestThreadInspector {
+        private boolean onPauseAwaited = false;
+        private boolean onPauseFinished = false;
+
+        public void onPauseAwaited() {
+            onPauseAwaited = true;
+        }
+
+        public void onPauseFinished() {
+            onPauseFinished = true;
+        }
+
+        public void awaitAwaited() {

Review Comment:
   ```suggestion
           public void awaitStarted() {
   ```



##########
nifi-extension-bundles/nifi-aws-bundle/nifi-aws-processors/src/main/java/org/apache/nifi/processors/aws/kinesis/stream/pause/StandardRecordProcessorBlocker.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.nifi.processors.aws.kinesis.stream.pause;
+
+import java.util.concurrent.CountDownLatch;
+
+public class StandardRecordProcessorBlocker implements RecordProcessorBlocker {
+
+    private CountDownLatch blocker = new CountDownLatch(0);
+
+    public static StandardRecordProcessorBlocker create() {
+        return new StandardRecordProcessorBlocker();
+    }
+
+    protected StandardRecordProcessorBlocker() { }
+
+    public void await() throws InterruptedException {
+        blocker.await();

Review Comment:
   What happens if `unblock()` and `block()` get called while the 
RecordProcessor is waiting on this call, since the `CountDownLatch` instance 
would be changed in the `block()` method?
   
   The `CyclicBarrier` can be reset, but doesn't have all the semantics that 
seem to be required.



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