Search in sources :

Example 1 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class BlobWriteChannelTest method testSaveAndRestore.

@Test
public void testSaveAndRestore() throws IOException {
    expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID);
    Capture<byte[]> capturedBuffer = Capture.newInstance(CaptureType.ALL);
    Capture<Long> capturedPosition = Capture.newInstance(CaptureType.ALL);
    storageRpcMock.write(eq(UPLOAD_ID), capture(capturedBuffer), eq(0), captureLong(capturedPosition), eq(DEFAULT_CHUNK_SIZE), eq(false));
    expectLastCall().times(2);
    replay(storageRpcMock);
    ByteBuffer buffer1 = randomBuffer(DEFAULT_CHUNK_SIZE);
    ByteBuffer buffer2 = randomBuffer(DEFAULT_CHUNK_SIZE);
    writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
    assertEquals(DEFAULT_CHUNK_SIZE, writer.write(buffer1));
    assertArrayEquals(buffer1.array(), capturedBuffer.getValues().get(0));
    assertEquals(new Long(0L), capturedPosition.getValues().get(0));
    RestorableState<WriteChannel> writerState = writer.capture();
    WriteChannel restoredWriter = writerState.restore();
    assertEquals(DEFAULT_CHUNK_SIZE, restoredWriter.write(buffer2));
    assertArrayEquals(buffer2.array(), capturedBuffer.getValues().get(1));
    assertEquals(new Long(DEFAULT_CHUNK_SIZE), capturedPosition.getValues().get(1));
}
Also used : WriteChannel(com.google.cloud.WriteChannel) EasyMock.captureLong(org.easymock.EasyMock.captureLong) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class BlobWriteChannelTest method testStateEquals.

@Test
public void testStateEquals() {
    expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID).times(2);
    replay(storageRpcMock);
    writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
    // avoid closing when you don't want partial writes to GCS upon failure
    @SuppressWarnings("resource") WriteChannel writer2 = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
    RestorableState<WriteChannel> state = writer.capture();
    RestorableState<WriteChannel> state2 = writer2.capture();
    assertEquals(state, state2);
    assertEquals(state.hashCode(), state2.hashCode());
    assertEquals(state.toString(), state2.toString());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 3 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class BlobWriteChannelTest method testSaveAndRestoreClosed.

@Test
public void testSaveAndRestoreClosed() throws IOException {
    expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID);
    Capture<byte[]> capturedBuffer = Capture.newInstance();
    storageRpcMock.write(eq(UPLOAD_ID), capture(capturedBuffer), eq(0), eq(0L), eq(0), eq(true));
    replay(storageRpcMock);
    writer = new BlobWriteChannel(options, BLOB_INFO, EMPTY_RPC_OPTIONS);
    writer.close();
    RestorableState<WriteChannel> writerState = writer.capture();
    RestorableState<WriteChannel> expectedWriterState = BlobWriteChannel.StateImpl.builder(options, BLOB_INFO, UPLOAD_ID).setBuffer(null).setChunkSize(DEFAULT_CHUNK_SIZE).setIsOpen(false).setPosition(0).build();
    WriteChannel restoredWriter = writerState.restore();
    assertArrayEquals(new byte[0], capturedBuffer.getValue());
    assertEquals(expectedWriterState, restoredWriter.capture());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 4 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class TableDataWriteChannelTest method testSaveAndRestore.

@Test
public void testSaveAndRestore() throws IOException {
    expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID);
    Capture<byte[]> capturedBuffer = Capture.newInstance(CaptureType.ALL);
    Capture<Long> capturedPosition = Capture.newInstance(CaptureType.ALL);
    expect(bigqueryRpcMock.write(eq(UPLOAD_ID), capture(capturedBuffer), eq(0), captureLong(capturedPosition), eq(DEFAULT_CHUNK_SIZE), eq(false))).andReturn(null).times(2);
    replay(bigqueryRpcMock);
    ByteBuffer buffer1 = randomBuffer(DEFAULT_CHUNK_SIZE);
    ByteBuffer buffer2 = randomBuffer(DEFAULT_CHUNK_SIZE);
    writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
    assertEquals(DEFAULT_CHUNK_SIZE, writer.write(buffer1));
    assertArrayEquals(buffer1.array(), capturedBuffer.getValues().get(0));
    assertEquals(new Long(0L), capturedPosition.getValues().get(0));
    assertNull(writer.getJob());
    RestorableState<WriteChannel> writerState = writer.capture();
    WriteChannel restoredWriter = writerState.restore();
    assertEquals(DEFAULT_CHUNK_SIZE, restoredWriter.write(buffer2));
    assertArrayEquals(buffer2.array(), capturedBuffer.getValues().get(1));
    assertEquals(new Long(DEFAULT_CHUNK_SIZE), capturedPosition.getValues().get(1));
}
Also used : WriteChannel(com.google.cloud.WriteChannel) EasyMock.captureLong(org.easymock.EasyMock.captureLong) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class TableDataWriteChannelTest method testStateEquals.

@Test
public void testStateEquals() {
    expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID).times(2);
    replay(bigqueryRpcMock);
    writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
    // avoid closing when you don't want partial writes upon failure
    @SuppressWarnings("resource") WriteChannel writer2 = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
    RestorableState<WriteChannel> state = writer.capture();
    RestorableState<WriteChannel> state2 = writer2.capture();
    assertEquals(state, state2);
    assertEquals(state.hashCode(), state2.hashCode());
    assertEquals(state.toString(), state2.toString());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Aggregations

WriteChannel (com.google.cloud.WriteChannel)18 Test (org.junit.Test)17 BlobInfo (com.google.cloud.storage.BlobInfo)8 ByteBuffer (java.nio.ByteBuffer)7 ReadChannel (com.google.cloud.ReadChannel)5 StorageException (com.google.cloud.storage.StorageException)3 Random (java.util.Random)2 EasyMock.captureLong (org.easymock.EasyMock.captureLong)2 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1