Madan Jampani | 778f7ad | 2014-11-05 22:46:15 -0800 | [diff] [blame^] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.nio.file.Files; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import net.kuujo.copycat.internal.log.OperationEntry; |
| 9 | import net.kuujo.copycat.log.Entry; |
| 10 | import net.kuujo.copycat.log.Log; |
| 11 | |
| 12 | import org.junit.After; |
| 13 | import org.junit.Assert; |
| 14 | import org.junit.Before; |
| 15 | import org.junit.Test; |
| 16 | import org.onlab.onos.store.serializers.StoreSerializer; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | |
| 20 | /** |
| 21 | * Test the MapDBLog implementation. |
| 22 | */ |
| 23 | public class MapDBLogTest { |
| 24 | |
| 25 | private static final String DB_FILE_NAME = "mapdbTest"; |
| 26 | private static final StoreSerializer SERIALIZER = ClusterMessagingProtocol.SERIALIZER; |
| 27 | private static final Entry TEST_ENTRY1 = new OperationEntry(1, "test1"); |
| 28 | private static final Entry TEST_ENTRY2 = new OperationEntry(2, "test12"); |
| 29 | private static final Entry TEST_ENTRY3 = new OperationEntry(3, "test123"); |
| 30 | private static final Entry TEST_ENTRY4 = new OperationEntry(4, "test1234"); |
| 31 | |
| 32 | private static final Entry TEST_SNAPSHOT_ENTRY = new OperationEntry(5, "snapshot"); |
| 33 | |
| 34 | private static final long TEST_ENTRY1_SIZE = SERIALIZER.encode(TEST_ENTRY1).length; |
| 35 | private static final long TEST_ENTRY2_SIZE = SERIALIZER.encode(TEST_ENTRY2).length; |
| 36 | private static final long TEST_ENTRY3_SIZE = SERIALIZER.encode(TEST_ENTRY3).length; |
| 37 | private static final long TEST_ENTRY4_SIZE = SERIALIZER.encode(TEST_ENTRY4).length; |
| 38 | |
| 39 | private static final long TEST_SNAPSHOT_ENTRY_SIZE = SERIALIZER.encode(TEST_SNAPSHOT_ENTRY).length; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | } |
| 44 | |
| 45 | @After |
| 46 | public void tearDown() throws Exception { |
| 47 | Files.deleteIfExists(new File(DB_FILE_NAME).toPath()); |
| 48 | } |
| 49 | |
| 50 | @Test(expected = IllegalStateException.class) |
| 51 | public void testAssertOpen() { |
| 52 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 53 | log.size(); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testAppendEntry() throws IOException { |
| 58 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 59 | log.open(); |
| 60 | log.appendEntry(TEST_ENTRY1); |
| 61 | OperationEntry first = log.firstEntry(); |
| 62 | OperationEntry last = log.lastEntry(); |
| 63 | new EqualsTester() |
| 64 | .addEqualityGroup(first, last, TEST_ENTRY1) |
| 65 | .testEquals(); |
| 66 | Assert.assertEquals(TEST_ENTRY1_SIZE, log.size()); |
| 67 | Assert.assertEquals(1, log.firstIndex()); |
| 68 | Assert.assertEquals(1, log.lastIndex()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testAppendEntries() throws IOException { |
| 73 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 74 | log.open(); |
| 75 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3); |
| 76 | OperationEntry first = log.firstEntry(); |
| 77 | OperationEntry last = log.lastEntry(); |
| 78 | new EqualsTester() |
| 79 | .addEqualityGroup(first, TEST_ENTRY1) |
| 80 | .addEqualityGroup(last, TEST_ENTRY3) |
| 81 | .testEquals(); |
| 82 | Assert.assertEquals(TEST_ENTRY1_SIZE + TEST_ENTRY2_SIZE, TEST_ENTRY3_SIZE, log.size()); |
| 83 | Assert.assertEquals(1, log.firstIndex()); |
| 84 | Assert.assertEquals(3, log.lastIndex()); |
| 85 | Assert.assertTrue(log.containsEntry(1)); |
| 86 | Assert.assertTrue(log.containsEntry(2)); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testDelete() throws IOException { |
| 91 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 92 | log.open(); |
| 93 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2); |
| 94 | log.delete(); |
| 95 | Assert.assertEquals(0, log.size()); |
| 96 | Assert.assertTrue(log.isEmpty()); |
| 97 | Assert.assertEquals(0, log.firstIndex()); |
| 98 | Assert.assertNull(log.firstEntry()); |
| 99 | Assert.assertEquals(0, log.lastIndex()); |
| 100 | Assert.assertNull(log.lastEntry()); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testGetEntries() throws IOException { |
| 105 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 106 | log.open(); |
| 107 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3, TEST_ENTRY4); |
| 108 | Assert.assertEquals( |
| 109 | TEST_ENTRY1_SIZE + |
| 110 | TEST_ENTRY2_SIZE + |
| 111 | TEST_ENTRY3_SIZE + |
| 112 | TEST_ENTRY4_SIZE, log.size()); |
| 113 | |
| 114 | List<Entry> entries = log.getEntries(2, 3); |
| 115 | new EqualsTester() |
| 116 | .addEqualityGroup(log.getEntry(4), TEST_ENTRY4) |
| 117 | .addEqualityGroup(entries.get(0), TEST_ENTRY2) |
| 118 | .addEqualityGroup(entries.get(1), TEST_ENTRY3) |
| 119 | .testEquals(); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testRemoveAfter() throws IOException { |
| 124 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 125 | log.open(); |
| 126 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3, TEST_ENTRY4); |
| 127 | log.removeAfter(1); |
| 128 | Assert.assertEquals(TEST_ENTRY1_SIZE, log.size()); |
| 129 | new EqualsTester() |
| 130 | .addEqualityGroup(log.firstEntry(), log.lastEntry(), TEST_ENTRY1) |
| 131 | .testEquals(); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testAddAfterRemove() throws IOException { |
| 136 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 137 | log.open(); |
| 138 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3, TEST_ENTRY4); |
| 139 | log.removeAfter(1); |
| 140 | log.appendEntry(TEST_ENTRY4); |
| 141 | Assert.assertEquals(TEST_ENTRY1_SIZE + TEST_ENTRY4_SIZE, log.size()); |
| 142 | new EqualsTester() |
| 143 | .addEqualityGroup(log.firstEntry(), TEST_ENTRY1) |
| 144 | .addEqualityGroup(log.lastEntry(), TEST_ENTRY4) |
| 145 | .addEqualityGroup(log.size(), TEST_ENTRY1_SIZE + TEST_ENTRY4_SIZE) |
| 146 | .testEquals(); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testClose() throws IOException { |
| 151 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 152 | Assert.assertFalse(log.isOpen()); |
| 153 | log.open(); |
| 154 | Assert.assertTrue(log.isOpen()); |
| 155 | log.close(); |
| 156 | Assert.assertFalse(log.isOpen()); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testReopen() throws IOException { |
| 161 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 162 | log.open(); |
| 163 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3, TEST_ENTRY4); |
| 164 | log.close(); |
| 165 | log.open(); |
| 166 | |
| 167 | new EqualsTester() |
| 168 | .addEqualityGroup(log.firstEntry(), TEST_ENTRY1) |
| 169 | .addEqualityGroup(log.getEntry(2), TEST_ENTRY2) |
| 170 | .addEqualityGroup(log.lastEntry(), TEST_ENTRY4) |
| 171 | .addEqualityGroup(log.size(), |
| 172 | TEST_ENTRY1_SIZE + |
| 173 | TEST_ENTRY2_SIZE + |
| 174 | TEST_ENTRY3_SIZE + |
| 175 | TEST_ENTRY4_SIZE) |
| 176 | .testEquals(); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testCompact() throws IOException { |
| 181 | Log log = new MapDBLog(new File(DB_FILE_NAME), SERIALIZER); |
| 182 | log.open(); |
| 183 | log.appendEntries(TEST_ENTRY1, TEST_ENTRY2, TEST_ENTRY3, TEST_ENTRY4); |
| 184 | log.compact(3, TEST_SNAPSHOT_ENTRY); |
| 185 | new EqualsTester() |
| 186 | .addEqualityGroup(log.firstEntry(), TEST_SNAPSHOT_ENTRY) |
| 187 | .addEqualityGroup(log.lastEntry(), TEST_ENTRY4) |
| 188 | .addEqualityGroup(log.size(), |
| 189 | TEST_SNAPSHOT_ENTRY_SIZE + |
| 190 | TEST_ENTRY4_SIZE) |
| 191 | .testEquals(); |
| 192 | } |
| 193 | } |