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


##########
nifi-nar-bundles/nifi-questdb-bundle/nifi-questdb/src/test/java/org/apache/nifi/questdb/embedded/EmbeddedDatabaseManagerTest.java:
##########
@@ -0,0 +1,397 @@
+/*
+ * 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.questdb.embedded;
+
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.nifi.questdb.Client;
+import org.apache.nifi.questdb.DatabaseException;
+import org.apache.nifi.questdb.DatabaseManager;
+import org.apache.nifi.questdb.mapping.RequestMapping;
+import org.apache.nifi.questdb.rollover.RolloverStrategy;
+import org.apache.nifi.questdb.util.Event;
+import org.apache.nifi.questdb.util.QuestDbTestUtil;
+import org.apache.nifi.util.file.FileUtils;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.StreamSupport;
+
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.CREATE_EVENT2_TABLE;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.CREATE_EVENT_TABLE;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.EVENT2_TABLE_NAME;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.EVENT_TABLE_NAME;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.SELECT_QUERY;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.getRandomTestData;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.getTestData;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class EmbeddedDatabaseManagerTest extends EmbeddedQuestDbTest {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(EmbeddedDatabaseManagerTest.class);
+    private static final int DAYS_TO_KEEP_EVENT = 1;
+    private static final String NON_EXISTING_PLACE = SystemUtils.IS_OS_WINDOWS 
? "T:/nonExistingPlace" : "/nonExistingPlace";
+
+    @Test
+    public void testAcquiringWithoutInitialization() {
+        final EmbeddedDatabaseManager testSubject = new 
EmbeddedDatabaseManager(new SimpleEmbeddedDatabaseManagerContext());
+        assertThrows(IllegalStateException.class, () -> 
testSubject.acquireClient());
+    }
+
+    @Test
+    public void testHappyPath() throws DatabaseException {
+        final List<Event> testData = getTestData();
+        final DatabaseManager testSubject = getTestSubject();
+        assertDatabaseFolderIsNotEmpty();
+
+        final Client client = testSubject.acquireClient();
+
+        client.insert(EVENT_TABLE_NAME, 
QuestDbTestUtil.getEventTableDataSource(testData));
+        final List<Event> result = client.query(SELECT_QUERY, 
RequestMapping.getResultProcessor(QuestDbTestUtil.EVENT_TABLE_REQUEST_MAPPING));
+
+        assertIterableEquals(testData, result);
+
+        testSubject.close();
+
+        // Even if the client itself is not connected, manager prevents client 
to reach database after closing
+        assertFalse(client.query(SELECT_QUERY, 
RequestMapping.getResultProcessor(QuestDbTestUtil.EVENT_TABLE_REQUEST_MAPPING)).iterator().hasNext());
+    }
+
+    @Test
+    public void testRollover() throws DatabaseException, InterruptedException {
+        final List<Event> testData = new ArrayList<>();
+        testData.add(new Event(Instant.now().minus((DAYS_TO_KEEP_EVENT + 1), 
ChronoUnit.DAYS), "A", 1));
+        testData.add(new Event(Instant.now(), "B", 2));
+        final DatabaseManager testSubject = getTestSubject();
+
+        final Client client = testSubject.acquireClient();
+        client.insert(EVENT_TABLE_NAME, 
QuestDbTestUtil.getEventTableDataSource(testData));
+
+        // The rollover runs in every 5 seconds
+        Thread.sleep(TimeUnit.SECONDS.toMillis(6));
+
+        final List<Event> result = client.query(SELECT_QUERY, 
RequestMapping.getResultProcessor(QuestDbTestUtil.EVENT_TABLE_REQUEST_MAPPING));
+        testSubject.close();
+
+        assertEquals(1, result.size());

Review Comment:
   When building, the module, this test method fails intermittently. It seems 
like the `Thread.sleep()` approach is not a strong enough guarantee of rollover 
completion. It may be necessary to do some additional checking on the directory 
content itself to determine if a rollover has occurred. If all else fails, it 
would be better to remove the test method, as opposed to having a test that 
fails intermittently.
   
   ```
   [ERROR] Tests run: 13, Failures: 1, Errors: 0, Skipped: 1, Time elapsed: 
18.24 s <<< FAILURE! -- in 
org.apache.nifi.questdb.embedded.EmbeddedDatabaseManagerTest
   [ERROR] 
org.apache.nifi.questdb.embedded.EmbeddedDatabaseManagerTest.testRollover -- 
Time elapsed: 7.888 s <<< FAILURE!
   org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
        at 
org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at 
org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at 
org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at 
org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
        at 
org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:527)
        at 
org.apache.nifi.questdb.embedded.EmbeddedDatabaseManagerTest.testRollover(EmbeddedDatabaseManagerTest.java:109)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
   ```



##########
nifi-nar-bundles/nifi-questdb-bundle/nifi-questdb/src/test/java/org/apache/nifi/questdb/embedded/EmbeddedDatabaseManagerTest.java:
##########
@@ -0,0 +1,397 @@
+/*
+ * 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.questdb.embedded;
+
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.nifi.questdb.Client;
+import org.apache.nifi.questdb.DatabaseException;
+import org.apache.nifi.questdb.DatabaseManager;
+import org.apache.nifi.questdb.mapping.RequestMapping;
+import org.apache.nifi.questdb.rollover.RolloverStrategy;
+import org.apache.nifi.questdb.util.Event;
+import org.apache.nifi.questdb.util.QuestDbTestUtil;
+import org.apache.nifi.util.file.FileUtils;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.StreamSupport;
+
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.CREATE_EVENT2_TABLE;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.CREATE_EVENT_TABLE;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.EVENT2_TABLE_NAME;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.EVENT_TABLE_NAME;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.SELECT_QUERY;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.getRandomTestData;
+import static org.apache.nifi.questdb.util.QuestDbTestUtil.getTestData;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertIterableEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class EmbeddedDatabaseManagerTest extends EmbeddedQuestDbTest {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(EmbeddedDatabaseManagerTest.class);
+    private static final int DAYS_TO_KEEP_EVENT = 1;
+    private static final String NON_EXISTING_PLACE = SystemUtils.IS_OS_WINDOWS 
? "T:/nonExistingPlace" : "/nonExistingPlace";
+
+    @Test
+    public void testAcquiringWithoutInitialization() {
+        final EmbeddedDatabaseManager testSubject = new 
EmbeddedDatabaseManager(new SimpleEmbeddedDatabaseManagerContext());
+        assertThrows(IllegalStateException.class, () -> 
testSubject.acquireClient());
+    }
+
+    @Test
+    public void testHappyPath() throws DatabaseException {
+        final List<Event> testData = getTestData();
+        final DatabaseManager testSubject = getTestSubject();
+        assertDatabaseFolderIsNotEmpty();
+
+        final Client client = testSubject.acquireClient();
+
+        client.insert(EVENT_TABLE_NAME, 
QuestDbTestUtil.getEventTableDataSource(testData));
+        final List<Event> result = client.query(SELECT_QUERY, 
RequestMapping.getResultProcessor(QuestDbTestUtil.EVENT_TABLE_REQUEST_MAPPING));
+
+        assertIterableEquals(testData, result);
+
+        testSubject.close();
+
+        // Even if the client itself is not connected, manager prevents client 
to reach database after closing
+        assertFalse(client.query(SELECT_QUERY, 
RequestMapping.getResultProcessor(QuestDbTestUtil.EVENT_TABLE_REQUEST_MAPPING)).iterator().hasNext());
+    }
+
+    @Test
+    public void testRollover() throws DatabaseException, InterruptedException {
+        final List<Event> testData = new ArrayList<>();
+        testData.add(new Event(Instant.now().minus((DAYS_TO_KEEP_EVENT + 1), 
ChronoUnit.DAYS), "A", 1));
+        testData.add(new Event(Instant.now(), "B", 2));
+        final DatabaseManager testSubject = getTestSubject();
+
+        final Client client = testSubject.acquireClient();
+        client.insert(EVENT_TABLE_NAME, 
QuestDbTestUtil.getEventTableDataSource(testData));
+
+        // The rollover runs in every 5 seconds
+        Thread.sleep(TimeUnit.SECONDS.toMillis(6));

Review Comment:
   This is a long time to sleep for a unit test, by adjusting the rollover 
time, can this value be reduced?



-- 
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: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to