mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-25 07:27:18 +01:00
PersistenceFileWrite: Truncate file before writing
This commit is contained in:
parent
bf57524671
commit
f7443b7676
2 changed files with 36 additions and 1 deletions
|
@ -34,7 +34,12 @@ public class PersistenceFileWriter {
|
||||||
|
|
||||||
public CompletableFuture<Void> write(byte[] data) {
|
public CompletableFuture<Void> write(byte[] data) {
|
||||||
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
|
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
|
||||||
scheduleAsyncWrite(data, 0, data.length, completableFuture);
|
asyncWriter.truncate()
|
||||||
|
.thenRun(() -> scheduleAsyncWrite(data, 0, data.length, completableFuture))
|
||||||
|
.exceptionally(throwable -> {
|
||||||
|
completableFuture.completeExceptionally(throwable);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
return completableFuture;
|
return completableFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
|
|
||||||
package bisq.persistence;
|
package bisq.persistence;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
@ -32,9 +35,13 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import static java.util.concurrent.CompletableFuture.completedFuture;
|
import static java.util.concurrent.CompletableFuture.completedFuture;
|
||||||
|
import static java.util.concurrent.CompletableFuture.failedFuture;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
@ -56,8 +63,29 @@ public class PersistenceFileWriterTests {
|
||||||
writeRequestScheduler.shutdownNow();
|
writeRequestScheduler.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failTruncation() throws ExecutionException, InterruptedException, TimeoutException {
|
||||||
|
var ioException = new IOException("Truncation failed.");
|
||||||
|
doReturn(failedFuture(ioException))
|
||||||
|
.when(asyncWriter).truncate();
|
||||||
|
|
||||||
|
CountDownLatch exceptionThrownLatch = new CountDownLatch(1);
|
||||||
|
fileWriter.write(DATA)
|
||||||
|
.exceptionally(throwable -> {
|
||||||
|
assertThat(throwable.getCause(), is(ioException));
|
||||||
|
exceptionThrownLatch.countDown();
|
||||||
|
return null;
|
||||||
|
})
|
||||||
|
.get(30, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
assertThat(exceptionThrownLatch.getCount(), is(0L));
|
||||||
|
verify(asyncWriter, times(1)).truncate();
|
||||||
|
verify(asyncWriter, never()).write(any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void writeInOneGo() throws InterruptedException, ExecutionException, TimeoutException {
|
void writeInOneGo() throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
doReturn(completedFuture(null)).when(asyncWriter).truncate();
|
||||||
doReturn(completedFuture(DATA.length))
|
doReturn(completedFuture(DATA.length))
|
||||||
.when(asyncWriter).write(any(), anyInt());
|
.when(asyncWriter).write(any(), anyInt());
|
||||||
|
|
||||||
|
@ -69,6 +97,7 @@ public class PersistenceFileWriterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void writeInTwoPhases() throws InterruptedException, ExecutionException, TimeoutException {
|
void writeInTwoPhases() throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
doReturn(completedFuture(null)).when(asyncWriter).truncate();
|
||||||
doReturn(completedFuture(25), completedFuture(75))
|
doReturn(completedFuture(25), completedFuture(75))
|
||||||
.when(asyncWriter).write(any(), anyInt());
|
.when(asyncWriter).write(any(), anyInt());
|
||||||
|
|
||||||
|
@ -80,6 +109,7 @@ public class PersistenceFileWriterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void writeInFivePhases() throws InterruptedException, ExecutionException, TimeoutException {
|
void writeInFivePhases() throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
doReturn(completedFuture(null)).when(asyncWriter).truncate();
|
||||||
doReturn(completedFuture(10), completedFuture(20),
|
doReturn(completedFuture(10), completedFuture(20),
|
||||||
completedFuture(30), completedFuture(15),
|
completedFuture(30), completedFuture(15),
|
||||||
completedFuture(25))
|
completedFuture(25))
|
||||||
|
|
Loading…
Add table
Reference in a new issue