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

Add the @TestTransaction annotation #11798

Merged
merged 2 commits into from
Sep 7, 2020

Conversation

stuartwdouglas
Copy link
Member

This allows you to run tests in a rollback only transaction.

Fixes #6463

Copy link
Contributor

@mkouba mkouba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea!

Added some comments...

@@ -96,6 +114,40 @@ public void build(NarayanaJtaRecorder recorder,
recorder.setDefaultTimeout(transactions);
}

@BuildStep
void testTx(LaunchModeBuildItem lm, BuildProducer<GeneratedBeanBuildItem> generatedBeanBuildItemBuildProducer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so you generate an interceptor class because you can't add the @TestTransaction interceptor binding, right? Ok, that's probably the simplest solution. However, I'd move as much as possible logic to the parent class, i.e. injection field of UserTransaction and the whole @AroundInvoke method. If it doesn't work then we have a bug in ArC...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work, although I did not have time to look into why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's because the parent class is not part of the index?

@famod
Copy link
Member

famod commented Sep 3, 2020

WDYT about flushing the EntityManager before rolling back? We actually do so in a custom rollback interceptor (some colleagues were used to test rollbacks from Spring).
Otherwise you might not catch certain problems in your tests that occur on auto-flush when committing (e.g. constraint violations etc.).

@stuartwdouglas
Copy link
Member Author

Done

This allows you to run tests in a rollback only transaction.

Fixes quarkusio#6463
@mkouba
Copy link
Contributor

mkouba commented Sep 7, 2020

@stuartwdouglas FYI I'm going to rebase this PR, resolve conflicts and find out why the logic in the parent class did now work...

- add basic support for interceptor's inheritance
- other unsupported use cases are described here:
quarkusio#11942
Copy link
Contributor

@mkouba mkouba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good from my POV.


static final List<TestTransactionCallback> CALLBACKS;

static {
Copy link
Member

@famod famod Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for having considered my flush()-suggestion. Very cool approach with this new TestTransactionInterceptor!

I am wondering whether this block could kick in cases where it is not really needed? Usually ServiceLoader calls are not super-cheap.
I used to apply https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom in my projects but I don't know whether this would make any difference here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the callbacks are only loaded once per test config...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, we could easily turn the list into a io.quarkus.arc.impl.LazyValue<List<TestTransactionCallback>> ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code is fine. ServiceLoader is expensive in terms of 'don't do this every request so you are doing it tens of thousands of times per second', its fine for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for your feedback.

@stuartwdouglas stuartwdouglas merged commit 2f645d4 into quarkusio:master Sep 7, 2020
@famod
Copy link
Member

famod commented Sep 8, 2020

I suppose this is not going to be included in 1.8.0.Final?

@gsmet gsmet added this to the 1.9.0 - master milestone Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@Transactional annotation doesn't work in JUnit tests
4 participants