Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excepion when running multiple tests using MongoDbRule + Spring+Real remote Mongo Db+ CLEAN_INSERT Strategy #178

Closed
tinesoft opened this issue Mar 23, 2018 · 10 comments

Comments

@tinesoft
Copy link
Contributor

tinesoft commented Mar 23, 2018

Hi,

First, thanks for this project, really usefully.

I'm trying to writing integration tests using your library, with the scenario described in the issue's title (using a MongoClient instance created in Spring context from a real running MongoDb.)

Here is my configuration (most relevant parts)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@UsingDataSet(locations = "/data.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public class MyIntegrationTest {
	@SuppressWarnings("unused")
	@Autowired
	private ApplicationContext applicationContext; // required by MongoDbRule(nosql-unit)

	@Rule
	public MongoDbRule mongoDbRule = MongoDbRuleBuilder.newMongoDbRule().defaultSpringMongoDb("mydb");

	@Test
	public void test1() {
...
	}

	@Test
	public void test2() {
...
	}

  @Configuration
  @EnableMongoRepositories("my.repo.packge")
  public static class MongoConfigurationIT extends AbstractMongoConfiguration {

	@Override
	@Bean
	public MongoClient mongoClient() {

		return new MongoClient(...);//returns  a MongoClient initialised from a real running mongo db
	}

	@Override
	protected String getDatabaseName() {
		return "mydb";
	}

	@Override
	protected Collection<String> getMappingBasePackages() {
		return Collections.singleton("my.dom.package");
	}
  }
}

The second running test always failed, because* after the first test execution, the DB connection is closed*, so further attempts to load data failed (due to an assert in MongoDb that checks is connection is open)

Here is the stack trace:

java.lang.IllegalStateException: state should be: open
	at com.mongodb.assertions.Assertions.isTrue(Assertions.java:70)
	at com.mongodb.connection.BaseCluster.getDescription(BaseCluster.java:152)
	at com.mongodb.Mongo.getConnectedClusterDescription(Mongo.java:885)
	at com.mongodb.Mongo.createClientSession(Mongo.java:877)
	at com.mongodb.Mongo$3.getClientSession(Mongo.java:866)
	at com.mongodb.Mongo$3.execute(Mongo.java:823)
	at com.mongodb.MongoIterableImpl.execute(MongoIterableImpl.java:130)
	at com.mongodb.MongoIterableImpl.iterator(MongoIterableImpl.java:77)
	at com.mongodb.MappingIterable.iterator(MappingIterable.java:40)
	at com.lordofthejars.nosqlunit.mongodb.MongoOperation.deleteAllElements(MongoOperation.java:82)
	at com.lordofthejars.nosqlunit.mongodb.MongoOperation.deleteAll(MongoOperation.java:76)
	at com.lordofthejars.nosqlunit.core.CleanInsertLoadStrategyOperation.executeClean(CleanInsertLoadStrategyOperation.java:38)
	at com.lordofthejars.nosqlunit.core.CleanInsertLoadStrategyOperation.executeScripts(CleanInsertLoadStrategyOperation.java:24)
	at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.loadDataSet(AbstractNoSqlTestRule.java:380)
	at com.lordofthejars.nosqlunit.core.AbstractNoSqlTestRule$1.evaluate(AbstractNoSqlTestRule.java:70)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


PS: Note everything works fine when using a in memory db like Fongo

WORKAROUND: add @DirtiesContext to each test method, to force recreating ApplicationContext (thus new a MongoClient is obtained for each test), but i would like a better solution...

@tinesoft
Copy link
Contributor Author

tinesoft commented Apr 9, 2018

Any updates on this? :(

@lordofthejars
Copy link
Owner

@tinesoft
Copy link
Contributor Author

The connection to Mongo obtained from Spring, is closed in AbstractNoSqlTestRule#apply()'s finally block, after the first test method has run. So subsequent attempts to insert data in following tests failed because the connection is closed (in fact, there is a safety assertion: isTrue("open", !isClosed()); in Mongo impl. to ensure that).

In your example, the second test method use the DELETE_ALL strategy (and not a CLEAN_INSERT or INSERT as in my use case): so there is no 2nd attempt to insert data in the db, just a deletion.

Besides, your are using a newMongoDbRule().defaultEmbeddedMongoDb()
to get the MongoDbRule, which internally used a inMemoryMongoDb Fongo implementation (not a real remote db as in my use case...).

As i have mentioned, the Fongo implementation works great, simply because there is no isTrue("open", !isClosed()); assertion as in the Mongo's.

@lordofthejars
Copy link
Owner

I see, I think that the problem happens because of Spring integration, because then the Mongo Client is started by Spring but closed by NoSQLUnit. I will need to figure out if there is a way to detect this

@tinesoft
Copy link
Contributor Author

Yep. Maybe SpringMongoDbRule should override close() to do nothing... and let Spring manage the closing when destroying the context at the end of the tests?

That's the workaround i've implemented for my tests...

@lordofthejars
Copy link
Owner

Yes I think this is right. Can you provide a PR?

@tinesoft tinesoft changed the title Excepion when running multiple tests using MongoDbRule + Spring+Real remote Mongo Db+ CLEAN_INSERT Stratgey Excepion when running multiple tests using MongoDbRule + Spring+Real remote Mongo Db+ CLEAN_INSERT Strategy Apr 11, 2018
tinesoft added a commit to tinesoft/nosql-unit that referenced this issue Apr 11, 2018
@tinesoft
Copy link
Contributor Author

Sure, i will push it tonight

@stefluhh
Copy link

Hello @lordofthejars

when is this fix to be expected to be included into a release?

@lordofthejars
Copy link
Owner

I will try to do today, if I finish one thing :) if not then hope that during this week. Sorry :)

@smirzai
Copy link

smirzai commented Nov 19, 2018

The change does not exist in the latest release of the maven:
https://mvnrepository.com/artifact/com.lordofthejars/nosqlunit-mongodb/1.0.0-rc.5 and it dates back to more than a year ago.
Is there any more recent repository for maven artifacts ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants