Skip to content

Commit 638bd92

Browse files
committed
Add test for save/restore closed BlobWriteChannelImpl
1 parent 2060526 commit 638bd92

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,11 @@ static Builder builder(StorageOptions options, BlobInfo blobInfo, String uploadI
227227
@Override
228228
public BlobWriteChannel restore() {
229229
BlobWriteChannelImpl channel = new BlobWriteChannelImpl(serviceOptions, blobInfo, uploadId);
230+
if (buffer != null) {
231+
channel.buffer = buffer.clone();
232+
channel.limit = buffer.length;
233+
}
230234
channel.position = position;
231-
channel.buffer = buffer.clone();
232-
channel.limit = buffer.length;
233235
channel.isOpen = isOpen;
234236
channel.chunkSize = chunkSize;
235237
return channel;

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,32 @@ public void testSaveAndRestore() throws IOException {
220220
assertEquals(new Long(DEFAULT_CHUNK_SIZE), capturedPosition.getValues().get(1));
221221
}
222222

223+
@Test
224+
public void testSaveAndRestoreClosed() throws IOException {
225+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock).times(2);
226+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
227+
EasyMock.replay(optionsMock);
228+
EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID);
229+
Capture<byte[]> capturedBuffer = Capture.newInstance();
230+
storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0),
231+
EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(0), EasyMock.eq(true));
232+
EasyMock.expectLastCall();
233+
EasyMock.replay(storageRpcMock);
234+
writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
235+
writer.close();
236+
RestorableState<BlobWriteChannel> writerState = writer.save();
237+
RestorableState<BlobWriteChannel> expectedWriterState =
238+
BlobWriteChannelImpl.StateImpl.builder(optionsMock, BLOB_INFO, UPLOAD_ID)
239+
.buffer(null)
240+
.chunkSize(DEFAULT_CHUNK_SIZE)
241+
.isOpen(false)
242+
.position(0)
243+
.build();
244+
BlobWriteChannel restoredWriter = writerState.restore();
245+
assertArrayEquals(new byte[0], capturedBuffer.getValue());
246+
assertEquals(expectedWriterState, restoredWriter.save());
247+
}
248+
223249
@Test
224250
public void testStateEquals() {
225251
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock).times(2);

0 commit comments

Comments
 (0)