Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close() call on already closed S3WritableByteChannel (awslabs#453) #454

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public boolean isOpen() {
@Override
public void close() throws IOException {
channel.close();
if (!open) {
// channel has already been closed -> close() should have no effect
return;
}

s3TransferUtil.uploadLocalFile(path, tempFile);
Files.deleteIfExists(tempFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import software.amazon.awssdk.services.s3.S3AsyncClient;

Expand Down Expand Up @@ -125,6 +127,24 @@ void tmpFileIsCleanedUpAfterClose(@TempDir Path tempDir) throws InterruptedExcep
assertThat(countAfterClosing).isLessThan(countAfterOpening);
}

@Test
@DisplayName("second close() call should be a no-op")
void secondCloseIsNoOp() throws InterruptedException, TimeoutException, IOException {
S3FileSystemProvider provider = mock();
when(provider.exists(any(S3AsyncClient.class), any())).thenReturn(false);
S3FileSystem fs = mock();
when(fs.provider()).thenReturn(provider);
var file = S3Path.getPath(fs, "somefile");

S3TransferUtil utilMock = mock();
var channel = new S3WritableByteChannel(file, mock(), utilMock, Set.of(CREATE));
channel.close();
// this close() call should be a no-op
channel.close();

verify(utilMock, times(1)).uploadLocalFile(any(), any());
}

private long countTemporaryFiles(Path tempDir) throws IOException {
try (var list = Files.list(tempDir.getParent())) {
return list
Expand Down