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


##########
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:
   This is an answer for both this and the previous one. 
   
   In general I would lean toward to use something like a latch, which 
guarantees the end of the background task. Due to the nature of the test case I 
could not use a spy for this, and I rely on waiting. As I left some gap between 
the roundtrip and wait time, I did not have issue with it so far, but based on 
your concerns I proposed a more refined way to do this. The fundamentals are 
the same: the test thread waits, but it polls multiple times to shorten the 
usual test duration and now it has 2 sec of gaps between the scheduled round 
trips and the waiting times. In case 3 seconds is not enough for the positive 
result I assume we have an issue indeed. Either with the test or the environment



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