joelittlejohn / embedmongo-maven-plugin

Maven plugin wrapper for the flapdoodle.de embedded MongoDB API
Apache License 2.0
88 stars 51 forks source link

Connection refused #29

Closed aurabhi closed 11 years ago

aurabhi commented 11 years ago

I am trying to connect to the embedded mongo instance from a unit test. My configuration looks like this:

            <plugin>
                <groupId>com.github.joelittlejohn.embedmongo</groupId>
                <artifactId>embedmongo-maven-plugin</artifactId>
                <version>0.1.9</version>
                <configuration>
                    <bindIp>127.0.0.1</bindIp>
                    <port>37017</port> 
                    <randomPort>true</randomPort>
                    <version>2.2.3</version>
                    <databaseDirectory>/tmp</databaseDirectory>
                    <logging>file</logging>
                    <logFile>${project.build.directory}/mongodb.log</logFile>
                    <logFileEncoding>utf-8</logFileEncoding>
                </configuration>
                <executions>
                    <execution>
                        <id>start-mongo</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-mongo</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

When in the unit test I try to connect from an integration test. I see the following error. (The mongolog is clean and stops mongod only post-integration-test.

Sep 02, 2013 2:15:26 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:37017
java.io.IOException: couldn't connect to [localhost/127.0.0.1:37017] bc:java.net.ConnectException: Connection refused
        at com.mongodb.DBPort._open(DBPort.java:214)
        at com.mongodb.DBPort.go(DBPort.java:107)
        at com.mongodb.DBPort.go(DBPort.java:88)
        at com.mongodb.DBPort.findOne(DBPort.java:143)
        at com.mongodb.DBPort.runCommand(DBPort.java:148)
        at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
        at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:372)
        at com.mongodb.Mongo.isMongosConnection(Mongo.java:627)
        at com.mongodb.DBCollection.findOne(DBCollection.java:723)
        at com.mongodb.DBCollection.findOne(DBCollection.java:669)
        at com.mongodb.DBCollection.findOne(DBCollection.java:659)
        at at itests.MongoTest.testUpload(MongoTest.java:96)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
        at com.sun.proxy.$Proxy0.invoke(Unknown Source)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

Sep 02, 2013 2:15:26 PM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on localhost/127.0.0.1:37017
java.io.IOException: couldn't connect to [localhost/127.0.0.1:37017] bc:java.net.ConnectException: Connection refused
        at com.mongodb.DBPort._open(DBPort.java:214)
        at com.mongodb.DBPort.go(DBPort.java:107)
        at com.mongodb.DBPort.go(DBPort.java:88)
        at com.mongodb.DBPort.findOne(DBPort.java:143)
        at com.mongodb.DBPort.runCommand(DBPort.java:148)
        at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
        at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:277)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
        at com.mongodb.DBCollection.findOne(DBCollection.java:727)
        at com.mongodb.DBCollection.findOne(DBCollection.java:669)
        at com.mongodb.DBCollection.findOne(DBCollection.java:659)
        at at itests.MongoTest.testUpload(MongoTest.java:96)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
        at com.sun.proxy.$Proxy0.invoke(Unknown Source)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

Sep 02, 2013 2:15:26 PM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to localhost/127.0.0.1:37017 b/c of error
java.io.IOException: couldn't connect to [localhost/127.0.0.1:37017] bc:java.net.ConnectException: Connection refused
        at com.mongodb.DBPort._open(DBPort.java:214)
        at com.mongodb.DBPort.go(DBPort.java:107)
        at com.mongodb.DBPort.call(DBPort.java:74)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:286)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
        at com.mongodb.DBCollection.findOne(DBCollection.java:727)
        at com.mongodb.DBCollection.findOne(DBCollection.java:669)
        at com.mongodb.DBCollection.findOne(DBCollection.java:659)
        at itests.MongoTest.testUpload(MongoTest.java:96)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
        at com.sun.proxy.$Proxy0.invoke(Unknown Source)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)

com.mongodb.MongoException$Network: can't call something : localhost/127.0.0.1:37017/svmreporter
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:295)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:257)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
        at com.mongodb.DBCollection.findOne(DBCollection.java:727)
        at com.mongodb.DBCollection.findOne(DBCollection.java:669)
        at com.mongodb.DBCollection.findOne(DBCollection.java:659)
        at itests.MongoTest.testUpload(MongoTest.java:96)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
        at com.sun.proxy.$Proxy0.invoke(Unknown Source)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:87)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: java.io.IOException: couldn't connect to [localhost/127.0.0.1:37017] bc:java.net.ConnectException: Connection refused
        at com.mongodb.DBPort._open(DBPort.java:214)
        at com.mongodb.DBPort.go(DBPort.java:107)
        at com.mongodb.DBPort.call(DBPort.java:74)
        at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:286)
        ... 39 more
joelittlejohn commented 11 years ago

What maven plugin are you using to run your integration tests? The typical way to run integration tests with Maven is to use the maven-failsafe-plugin and give your test a name like MongoIT (instead of MongoTest).

I think what might be happening here is that your integration test is actually running as a unit test. That means, it's being run by the maven-surefure-plugin and is running during the test phase instead of the integration phase.

Maven phases are ordered like this:

validate -> compile -> test -> package -> integration -> verify -> install -> deploy

By default, the embedmongo-maven-plugin starts before the integration phase and stops after the integration phase. If you run your test as a unit test during the test phase then you are running your test before Mongo has started.

What exact command are you using to run Maven (what phase or goals are you specifying)?

aurabhi commented 11 years ago

Hi Joe,

  Thanks for the response. I am using maven-failsafe-plugin to run integration tests. I use 'mvn install' to run the same. There are other integration tests in the project and they run successfully. Only the ones related to mongo fail with the error that I had mentioned below. I see that the mongod is running and listening on that port during the 'integration' phase. But I always get a connection refused.

Any help with the plugin is really appreciated as I have already spent a complete day trying to fix this test.

Thanks,


From: Joe Littlejohn notifications@github.com To: joelittlejohn/embedmongo-maven-plugin embedmongo-maven-plugin@noreply.github.com Cc: aurabhi aurabhi@yahoo.com Sent: Monday, September 2, 2013 3:12 PM Subject: Re: [embedmongo-maven-plugin] Connection refused (#29)

What maven plugin are you using to run your integration tests? The typical way to run integration tests with Maven is to use the maven-failsafe-plugin and give your test a name like MongoIT (instead of MongoTest). I think what might be happening here is that your integration test is actually running as a unit test. That means, it's being run by the maven-surefure-plugin and is running during the test phase instead of the integration phase. Maven phases are ordered like this: validate -> compile -> test -> package -> integration -> verify -> install -> deploy By default, the embedmongo-maven-plugin starts before the integration phase and stops after the integration phase. If you run your test as a unit test during the test phase then you are running your test before Mongo has started. What exact command are you using to run Maven (what phase or goals are you specifying)? — Reply to this email directly or view it on GitHub.

joelittlejohn commented 11 years ago

Have you actually disabled the surefire plugin for this project? By default it will attempt to run your test if you have the test named like MongoTest. The default includes for failsafe are:

 <include>**/Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>

and for failsafe default includes are:

 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>

My other thought would be firewall problems. Are you developing on Windows? If so you might want to add <bindIp>127.0.0.1</bindIp> to your config.

aurabhi commented 11 years ago

Joe,

I have already tried disabling surefire. My config includes the tests in failsafe config.

I am developing on CentOS. I tried binding it to localhost, disabling iptables etc but non of these helped.

On 03-Sep-2013, at 5:25 PM, Joe Littlejohn notifications@github.com wrote:

Have you actually disabled the surefire plugin for this project? By default it will attempt to run your test if you have the test named like MongoTest. The default includes for failsafe are:

**/Test*.java **/_Test.java __/_TestCase.java

and for failsafe default includes are:

**/IT*.java **/_IT.java __/_ITCase.java

My other thought would be firewall problems. Are you developing on Windows? If so you might want to add 127.0.0.1 to your config.

— Reply to this email directly or view it on GitHub.

joelittlejohn commented 11 years ago

Could you send me your maven log? Either via pastebin or (if you don't want to make it public) then feel free to email it to me using my github username at gmail.com. Also, could you send your mongodb.log?

There's really very little to go wrong here, either you are running your test at the wrong time (when mongo is not running) or your system configuration is blocking this communication.

Another thing you could try is invoking Maven with this extra property:

-Dembedmongo.wait

This should cause Mongo to start but will leave the build paused. You could then try connecting using the mongo shell:

mongo localhost 37017

To make sure that mongo is starting correctly. Ctrl-C to stop.

joelittlejohn commented 11 years ago

Not enough information here to investigate I'm afraid. It looks like you are not running the plugin at the same time as your tests.

aurabhi commented 10 years ago

In my config, that I had mentioned above, we can see that a port is specified and randomPort is also set to true.

<port>37017</port>
<randomPort>true</randomPort>

and because random port is set to true it ignores the specified port and starts the MongoDB on a random port. That's why I was getting the connection refused error. This should either be documented or a warn msg must be logged when the MongoDB is being started.

joelittlejohn commented 10 years ago

Thanks for reporting back after you solved this, I'll definitely put in a warning and mention the order of precedence in the docs somewhere.

joelittlejohn commented 10 years ago

Ah, it's actually already mentioned in the docs that the randomPort setting overrides the port parameter.

I'll add a warning though.