2222import com .google .cloud .datastore .DatastoreOptions ;
2323import com .google .cloud .testing .BaseEmulatorHelper ;
2424import com .google .common .collect .ImmutableList ;
25-
26- import org .threeten .bp .Duration ;
27-
2825import java .io .IOException ;
2926import java .net .MalformedURLException ;
3027import java .net .URL ;
4037import java .util .concurrent .TimeoutException ;
4138import java .util .logging .Level ;
4239import java .util .logging .Logger ;
40+ import org .threeten .bp .Duration ;
4341
4442/**
45- * Utility to start and stop local Google Cloud Datastore process .
46- *
47- * Internal testing use only
43+ * Utility to start and stop local Google Cloud Datastore emulators .
44+ *
45+ * <p>This class is unstable.
4846 */
4947@ InternalApi
5048public class LocalDatastoreHelper extends BaseEmulatorHelper <DatastoreOptions > {
@@ -185,6 +183,8 @@ public static LocalDatastoreHelper create() {
185183 /**
186184 * Starts the local Datastore emulator through {@code gcloud}, downloads and caches the zip file
187185 * if user does not have {@code gcloud} or a compatible emulator version installed.
186+ *
187+ * <p>Currently the emulator does not persist any state across runs.
188188 */
189189 @ Override
190190 public void start () throws IOException , InterruptedException {
@@ -193,21 +193,47 @@ public void start() throws IOException, InterruptedException {
193193 }
194194
195195 /**
196- * Reset the internal state of the Datastore emulator.
196+ * Resets the internal state of the Datastore emulator.
197+ *
198+ * <p>When running tests, one might {@code reset()} before each test, so earlier tests would not
199+ * affect later ones.
197200 */
201+ @ Override
198202 public void reset () throws IOException {
199203 sendPostRequest ("/reset" );
200204 }
201205
202206 /**
203207 * Stops the Datastore emulator.
208+ *
209+ * <p>It is important to stop the emulator. Since the emulator runs in its own process, not
210+ * stopping it might cause it to become orphan.
211+ *
212+ * <p>It is not required to call {@link #reset()} before {@code stop}.
213+ *
214+ * @param timeout The duration to wait for the emulator process to stop. It is recommended to set
215+ * this value high to ensure proper shutdown, like 5 seconds or more.
204216 */
217+ @ Override
205218 public void stop (Duration timeout ) throws IOException , InterruptedException , TimeoutException {
206219 sendPostRequest ("/shutdown" );
207220 waitForProcess (timeout );
208221 deleteRecursively (gcdPath );
209222 }
210223
224+ /**
225+ * Stops the Datastore emulator. The same as {@link stop(Duration)} but with timeout duration of
226+ * 20 seconds.
227+ *
228+ * <p>It is important to stop the emulator. Since the emulator runs in its own process, not
229+ * stopping it might cause it to become orphan.
230+ *
231+ * <p>It is not required to call {@link #reset()} before {@code stop()}.
232+ */
233+ public void stop () throws IOException , InterruptedException , TimeoutException {
234+ stop (Duration .ofSeconds (20 ));
235+ }
236+
211237 private static void deleteRecursively (Path path ) throws IOException {
212238 if (path == null || !Files .exists (path )) {
213239 return ;
0 commit comments