micronaut-groovy icon indicating copy to clipboard operation
micronaut-groovy copied to clipboard

MongoDatastore is closed too early

Open Doogiemuc opened this issue 4 years ago • 1 comments

I have a Micronaut Backend API with some simple REST resources. Implemented in Groovy. The backend uses a (local) mongoDB. The backend services access this mongoDB with GORM for mongoDB

I have two spock test classes with some simpl tests that send REST requests to that backend. Both test classes inject the MongoDatastore to directly access the DB where needed. For example to pre-fill the DB with test data.

When I run the test classes individually, one at a time, the tests in each class run successfully. But when I run both classes together with gradle test one fails with the following error:

First test class: HappyCase.groovy

@MicronautTest
@Slf4j
class HappyCase extends Specification {
	@Shared
	@AutoCleanup
	EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)

	@Shared
	@AutoCleanup
	BlockingHttpClient client = HttpClient.create(embeddedServer.URL).toBlocking()

	@Inject
	MongoDatastore mongoDatastore

       [... test methods ...]
}

The second TestClass has the same fields.

Error message

java.lang.IllegalStateException: state should be: open

The stacktrace always leads back to a line of code where the backend access one of my GORM Models, e.g. with a finder method.

  @Put("/joinTeam")
  @Secured(SecurityRule.IS_ANONYMOUS)
    HttpResponse joinTeam(@Body @Valid JoinTeamRequest req) {
    // Remark: Team.class is one of my GORM @Entity 
    Team team = Team.findByInviteCode(req.inviteCode)            // <=== this  is where the error is thrown
    [...]
  }

My assumption would be that Micronaut already closes the connection to the DB via the injected mongoDatastore, while the other test is still running. But that is just a guess.

Environment Information

  • Operating System: Windows
  • Micronaut Version:
  • JDK Version: jdk 13.0.1

GitHub Repo

https://github.com/Doogiemuc/liquido-micronaut-groovy

Direct Links:

Things I alreay tried

  • Adding/Removing @Shared does not help
  • Adding/Removing @AutoCleanup (or in combination with or without @Shared) does not help
  • Running from within IntelliJ gives the same effect: Run one test class works. Run both at the same time does not work
  • Adding packe of Application.class to @MicronautTest annotation (mabe) for better class path scanning does not help: @MicronautTest(application = org.doogie.Application.class)

(Please tell me if I can add any more information to this ticket.)

Doogiemuc avatar Oct 05 '20 08:10 Doogiemuc

I found a workaround:

Team team = Team.find(Filters.eq("inviteCode", req.inviteCode)).first()     // this works
//Team team = Team.findByInviteCode(req.inviteCode)  // BUG: throws "Internal Server Error: state should be: open"

So in the end this looks like a Groovy Mongo GORM bug ...

Doogiemuc avatar Oct 25 '20 21:10 Doogiemuc