Skip to content

Commit 5e4aef0

Browse files
author
andrey-qlogic
committed
4107: Added integration test testWriteChannelWithConnectionPool
1 parent 2b3b435 commit 5e4aef0

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it

google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
import static org.junit.Assume.assumeFalse;
2929
import static org.junit.Assume.assumeTrue;
3030

31+
import com.google.api.client.http.HttpTransport;
32+
import com.google.api.client.http.apache.ApacheHttpTransport;
3133
import com.google.api.client.util.DateTime;
3234
import com.google.api.gax.paging.Page;
3335
import com.google.auth.ServiceAccountSigner;
36+
import com.google.auth.http.HttpTransportFactory;
3437
import com.google.auth.oauth2.GoogleCredentials;
3538
import com.google.cloud.kms.v1.CreateCryptoKeyRequest;
3639
import com.google.cloud.kms.v1.CreateKeyRingRequest;
@@ -45,7 +48,9 @@
4548
import com.google.cloud.Policy;
4649
import com.google.cloud.ReadChannel;
4750
import com.google.cloud.RestorableState;
51+
import com.google.cloud.TransportOptions;
4852
import com.google.cloud.WriteChannel;
53+
import com.google.cloud.http.HttpTransportOptions;
4954
import com.google.cloud.kms.v1.LocationName;
5055
import com.google.cloud.storage.Acl;
5156
import com.google.cloud.storage.Acl.Role;
@@ -113,6 +118,8 @@
113118
import io.grpc.StatusRuntimeException;
114119
import io.grpc.stub.MetadataUtils;
115120

121+
import org.apache.http.impl.client.HttpClients;
122+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
116123
import org.junit.AfterClass;
117124
import org.junit.BeforeClass;
118125
import org.junit.Test;
@@ -184,6 +191,15 @@ public static void afterClass() throws ExecutionException, InterruptedException
184191
}
185192
}
186193

194+
private static class CustomHttpTransportFactory implements HttpTransportFactory {
195+
@Override
196+
public HttpTransport create() {
197+
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
198+
manager.setMaxTotal(1);
199+
return new ApacheHttpTransport(HttpClients.createMinimal(manager));
200+
}
201+
}
202+
187203
private static void prepareKmsKeys() throws IOException {
188204
String projectId = remoteStorageHelper.getOptions().getProjectId();
189205
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
@@ -1741,6 +1757,27 @@ public void testWriteChannelExistingBlob() throws IOException {
17411757
assertTrue(storage.delete(BUCKET, blobName));
17421758
}
17431759

1760+
@Test(timeout = 5000)
1761+
public void testWriteChannelWithConnectionPool() throws IOException {
1762+
TransportOptions transportOptions = HttpTransportOptions.newBuilder()
1763+
.setHttpTransportFactory(new CustomHttpTransportFactory()).build();
1764+
Storage storageWithPool =
1765+
StorageOptions.newBuilder().setTransportOptions(transportOptions).build().getService();
1766+
String blobName = "test-read-and-write-channels-blob";
1767+
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
1768+
byte[] stringBytes;
1769+
try (WriteChannel writer = storageWithPool.writer(blob)) {
1770+
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
1771+
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
1772+
writer.write(ByteBuffer.wrap(stringBytes));
1773+
}
1774+
try (WriteChannel writer = storageWithPool.writer(blob)) {
1775+
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
1776+
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
1777+
writer.write(ByteBuffer.wrap(stringBytes));
1778+
}
1779+
}
1780+
17441781
@Test
17451782
public void testGetSignedUrl() throws IOException {
17461783
if(storage.getOptions().getCredentials() != null) {

0 commit comments

Comments
 (0)