google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

LocalDatastoreHelper throws an exception in empty test method

Open pauloleitemoreira opened this issue 10 years ago • 5 comments

Consider this code:

public class AnyTest {
    private final LocalDatastoreHelper helper = LocalDatastoreHelper.create();

    @Before
    public void setUp() throws IOException, InterruptedException {
        helper.start();
    }

    @After
    public void tearDown() throws IOException, InterruptedException {
        helper.stop();
    }

    @Test
    public void testOk(){
        Datastore datastore = helper.getOptions().getService();

        Key key = datastore.newKeyFactory().setKind("AnyKind").newKey(1234);
        Entity entity = Entity.newBuilder(key).build();

        datastore.put(entity);
    }

    @Test
    public void testWeird(){
        
    }
}

When I execute these tests, testOk() pass but the empty testWeird() does not pass. It gives me the following exception:

java.net.ConnectException: Connection refused: connect

	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at java.net.Socket.connect(Socket.java:538)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:781)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
	at com.google.cloud.testing.BaseEmulatorHelper.sendPostRequest(BaseEmulatorHelper.java:163)
	at com.google.cloud.datastore.testing.LocalDatastoreHelper.stop(LocalDatastoreHelper.java:229)
	at com.usefulmilk.endpoints.example.AnyTest.tearDown(AnyTest.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Another thing to consider is when we remove datastore.put(entity); from testOk() method, the exception is being thrown too.

pauloleitemoreira avatar Nov 25 '16 16:11 pauloleitemoreira

It looks like this is mostly an annoyance and can be worked around, correct?

garrettjonesgoogle avatar Nov 29 '16 14:11 garrettjonesgoogle

Actually not. I cannot test anything that didn't call datastore action. For instance, I cannot test some expectedException, because that exception is thrown..

pauloleitemoreira avatar Nov 29 '16 15:11 pauloleitemoreira

Ahh ok. I will investigate.

garrettjonesgoogle avatar Nov 29 '16 15:11 garrettjonesgoogle

For the time being, we have decided to remove local emulator helpers. We will provide some instructions so that people can start the emulator themselves using a shell script and then connect to it from Java. LocalDatastoreHelper will consequently be deleted soon.

garrettjonesgoogle avatar Apr 12 '17 02:04 garrettjonesgoogle

https://github.com/GoogleCloudPlatform/google-cloud-java/pull/1931 has marked the LocalDatastoreHelper as internal use only. Now this issue should be obsolete.

shinfan avatar Apr 18 '17 16:04 shinfan