1919import com .google .api .gax .testing .DownloadableEmulatorRunner ;
2020import com .google .api .gax .testing .GCloudEmulatorRunner ;
2121import com .google .api .gax .testing .LocalServiceHelper ;
22-
23- import io .grpc .ManagedChannel ;
24- import io .grpc .netty .NegotiationType ;
25- import io .grpc .netty .NettyChannelBuilder ;
22+ import com .google .cloud .AuthCredentials ;
23+ import com .google .cloud .RetryParams ;
24+ import com .google .cloud .pubsub .PubSubOptions ;
2625
2726import java .io .IOException ;
2827import java .net .MalformedURLException ;
2928import java .net .URL ;
3029import java .util .ArrayList ;
3130import java .util .Arrays ;
3231import java .util .List ;
32+ import java .util .UUID ;
33+
34+ import io .grpc .ManagedChannel ;
35+ import io .grpc .netty .NegotiationType ;
36+ import io .grpc .netty .NettyChannelBuilder ;
3337
3438/**
3539 * A class that runs a Pubsub emulator instance for use in tests.
@@ -38,11 +42,13 @@ public class LocalPubsubHelper {
3842
3943 private final int port ;
4044 private final LocalServiceHelper serviceHelper ;
45+ private final String projectId ;
4146
4247 // Local server settings
4348 private static final int DEFAULT_PORT = 8080 ;
4449 private static final String DEFAULT_HOST = "localhost" ;
4550 private static final URL EMULATOR_URL ;
51+ private static final String PROJECT_ID_PREFIX = "test-project-" ;
4652
4753 // GCloud emulator settings
4854 private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators pubsub start" ;
@@ -77,8 +83,8 @@ public LocalPubsubHelper() {
7783 DownloadableEmulatorRunner downloadRunner =
7884 new DownloadableEmulatorRunner (Arrays .asList (BIN_NAME , BIN_CMD_PORT_FLAG + port ),
7985 EMULATOR_URL , MD5_CHECKSUM );
80- serviceHelper =
81- new LocalServiceHelper ( Arrays . asList ( gcloudRunner , downloadRunner ), port );
86+ serviceHelper = new LocalServiceHelper ( Arrays . asList ( gcloudRunner , downloadRunner ), port );
87+ projectId = PROJECT_ID_PREFIX + UUID . randomUUID (). toString ( );
8288 }
8389
8490 /**
@@ -121,4 +127,17 @@ public ManagedChannel createChannel() {
121127 .negotiationType (NegotiationType .PLAINTEXT )
122128 .build ();
123129 }
130+
131+ /**
132+ * Returns a {@link PubSubOptions} instance that sets the host to use the PubSub emulator on
133+ * localhost.
134+ */
135+ public PubSubOptions options () {
136+ return PubSubOptions .builder ()
137+ .projectId (projectId )
138+ .host ("localhost:" + port )
139+ .authCredentials (AuthCredentials .noAuth ())
140+ .retryParams (RetryParams .noRetries ())
141+ .build ();
142+ }
124143}
0 commit comments