Skip to content

Commit

Permalink
Add options() method and project name to LocalPubsubHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed May 11, 2016
1 parent cd848c0 commit ca46965
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
import com.google.api.gax.testing.DownloadableEmulatorRunner;
import com.google.api.gax.testing.GCloudEmulatorRunner;
import com.google.api.gax.testing.LocalServiceHelper;

import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import com.google.cloud.AuthCredentials;
import com.google.cloud.RetryParams;
import com.google.cloud.pubsub.PubSubOptions;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;

/**
* A class that runs a Pubsub emulator instance for use in tests.
Expand All @@ -38,11 +42,13 @@ public class LocalPubsubHelper {

private final int port;
private final LocalServiceHelper serviceHelper;
private final String projectId;

// Local server settings
private static final int DEFAULT_PORT = 8080;
private static final String DEFAULT_HOST = "localhost";
private static final URL EMULATOR_URL;
private static final String PROJECT_ID_PREFIX = "test-project-";

// GCloud emulator settings
private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators pubsub start";
Expand Down Expand Up @@ -77,8 +83,8 @@ public LocalPubsubHelper() {
DownloadableEmulatorRunner downloadRunner =
new DownloadableEmulatorRunner(Arrays.asList(BIN_NAME, BIN_CMD_PORT_FLAG + port),
EMULATOR_URL, MD5_CHECKSUM);
serviceHelper =
new LocalServiceHelper(Arrays.asList(gcloudRunner, downloadRunner), port);
serviceHelper = new LocalServiceHelper(Arrays.asList(gcloudRunner, downloadRunner), port);
projectId = PROJECT_ID_PREFIX + UUID.randomUUID().toString();
}

/**
Expand Down Expand Up @@ -121,4 +127,17 @@ public ManagedChannel createChannel() {
.negotiationType(NegotiationType.PLAINTEXT)
.build();
}

/**
* Returns a {@link PubSubOptions} instance that sets the host to use the PubSub emulator on
* localhost.
*/
public PubSubOptions options() {
return PubSubOptions.builder()
.projectId(projectId)
.host("localhost:" + port)
.authCredentials(AuthCredentials.noAuth())
.retryParams(RetryParams.noRetries())
.build();
}
}

0 comments on commit ca46965

Please sign in to comment.