Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
295 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ft/src/test/java/io/atomix/protocols/raft/storage/snapshot/AbstractSnapshotStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,86 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed 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 io.atomix.protocols.raft.storage.snapshot; | |||
|
|||
import io.atomix.protocols.raft.service.ServiceId; | |||
import io.atomix.time.WallClockTimestamp; | |||
import org.testng.annotations.Test; | |||
|
|||
import static org.testng.Assert.assertEquals; | |||
import static org.testng.Assert.assertNull; | |||
|
|||
/** | |||
* Snapshot store test. | |||
* | |||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | |||
*/ | |||
@Test | |||
public abstract class AbstractSnapshotStoreTest { | |||
|
|||
/** | |||
* Returns a new snapshot store. | |||
*/ | |||
protected abstract SnapshotStore createSnapshotStore(); | |||
|
|||
/** | |||
* Tests writing a snapshot. | |||
*/ | |||
public void testWriteSnapshotChunks() { | |||
SnapshotStore store = createSnapshotStore(); | |||
WallClockTimestamp timestamp = new WallClockTimestamp(); | |||
Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, timestamp); | |||
assertEquals(snapshot.serviceId(), ServiceId.from(1)); | |||
assertEquals(snapshot.index(), 2); | |||
assertEquals(snapshot.timestamp(), timestamp); | |||
|
|||
assertNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNull(store.getSnapshotByIndex(2)); | |||
|
|||
try (SnapshotWriter writer = snapshot.openWriter()) { | |||
writer.writeLong(10); | |||
} | |||
|
|||
assertNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNull(store.getSnapshotByIndex(2)); | |||
|
|||
try (SnapshotWriter writer = snapshot.openWriter()) { | |||
writer.writeLong(11); | |||
} | |||
|
|||
assertNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNull(store.getSnapshotByIndex(2)); | |||
|
|||
try (SnapshotWriter writer = snapshot.openWriter()) { | |||
writer.writeLong(12); | |||
} | |||
|
|||
assertNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNull(store.getSnapshotByIndex(2)); | |||
snapshot.complete(); | |||
|
|||
assertEquals(store.getSnapshotById(ServiceId.from(1)).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotById(ServiceId.from(1)).index(), 2); | |||
assertEquals(store.getSnapshotByIndex(2).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotByIndex(2).index(), 2); | |||
|
|||
try (SnapshotReader reader = store.getSnapshotById(ServiceId.from(1)).openReader()) { | |||
assertEquals(reader.readLong(), 10); | |||
assertEquals(reader.readLong(), 11); | |||
assertEquals(reader.readLong(), 12); | |||
} | |||
} | |||
|
|||
} |
151 changes: 151 additions & 0 deletions
151
...s/raft/src/test/java/io/atomix/protocols/raft/storage/snapshot/FileSnapshotStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,151 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed 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 io.atomix.protocols.raft.storage.snapshot; | |||
|
|||
import io.atomix.protocols.raft.service.ServiceId; | |||
import io.atomix.protocols.raft.storage.RaftStorage; | |||
import io.atomix.storage.StorageLevel; | |||
import io.atomix.time.WallClockTimestamp; | |||
import org.testng.annotations.AfterMethod; | |||
import org.testng.annotations.BeforeMethod; | |||
import org.testng.annotations.Test; | |||
|
|||
import java.io.File; | |||
import java.io.IOException; | |||
import java.nio.file.FileVisitResult; | |||
import java.nio.file.Files; | |||
import java.nio.file.Path; | |||
import java.nio.file.Paths; | |||
import java.nio.file.SimpleFileVisitor; | |||
import java.nio.file.attribute.BasicFileAttributes; | |||
import java.util.UUID; | |||
|
|||
import static org.testng.Assert.assertEquals; | |||
import static org.testng.Assert.assertNotNull; | |||
import static org.testng.Assert.assertNull; | |||
|
|||
/** | |||
* File snapshot store test. | |||
* | |||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | |||
*/ | |||
@Test | |||
public class FileSnapshotStoreTest extends AbstractSnapshotStoreTest { | |||
private String testId; | |||
|
|||
/** | |||
* Returns a new snapshot store. | |||
*/ | |||
protected SnapshotStore createSnapshotStore() { | |||
RaftStorage storage = RaftStorage.newBuilder() | |||
.withPrefix("test") | |||
.withDirectory(new File(String.format("target/test-logs/%s", testId))) | |||
.withStorageLevel(StorageLevel.DISK) | |||
.build(); | |||
return new SnapshotStore(storage); | |||
} | |||
|
|||
/** | |||
* Tests storing and loading snapshots. | |||
*/ | |||
public void testStoreLoadSnapshot() { | |||
SnapshotStore store = createSnapshotStore(); | |||
|
|||
Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp()); | |||
try (SnapshotWriter writer = snapshot.openWriter()) { | |||
writer.writeLong(10); | |||
} | |||
snapshot.complete(); | |||
assertNotNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNotNull(store.getSnapshotByIndex(2)); | |||
store.close(); | |||
|
|||
store = createSnapshotStore(); | |||
assertNotNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNotNull(store.getSnapshotByIndex(2)); | |||
assertEquals(store.getSnapshotById(ServiceId.from(1)).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotById(ServiceId.from(1)).index(), 2); | |||
assertEquals(store.getSnapshotByIndex(2).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotByIndex(2).index(), 2); | |||
|
|||
try (SnapshotReader reader = snapshot.openReader()) { | |||
assertEquals(reader.readLong(), 10); | |||
} | |||
} | |||
|
|||
/** | |||
* Tests persisting and loading snapshots. | |||
*/ | |||
public void testPersistLoadSnapshot() { | |||
SnapshotStore store = createSnapshotStore(); | |||
|
|||
Snapshot snapshot = store.newTemporarySnapshot(ServiceId.from(1), 2, new WallClockTimestamp()); | |||
try (SnapshotWriter writer = snapshot.openWriter()) { | |||
writer.writeLong(10); | |||
} | |||
|
|||
snapshot = snapshot.persist(); | |||
|
|||
assertNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNull(store.getSnapshotByIndex(2)); | |||
|
|||
snapshot.complete(); | |||
assertNotNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNotNull(store.getSnapshotByIndex(2)); | |||
|
|||
try (SnapshotReader reader = snapshot.openReader()) { | |||
assertEquals(reader.readLong(), 10); | |||
} | |||
|
|||
store.close(); | |||
|
|||
store = createSnapshotStore(); | |||
assertNotNull(store.getSnapshotById(ServiceId.from(1))); | |||
assertNotNull(store.getSnapshotByIndex(2)); | |||
assertEquals(store.getSnapshotById(ServiceId.from(1)).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotById(ServiceId.from(1)).index(), 2); | |||
assertEquals(store.getSnapshotByIndex(2).serviceId(), ServiceId.from(1)); | |||
assertEquals(store.getSnapshotByIndex(2).index(), 2); | |||
|
|||
snapshot = store.getSnapshotById(ServiceId.from(1)); | |||
try (SnapshotReader reader = snapshot.openReader()) { | |||
assertEquals(reader.readLong(), 10); | |||
} | |||
} | |||
|
|||
@BeforeMethod | |||
@AfterMethod | |||
protected void cleanupStorage() throws IOException { | |||
Path directory = Paths.get("target/test-logs/"); | |||
if (Files.exists(directory)) { | |||
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { | |||
@Override | |||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |||
Files.delete(file); | |||
return FileVisitResult.CONTINUE; | |||
} | |||
|
|||
@Override | |||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | |||
Files.delete(dir); | |||
return FileVisitResult.CONTINUE; | |||
} | |||
}); | |||
} | |||
testId = UUID.randomUUID().toString(); | |||
} | |||
|
|||
} |
41 changes: 41 additions & 0 deletions
41
...raft/src/test/java/io/atomix/protocols/raft/storage/snapshot/MemorySnapshotStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
/* | |||
* Copyright 2015 the original author or authors. | |||
* | |||
* Licensed 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 io.atomix.protocols.raft.storage.snapshot; | |||
|
|||
import io.atomix.protocols.raft.storage.RaftStorage; | |||
import io.atomix.storage.StorageLevel; | |||
import org.testng.annotations.Test; | |||
|
|||
/** | |||
* Memory snapshot store test. | |||
* | |||
* @author <a href="http://github.com/kuujo>Jordan Halterman</a> | |||
*/ | |||
@Test | |||
public class MemorySnapshotStoreTest extends AbstractSnapshotStoreTest { | |||
|
|||
/** | |||
* Returns a new snapshot store. | |||
*/ | |||
protected SnapshotStore createSnapshotStore() { | |||
RaftStorage storage = RaftStorage.newBuilder() | |||
.withPrefix("test") | |||
.withStorageLevel(StorageLevel.MEMORY) | |||
.build(); | |||
return new SnapshotStore(storage); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters