@Test public void shouldLoadDataFromMoreFiles() throws IOException { byte[] value = new byte[] {1, 2}; byte[] newValue = new byte[] {1, 2, 3}; writer.changeInstanceValue(1, 2, value); writer.changeInstanceValue(2, 2, value); writer.changeInstanceValue(1, 3, newValue); writer.close(); writer = new FullSSDiscWriter(directoryPath); writer.changeInstanceView(1, 4); writer.close(); writer = new FullSSDiscWriter(directoryPath); ConsensusInstance[] instances = writer.load().toArray(new ConsensusInstance[0]); writer.close(); ConsensusInstance instance1 = instances[0]; ConsensusInstance instance2 = instances[1]; assertEquals(4, instance1.getView()); assertArrayEquals(newValue, instance1.getValue()); assertEquals(2, instance2.getView()); assertArrayEquals(value, instance2.getValue()); }
@Test public void shouldLoadCorruptedData() throws IOException { byte[] value = new byte[] {1, 2}; byte[] newValue = new byte[] {1, 2, 3}; writer.changeInstanceValue(1, 2, value); writer.changeInstanceValue(2, 2, value); writer.changeInstanceValue(1, 3, newValue); writer.changeInstanceView(1, 4); writer.close(); FileOutputStream stream = new FileOutputStream(directoryPath + "/sync.0.log", true); stream.write(new byte[] {1, 2, 3}); stream.close(); writer = new FullSSDiscWriter(directoryPath); ConsensusInstance[] instances = writer.load().toArray(new ConsensusInstance[0]); writer.close(); ConsensusInstance instance1 = instances[0]; ConsensusInstance instance2 = instances[1]; assertEquals(4, instance1.getView()); assertArrayEquals(newValue, instance1.getValue()); assertEquals(2, instance2.getView()); assertArrayEquals(value, instance2.getValue()); }
@Test public void shouldWriteOnInstanceValueChange() throws IOException { byte[] value = new byte[] {1, 2}; writer.changeInstanceValue(1, 2, value); ByteBuffer buffer = ByteBuffer.allocate(15); buffer.put((byte) 2); // type buffer.putInt(1); // id buffer.putInt(2); // view buffer.putInt(2); // value size buffer.put(value); // value String path = directoryPath + "/sync.0.log"; assertArrayEquals(readFile(path), buffer.array()); }