Skip to content

Commit 55a7d1a

Browse files
committed
pubsub: fix testStartStopReset
The test verifies that calling stop() actually stops the emulator. The previous implementation stops the emulator by - sending HTTP POST to /shutdown - terminating the emulator process The second step follows directly after the first. It then queries the server it just shut down to make sure it does not respond. This behavior is incorrect for two reasons. - The server might continue serving request after responding to /shutdown. - The "emulator process" might not be the emulator at all. It might be a script that in-turn runs the emulator. On UNIX systems, killing a parent process does not kill any of its children; the actual emulator might run indefinitely after we kill the process we spawned. This commit changes this behavior. After POSTing to /shutdown, we simply wait for the spawned process to terminate on its own. Technically, there is still a race: Another emulator could have been started on the same port. When we send a request to verify that the old emulator has terminated, we might get a response from the new one. Since we execute tests sequentially though, I believe this should not be a problem in practice. Updates #1429.
1 parent b24ec1a commit 55a7d1a

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

google-cloud-core/src/main/java/com/google/cloud/testing/BaseEmulatorHelper.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@ protected final void startProcess(String blockUntilOutput)
104104
}
105105

106106
/**
107-
* Stops the local service's subprocess and any possible thread listening for its output.
107+
* Waits for the local service's subprocess to terminate,
108+
* and stop any possible thread listening for its output.
108109
*/
109-
protected final void stopProcess() throws IOException, InterruptedException {
110+
protected final void waitForProcess() throws IOException, InterruptedException {
110111
if (blockingProcessReader != null) {
111112
blockingProcessReader.terminate();
112113
blockingProcessReader = null;
113114
}
114115
if (activeRunner != null) {
115-
activeRunner.stop();
116+
activeRunner.waitFor();
116117
activeRunner = null;
117118
}
118119
}
@@ -195,9 +196,9 @@ protected interface EmulatorRunner {
195196
void start() throws IOException;
196197

197198
/**
198-
* Stops the emulator associated to this runner.
199+
* Wait for the emulator associated to this runner to terminate.
199200
*/
200-
void stop() throws InterruptedException;
201+
void waitFor() throws InterruptedException;
201202

202203
/**
203204
* Returns the process associated to the emulator, if any.
@@ -239,9 +240,8 @@ public void start() throws IOException {
239240
}
240241

241242
@Override
242-
public void stop() throws InterruptedException {
243+
public void waitFor() throws InterruptedException {
243244
if (process != null) {
244-
process.destroy();
245245
process.waitFor();
246246
}
247247
}
@@ -337,9 +337,8 @@ public void start() throws IOException {
337337
}
338338

339339
@Override
340-
public void stop() throws InterruptedException {
340+
public void waitFor() throws InterruptedException {
341341
if (process != null) {
342-
process.destroy();
343342
process.waitFor();
344343
}
345344
}

google-cloud-core/src/test/java/com/google/cloud/testing/BaseEmulatorHelperTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void start() throws IOException, InterruptedException {
6767

6868
@Override
6969
public void stop() throws IOException, InterruptedException {
70-
stopProcess();
70+
waitForProcess();
7171
}
7272

7373
@Override
@@ -86,7 +86,7 @@ public void testEmulatorHelper() throws IOException, InterruptedException {
8686
emulatorRunner.start();
8787
EasyMock.expectLastCall();
8888
EasyMock.expect(emulatorRunner.getProcess()).andReturn(process);
89-
emulatorRunner.stop();
89+
emulatorRunner.waitFor();
9090
EasyMock.expectLastCall();
9191
EasyMock.replay(process, emulatorRunner);
9292
TestEmulatorHelper helper =
@@ -108,7 +108,7 @@ public void testEmulatorHelperMultipleRunners() throws IOException, InterruptedE
108108
secondRunner.start();
109109
EasyMock.expectLastCall();
110110
EasyMock.expect(secondRunner.getProcess()).andReturn(process);
111-
secondRunner.stop();
111+
secondRunner.waitFor();
112112
EasyMock.expectLastCall();
113113
EasyMock.replay(process, secondRunner);
114114
TestEmulatorHelper helper =

google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void reset() throws IOException {
227227
*/
228228
public void stop() throws IOException, InterruptedException {
229229
sendPostRequest("/shutdown");
230-
stopProcess();
230+
waitForProcess();
231231
deleteRecursively(gcdPath);
232232
}
233233

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/testing/LocalPubSubHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ public void reset() throws IOException {
146146
@Override
147147
public void stop() throws IOException, InterruptedException {
148148
sendPostRequest("/shutdown");
149-
stopProcess();
149+
waitForProcess();
150150
}
151151
}

0 commit comments

Comments
 (0)