3535 * A class that runs a Pubsub emulator instance for use in tests.
3636 */
3737public class LocalPubsubHelper {
38+
39+ private final int port ;
3840 private final LocalServiceHelper serviceHelper ;
39- private final List <String > gcloudCommand ;
40- private final URL emulatorUrl ;
41+
4142
4243 // Local server settings
4344 private static final int DEFAULT_PORT = 8080 ;
4445 private static final String DEFAULT_HOST = "localhost" ;
46+ private static final URL EMULATE_URL ;
4547
4648 // GCloud emulator settings
47- private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators pubsub start --host-port" ;
49+ private static final String GCLOUD_CMD_TEXT = "gcloud beta emulators pubsub start" ;
50+ private static final String GCLOUD_CMD_PORT_FLAG = "--host-port=" ;
4851 private static final String VERSION_PREFIX = "pubsub-emulator" ;
4952 private static final String MIN_VERSION = "2016.01.13" ;
53+ private static final String BIN_CMD_PORT_FLAG = "--port=" ;
5054
5155 // Downloadable emulator settings
5256 private static final String FILENAME = "pubsub-emulator-20160113-2.zip" ;
5357 private static final String BIN_NAME = "pubsub-emulator/bin/cloud-pubsub-fake" ;
5458 private static final String MD5_CHECKSUM = "20943e9defa300f2de101568459c133d" ;
5559
60+ static {
61+ try {
62+ EMULATE_URL = new URL ("http://storage.googleapis.com/pubsub/tools/" + FILENAME );
63+ } catch (MalformedURLException ex ) {
64+ throw new IllegalStateException (ex );
65+ }
66+ }
67+
5668 /**
5769 * Constructs a new LocalPubsubHelper. The method start() must
5870 * be called before it is used.
59- * @throws MalformedURLException
6071 */
61- public LocalPubsubHelper () throws MalformedURLException {
62- gcloudCommand = new ArrayList <>( Arrays . asList ( GCLOUD_CMD_TEXT . split ( " " )) );
63- gcloudCommand . add ( DEFAULT_HOST );
64- emulatorUrl = new URL ( "http://storage.googleapis.com/pubsub/tools/" + FILENAME );
72+ public LocalPubsubHelper () {
73+ port = LocalServiceHelper . findAvailablePort ( DEFAULT_PORT );
74+ List < String > gcloudCommand = new ArrayList <>( Arrays . asList ( GCLOUD_CMD_TEXT . split ( " " )) );
75+ gcloudCommand . add ( GCLOUD_CMD_PORT_FLAG + port );
6576 GCloudEmulatorRunner gcloudRunner =
6677 new GCloudEmulatorRunner (gcloudCommand , VERSION_PREFIX , MIN_VERSION );
6778 DownloadableEmulatorRunner downloadRunner =
68- new DownloadableEmulatorRunner (Arrays .asList (BIN_NAME ), emulatorUrl , MD5_CHECKSUM );
79+ new DownloadableEmulatorRunner (Arrays .asList (BIN_NAME , BIN_CMD_PORT_FLAG + port ),
80+ EMULATE_URL ,
81+ MD5_CHECKSUM );
6982 serviceHelper =
70- new LocalServiceHelper (Arrays .asList (gcloudRunner , downloadRunner ), DEFAULT_PORT );
83+ new LocalServiceHelper (Arrays .asList (gcloudRunner , downloadRunner ), port );
7184 }
7285
7386 /**
7487 * Start the local pubsub emulator through gcloud, download the zip file if user does not have
7588 * gcloud installed.
89+ *
7690 * @throws InterruptedException
7791 * @throws IOException
7892 */
7993 public void start () throws IOException , InterruptedException {
80- String blockUntilOutput = Integer .toString (DEFAULT_PORT );
94+ String blockUntilOutput = Integer .toString (port );
8195 serviceHelper .start (blockUntilOutput );
8296 }
8397
8498 /**
8599 * Reset the internal state of the emulator.
86- * @throws InterruptedException
100+ *
87101 * @throws IOException
88102 */
89- public void reset () throws IOException , InterruptedException {
103+ public void reset () throws IOException {
90104 this .serviceHelper .sendPostRequest ("/reset" );
91105 }
92106
93107 /**
94108 * Quit the local emulator and related local service.
109+ *
95110 * @throws InterruptedException
96111 * @throws IOException
97112 */
@@ -104,8 +119,8 @@ public void stop() throws IOException, InterruptedException {
104119 * Creates a channel for making requests to the in-memory service.
105120 */
106121 public ManagedChannel createChannel () {
107- return NettyChannelBuilder .forAddress (DEFAULT_HOST , DEFAULT_PORT )
122+ return NettyChannelBuilder .forAddress (DEFAULT_HOST , port )
108123 .negotiationType (NegotiationType .PLAINTEXT )
109124 .build ();
110125 }
111- }
126+ }
0 commit comments