fanyang89 commented on code in PR #2141:
URL: https://github.com/apache/zookeeper/pull/2141#discussion_r1732030695


##########
zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnLogTest.java:
##########
@@ -296,6 +302,73 @@ public void testLogSizeLimit(@TempDir File tmpDir) throws 
Exception {
         }
     }
 
+    private void prepareTxnLogs(File dir, int n) throws IOException {
+        FileTxnLog.setTxnLogSizeLimit(1);
+        FileTxnLog log = new FileTxnLog(dir);
+        CreateRequest record = new CreateRequest(null, new byte[NODE_SIZE],
+                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag());
+        int zxid = 1;
+        for (int i = 0; i < n; i++) {
+            log.append(new Request(0, 0, 0, new TxnHeader(0, 0, zxid, 0, -1), 
record, zxid));
+            zxid++;
+            log.commit();
+        }
+        log.close();
+    }
+
+    public void testEmptyTxnLog(boolean clearTail) throws IOException {
+        // prepare a database with logs
+        File tmpDir = ClientBase.createTmpDir();
+        LOG.info("tmp dir: {}", tmpDir.getPath());
+        ClientBase.setupTestEnv();
+        prepareTxnLogs(tmpDir, 4);
+
+        // clear the log
+        List<File> files = Arrays.
+                stream(Objects.requireNonNull(tmpDir.listFiles((File f, String 
name) -> name.startsWith("log.")))).
+                sorted(Comparator.comparing(File::getName)).
+                collect(Collectors.toList());
+        File toClear;
+        if (clearTail) {
+            toClear = files.get(files.size() - 1);
+        } else {
+            toClear = files.get(files.size() - 2);
+        }
+        PrintWriter writer = new PrintWriter(toClear);
+        writer.close();
+        LOG.info("Txn log file {} cleared", toClear.getName());
+
+        // open txn log
+        boolean isEof = false;
+        try {
+            FileTxnLog.FileTxnIterator itr = new 
FileTxnLog.FileTxnIterator(tmpDir, 0x0, false);
+            while (itr.next()) {}
+        } catch (EOFException ex) {
+            isEof = true;
+        }
+
+        if (clearTail) {
+            FileTxnLog.FileTxnIterator itr = new 
FileTxnLog.FileTxnIterator(tmpDir, 0x0, false);
+            while (itr.next()) {}
+        } else {
+            assertTrue(isEof, "Mid txn log file empty should throw Exception");
+        }
+    }
+
+    @Test
+    public void testEmptyTailTxnLog() throws IOException {
+        long limit = FileTxnLog.getTxnLogSizeLimit();
+        testEmptyTxnLog(true);

Review Comment:
   No problem and revised



-- 
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: notifications-unsubscr...@zookeeper.apache.org

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

Reply via email to