Ensure that file is released back to pool when seek fails
Closes gh-9370
This commit is contained in:
@@ -18,7 +18,9 @@ package org.springframework.boot.loader.data;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -35,10 +37,20 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.mockito.internal.util.MockUtil;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.boot.loader.data.RandomAccessData.ResourceAccess;
|
||||
import org.springframework.boot.loader.data.RandomAccessDataFile.FilePool;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
/**
|
||||
* Tests for {@link RandomAccessDataFile}.
|
||||
@@ -309,4 +321,38 @@ public class RandomAccessDataFileTests {
|
||||
assertThat(queue.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seekFailuresDoNotPreventSubsequentReads() throws Exception {
|
||||
FilePool filePool = (FilePool) ReflectionTestUtils.getField(this.file,
|
||||
"filePool");
|
||||
FilePool spiedPool = spy(filePool);
|
||||
ReflectionTestUtils.setField(this.file, "filePool", spiedPool);
|
||||
willAnswer(new Answer<RandomAccessFile>() {
|
||||
|
||||
@Override
|
||||
public RandomAccessFile answer(InvocationOnMock invocation) throws Throwable {
|
||||
RandomAccessFile originalFile = (RandomAccessFile) invocation
|
||||
.callRealMethod();
|
||||
if (new MockUtil().isSpy(originalFile)) {
|
||||
return originalFile;
|
||||
}
|
||||
RandomAccessFile spiedFile = spy(originalFile);
|
||||
willThrow(new IOException("Seek failed")).given(spiedFile)
|
||||
.seek(anyLong());
|
||||
return spiedFile;
|
||||
}
|
||||
|
||||
}).given(spiedPool).acquire();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
try {
|
||||
this.file.getInputStream(ResourceAccess.PER_READ).read();
|
||||
fail("Read should fail due to exception from seek");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user