|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
19 | 19 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 22 | import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; |
22 | 23 |
|
|
30 | 31 | import java.nio.file.Path; |
31 | 32 | import java.nio.file.attribute.FileTime; |
32 | 33 | import java.time.Instant; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
33 | 36 | import java.util.concurrent.CompletableFuture; |
34 | 37 | import java.util.concurrent.Semaphore; |
35 | 38 | import java.util.concurrent.ThreadLocalRandom; |
@@ -208,6 +211,57 @@ public void changingFile_fileGetsTouched_failsBecauseUpdatedModificationTime() t |
208 | 211 | .hasCauseInstanceOf(SdkClientException.class); |
209 | 212 | } |
210 | 213 |
|
| 214 | + @Test |
| 215 | + public void preset_modifiedTime_failsBecauseUpdatedModificationTime() throws Exception { |
| 216 | + FileTime initialModifiedTime = Files.getLastModifiedTime(testFile); |
| 217 | + // Change the file to be updated |
| 218 | + Thread.sleep(1_000); // Wait for 1 second so that we are definitely in a different second than when the file was created |
| 219 | + Files.setLastModifiedTime(testFile, FileTime.from(Instant.now())); |
| 220 | + |
| 221 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 222 | + .path(testFile) |
| 223 | + .modifiedTimeAtStart(initialModifiedTime) |
| 224 | + .build(); |
| 225 | + ControllableSubscriber subscriber = new ControllableSubscriber(); |
| 226 | + |
| 227 | + // Start reading file |
| 228 | + asyncRequestBody.subscribe(subscriber); |
| 229 | + subscriber.sub.request(Long.MAX_VALUE); |
| 230 | + |
| 231 | + assertThatThrownBy(() -> subscriber.completed.get(5, TimeUnit.SECONDS)) |
| 232 | + .hasCauseInstanceOf(SdkClientException.class); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void split_changingFile_fileGetsTouched_failsBecauseUpdatedModificationTime() throws Exception { |
| 237 | + AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
| 238 | + .path(testFile) |
| 239 | + .build(); |
| 240 | + List<AsyncRequestBody> splits = new ArrayList<>(); |
| 241 | + asyncRequestBody.splitCloseable(s -> s.chunkSizeInBytes(8 * MiB)).subscribe(splits::add); |
| 242 | + assertEquals(2, splits.size()); |
| 243 | + |
| 244 | + ControllableSubscriber subscriber1 = new ControllableSubscriber(); |
| 245 | + |
| 246 | + // read the first chunk fully |
| 247 | + splits.get(0).subscribe(subscriber1); |
| 248 | + subscriber1.sub.request(Long.MAX_VALUE); |
| 249 | + assertTrue(subscriber1.onNextSemaphore.tryAcquire(5, TimeUnit.SECONDS)); |
| 250 | + |
| 251 | + // Change the file to be updated |
| 252 | + Thread.sleep(1_000); // Wait for 1 second so that we are definitely in a different second than when the file was created |
| 253 | + Files.setLastModifiedTime(testFile, FileTime.from(Instant.now())); |
| 254 | + |
| 255 | + |
| 256 | + // read the second chunk which should now detect the file modification |
| 257 | + ControllableSubscriber subscriber2 = new ControllableSubscriber(); |
| 258 | + splits.get(1).subscribe(subscriber2); |
| 259 | + subscriber2.sub.request(Long.MAX_VALUE); |
| 260 | + |
| 261 | + assertThatThrownBy(() -> subscriber2.completed.get(5, TimeUnit.SECONDS)) |
| 262 | + .hasCauseInstanceOf(SdkClientException.class); |
| 263 | + } |
| 264 | + |
211 | 265 | @Test |
212 | 266 | public void changingFile_fileGetsDeleted_failsBecauseDeleted() throws Exception { |
213 | 267 | AsyncRequestBody asyncRequestBody = FileAsyncRequestBody.builder() |
|
0 commit comments