2 * Copyright 2017-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.storage.journal;
18 import org.junit.Test;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
24 * Persistent journal test base.
26 public abstract class PersistentJournalTest extends AbstractJournalTest {
27 protected PersistentJournalTest(int maxSegmentSize, int cacheSize) {
28 super(maxSegmentSize, cacheSize);
32 * Tests reading from a compacted journal.
35 public void testCompactAndRecover() throws Exception {
36 SegmentedJournal<TestEntry> journal = createJournal();
38 // Write three segments to the journal.
39 JournalWriter<TestEntry> writer = journal.writer();
40 for (int i = 0; i < entriesPerSegment * 3; i++) {
44 // Commit the entries and compact the first segment.
45 writer.commit(entriesPerSegment * 3);
46 journal.compact(entriesPerSegment + 1);
51 // Reopen the journal and create a reader.
52 journal = createJournal();
53 writer = journal.writer();
54 JournalReader<TestEntry> reader = journal.openReader(1, JournalReader.Mode.COMMITS);
57 writer.commit(entriesPerSegment * 3);
59 // Ensure the reader starts at the first physical index in the journal.
60 assertEquals(entriesPerSegment + 1, reader.getNextIndex());
61 assertEquals(reader.getFirstIndex(), reader.getNextIndex());
62 assertTrue(reader.hasNext());
63 assertEquals(entriesPerSegment + 1, reader.getNextIndex());
64 assertEquals(reader.getFirstIndex(), reader.getNextIndex());
65 assertEquals(entriesPerSegment + 1, reader.next().index());