|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.core.internal.async;
|
17 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.FileNotFoundException; |
18 | 24 | import java.io.IOException;
|
19 | 25 | import java.nio.ByteBuffer;
|
20 |
| -import java.nio.channels.AsynchronousFileChannel; |
| 26 | +import java.nio.charset.StandardCharsets; |
21 | 27 | import java.nio.file.Files;
|
| 28 | +import java.nio.file.NoSuchFileException; |
22 | 29 | import java.nio.file.Path;
|
23 |
| -import java.nio.file.StandardOpenOption; |
| 30 | +import java.nio.file.attribute.FileTime; |
| 31 | +import java.time.Instant; |
24 | 32 | import java.util.concurrent.CompletableFuture;
|
25 | 33 | import java.util.concurrent.ExecutionException;
|
| 34 | +import java.util.concurrent.Semaphore; |
| 35 | +import java.util.concurrent.ThreadLocalRandom; |
26 | 36 | import java.util.concurrent.TimeUnit;
|
27 | 37 | import java.util.concurrent.TimeoutException;
|
28 |
| -import java.util.concurrent.atomic.AtomicLong; |
29 |
| -import org.junit.AfterClass; |
30 |
| -import org.junit.BeforeClass; |
| 38 | +import org.junit.After; |
| 39 | +import org.junit.Before; |
31 | 40 | import org.junit.Test;
|
32 | 41 | import org.reactivestreams.Subscriber;
|
33 | 42 | import org.reactivestreams.Subscription;
|
34 | 43 | import software.amazon.awssdk.core.async.AsyncRequestBody;
|
35 | 44 | import software.amazon.awssdk.testutils.RandomTempFile;
|
| 45 | +import software.amazon.awssdk.utils.BinaryUtils; |
36 | 46 |
|
37 | 47 | public class FileAsyncRequestBodyTest {
|
38 | 48 | private static final long MiB = 1024 * 1024;
|
39 | 49 | private static final long TEST_FILE_SIZE = 10 * MiB;
|
40 | 50 | private static Path testFile;
|
41 | 51 |
|
42 |
| - @BeforeClass |
43 |
| - public static void setup() throws IOException { |
| 52 | + @Before |
| 53 | + public void setup() throws IOException { |
44 | 54 | testFile = new RandomTempFile(TEST_FILE_SIZE).toPath();
|
45 | 55 | }
|
46 | 56 |
|
47 |
| - @AfterClass |
48 |
| - public static void teardown() throws IOException { |
49 |
| - Files.delete(testFile); |
| 57 | + @After |
| 58 | + public void teardown() throws IOException { |
| 59 | + try { |
| 60 | + Files.delete(testFile); |
| 61 | + } catch (NoSuchFileException e) { |
| 62 | + // ignore |
| 63 | + } |
50 | 64 | }
|
51 | 65 |
|
52 | 66 | // If we issue just enough requests to read the file entirely but not more (to go past EOF), we should still receive
|
@@ -92,4 +106,154 @@ public void onComplete() {
|
92 | 106 |
|
93 | 107 | completed.get(5, TimeUnit.SECONDS);
|
94 | 108 | }
|
| 109 | + |
| 110 | + @Test |
| 111 | + public void changingFile_fileGetsShorterThanAlreadyRead_failsBecauseTooShort() throws Exception { |
| 112 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 113 | + .path(testFile) |
| 114 | + .build(); |
| 115 | + |
| 116 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 117 | + |
| 118 | + // Start reading file |
| 119 | + asyncRequestBody.subscribe(subscriber); |
| 120 | + subscriber.sub.request(1); |
| 121 | + assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 122 | + |
| 123 | + // Change the file to be shorter than the amount read so far |
| 124 | + Files.write(testFile, "Hello".getBytes(StandardCharsets.UTF_8)); |
| 125 | + |
| 126 | + // Finishing reading the file |
| 127 | + subscriber.sub.request(Long.MAX_VALUE); |
| 128 | + |
| 129 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 130 | + .hasCauseInstanceOf(IOException.class); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void changingFile_fileGetsShorterThanExistingLength_failsBecauseTooShort() throws Exception { |
| 135 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 136 | + .path(testFile) |
| 137 | + .build(); |
| 138 | + |
| 139 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 140 | + |
| 141 | + // Start reading file |
| 142 | + asyncRequestBody.subscribe(subscriber); |
| 143 | + subscriber.sub.request(1); |
| 144 | + assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 145 | + |
| 146 | + // Change the file to be 1 byte shorter than when we started |
| 147 | + int currentSize = Math.toIntExact(Files.size(testFile)); |
| 148 | + byte[] slightlyShorterFileContent = new byte[currentSize - 1]; |
| 149 | + ThreadLocalRandom.current().nextBytes(slightlyShorterFileContent); |
| 150 | + Files.write(testFile, slightlyShorterFileContent); |
| 151 | + |
| 152 | + // Finishing reading the file |
| 153 | + subscriber.sub.request(Long.MAX_VALUE); |
| 154 | + |
| 155 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 156 | + .hasCauseInstanceOf(IOException.class); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void changingFile_fileGetsLongerThanExistingLength_failsBecauseTooLong() throws Exception { |
| 161 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 162 | + .path(testFile) |
| 163 | + .build(); |
| 164 | + |
| 165 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 166 | + |
| 167 | + // Start reading file |
| 168 | + asyncRequestBody.subscribe(subscriber); |
| 169 | + subscriber.sub.request(1); |
| 170 | + assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 171 | + |
| 172 | + // Change the file to be 1 byte longer than when we started |
| 173 | + int currentSize = Math.toIntExact(Files.size(testFile)); |
| 174 | + byte[] slightlyLongerFileContent = new byte[currentSize + 1]; |
| 175 | + ThreadLocalRandom.current().nextBytes(slightlyLongerFileContent); |
| 176 | + Files.write(testFile, slightlyLongerFileContent); |
| 177 | + |
| 178 | + // Finishing reading the file |
| 179 | + subscriber.sub.request(Long.MAX_VALUE); |
| 180 | + |
| 181 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 182 | + .hasCauseInstanceOf(IOException.class); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void changingFile_fileGetsTouched_failsBecauseUpdatedModificationTime() throws Exception { |
| 187 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 188 | + .path(testFile) |
| 189 | + .build(); |
| 190 | + |
| 191 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 192 | + |
| 193 | + // Start reading file |
| 194 | + asyncRequestBody.subscribe(subscriber); |
| 195 | + subscriber.sub.request(1); |
| 196 | + assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 197 | + |
| 198 | + // Change the file to be updated |
| 199 | + Thread.sleep(1_000); // Wait for 1 second so that we are definitely in a different second than when the file was created |
| 200 | + Files.setLastModifiedTime(testFile, FileTime.from(Instant.now())); |
| 201 | + |
| 202 | + // Finishing reading the file |
| 203 | + subscriber.sub.request(Long.MAX_VALUE); |
| 204 | + |
| 205 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 206 | + .hasCauseInstanceOf(IOException.class); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void changingFile_fileGetsDeleted_failsBecauseDeleted() throws Exception { |
| 211 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 212 | + .path(testFile) |
| 213 | + .build(); |
| 214 | + |
| 215 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 216 | + |
| 217 | + // Start reading file |
| 218 | + asyncRequestBody.subscribe(subscriber); |
| 219 | + subscriber.sub.request(1); |
| 220 | + assertTrue(subscriber.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 221 | + |
| 222 | + // Delete the file |
| 223 | + Files.delete(testFile); |
| 224 | + |
| 225 | + // Finishing reading the file |
| 226 | + subscriber.sub.request(Long.MAX_VALUE); |
| 227 | + |
| 228 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 229 | + .hasCauseInstanceOf(IOException.class); |
| 230 | + } |
| 231 | + |
| 232 | + private static class ControllableSubscriber implements Subscriber<ByteBuffer> { |
| 233 | + private final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 234 | + private final CompletableFuture<Void> completed = new CompletableFuture<>(); |
| 235 | + private final Semaphore onNextSemaphore = new Semaphore(0); |
| 236 | + private Subscription sub; |
| 237 | + |
| 238 | + @Override |
| 239 | + public void onSubscribe(Subscription subscription) { |
| 240 | + this.sub = subscription; |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public void onNext(ByteBuffer byteBuffer) { |
| 245 | + invokeSafely(() -> output.write(BinaryUtils.copyBytesFrom(byteBuffer))); |
| 246 | + onNextSemaphore.release(); |
| 247 | + } |
| 248 | + |
| 249 | + @Override |
| 250 | + public void onError(Throwable throwable) { |
| 251 | + completed.completeExceptionally(throwable); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public void onComplete() { |
| 256 | + completed.complete(null); |
| 257 | + } |
| 258 | + } |
95 | 259 | }
|
0 commit comments