Last active
May 16, 2018 10:50
-
-
Save ensonic/a1b64be415f537b59bcc951883b29596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx; | |
import com.google.cloud.datastore.Datastore; | |
import com.google.cloud.datastore.testing.LocalDatastoreHelper; | |
import com.google.common.flogger.FluentLogger; | |
import java.io.IOException; | |
import org.junit.rules.ExternalResource; | |
import org.threeten.bp.Duration; | |
/** Datastore helper for unittests. */ | |
public class DatastoreTestRule extends ExternalResource { | |
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); | |
private static final LocalDatastoreHelper datastoreHelper = LocalDatastoreHelper.create(); | |
private static final int NUM_RETRIES = 3; | |
public static Datastore getDatastore() { | |
/* When the datastore emulator crashes, we'll get | |
* java.net.ConnectException: Connection refused (Connection refused) | |
* For now restart the emulator to let the other tests run. | |
*/ | |
try { | |
datastoreHelper.reset(); | |
} catch (IOException e) { | |
logger.atWarning().withCause(e).log("Failed to reset LocalDatastoreHelper, restarting"); | |
try { | |
datastoreHelper.start(); | |
} catch (IOException|InterruptedException e2) { | |
logger.atSevere().withCause(e2).log("Failed to start LocalDatastoreHelper"); | |
return null; | |
} | |
} | |
return datastoreHelper.getOptions().getService(); | |
} | |
@Override | |
protected void before() throws Throwable { | |
datastoreHelper.start(); | |
} | |
@Override | |
protected void after() { | |
try { | |
datastoreHelper.stop(Duration.ofSeconds(10)); | |
} catch (Exception e) { | |
logger.atSevere().withCause(e).log("Failed to stop LocalDatastoreHelper"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment