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

Support timed transactional TestNG tests in the TestContext framework [SPR-6124] #10792

Closed
spring-projects-issues opened this issue Sep 18, 2009 · 7 comments

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Sep 18, 2009

Ed Randall opened SPR-6124 and commented

Status Quo

The Spring TestContext Framework (TCF) has never supported timed, transactional tests in conjunction with TestNG.

The reason for this is that the transaction managed by the TCF is only active for the currently executing thread; this behavior is in line with standard transaction semantics for Spring. Thus, since TestNG executes the timed test method in a new thread, the test code does not participate in the transaction managed by Spring.

Original Author's Description

If TestNG tests (which extend AbstractTransactionalTestNGSpringContextTests) are run with the timeOut attribute specified in the @Test annotation, they fail at EntityManager.flush() because there is no transaction.

If the timeout is set to zero, they pass.


Analysis

The invokeWithTimeout() method in TestNG's MethodInvocationHelper class spawns a new thread to execute a timed test. Spring's transaction support, however, registers the current transaction resources, etc. in a ThreadLocal which is bound to the thread of the test runner. Thus, the timed test method cannot see Spring's transaction.

Note that MethodInvocationHelper is deep in the internals of TestNG. Thus, it might be difficult to resolve this issue.


Deliverables

  1. Determine if it is possible to integrate the TCF in TestNG so that Spring TestExecutionListeners (specifically TransactionalTestExecutionListener) are executed in the same thread as the timed test.
    • See the Analysis section for further details.

Further Resources


Affects: 2.5.6, 3.0.5, 3.1.2

Attachments:

Issue Links:

  • #22176 Document effect of preemptive timeouts on transactional tests
  • #10800 AbstractTransactionalTestNGSpringContextTests not working as expected when an EJB with TransactionAttribute.REQUIRES_NEW is encountered

Referenced from: commits 1b92a2f

3 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Sam Brannen commented

Hi Ed,

First and foremost: Good Catch!

You're apparently the first person to try to run timed transactional tests with the TestNG integration in the Spring TestContext Framework. ;)

I was able to verify your claim. The cause is that the invokeWithTimeout(...) method in TestNG's MethodHelper class spawns a new thread to execute a timed test. Spring's transaction support, however, registers the current transaction resources, etc. in a ThreadLocal which is bound to the thread of the test runner. Thus, the timed test method cannot see Spring's transaction.

I'm not sure how easy it will be to solve this issue, since MethodHelper is deep in the internals of TestNG; however, we will look into finding a workable solution.

Thanks for bringing this to our attention!

Sam

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Ed Randall commented

Maybe java.lang.InheritableThreadLocal would work?

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 26, 2009

Sam Brannen commented

Good idea. In fact, I'd already tried that as a simple proof of concept using Spring's NamedInheritableThreadLocal in TransactionSynchronizationManager. That appears to do the trick, but making that change in TransactionSynchronizationManager could have far reach reaching (undesirable) consequences outside of the testing framework. Thus, we will have to investigate this topic more thoroughly.

Regards,

Sam

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 29, 2010

Ed Randall commented

#10800 may be related to this bug too.
http://jira.springframework.org/browse/SPR-6132

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 10, 2012

Sam Brannen commented

If you are interested in seeing this issue resolved, you are cordially invited to submit a proposal in the form of a code contribution on GitHub! ;)

For details please refer to the contributor guidelines.

Specifically, as discussed in the Deliverables section above, you would need to:

  • Determine if it is possible to integrate the TCF in TestNG so that Spring TestExecutionListeners (specifically TransactionalTestExecutionListener) are executed in the same thread as the timed test.
    • See the Analysis section for further details.

Regards,

Sam

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 13, 2013

Sam Brannen commented

In line with my previous comment, I have added the pull-request-encouraged label to this issue.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 12, 2019

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

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

Successfully merging a pull request may close this issue.

None yet
2 participants