-
Notifications
You must be signed in to change notification settings - Fork 152
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
How to test events from other threads? #1074
Comments
I am neither a JobRunr or Awaitility expert, but the Alternatively — and I realize I might be fully off as I don't have the slightest clue about JobRunr —, as |
Hi Ollie, thank you very much for your answer and your efforts.
Unfortunately, I don't see how the The Scenario is created by the However, this I hope I have recognized and understood the connections correctly. I don't have an easy way to get to the job's thread. From my point of view, it would make sense if all events reach the
I had that thought too, but I haven't found an easy way to control the thread creation. At the moment I am making do with:
|
I don't quite understand how JobRunr plays into this yet. Maybe we can get @rdehuyss' input on this, how one would set it up to align with a Spring provided threading arrangement? |
In my opinion, JobRunr does not play a special role here, but is just an example of when events are published from an independent thread. JobRunr now happens to be what I chose. I would find it a bit strange if I had to adapt the productive code (to which the framework belongs) so that the test framework works with it. Here I would rather expect flexibility and customization in the test framework. |
How is the comment on the ThreadBoundApplicationListenerAdapter class to be understood?
I can't see how you could register multiple event listeners and somehow check for specific threads? Or does the comment only refer to the Modulith internal implementation? |
The way the async event listener invocations work is the following. The executing thread (the one running your test) spawns new threads to then execute the listener methods on (that's how Zooming out a bit again,
means, in this context, how JobRunr handles threads and how they're connected to the thread executing the tests.
I don't think we can do that, as |
Thank you for the detailed explanation.
Please excuse the inaccuracy. I just wanted to say that in my code a method with The threads created by JobRunr do not appear to be connected to the thread that executes the test. JobRunr persists the jobs and then probably executes them with a separate execution unit. The execution could also take place after a shutdown. For this reason, it seems logical to me that the threads are independent.
Ahh, now a light is dawning on me and I understand the connections around the With these contexts in mind, the solution with the “auxiliary listener” seems to me to be the most pragmatic. |
Hi @odrotbohm, @jekutzsche - I'm reading through this thread but I don't know how I can be of any assistance? To give a bit of context of how JobRunr works: it has it's own managed thread pool and just takes jobs of the queue to process and runs them on the threads of the thread pool. There is indeed no link (except for the MDC) to the original thread that created the job. |
Hi @rdehuyss, thank you for your explanations. I now at least understand the background. |
@odrotbohm I think it would be good if the documentation and the JavaDocs pointed out the pitfall discussed here. I can't imagine that checking for events from independent threads is a rare side issue. |
Hi, I am testing the response to events using
org.springframework.modulith.test.Scenario
. The test publishes an event, my code processes it and should again publish events that the test checks for. The processing and publishing of the new events should be done asynchronously using JobRunr.If events are now published via the
ApplicationEventPublisher
within a separate thread (the JobRunr job), thenScenario.andWaitForEventOfType
does not react to this in the tests.If I have found this out correctly, it is because the
ThreadBoundApplicationListenerAdapter
used by thePublishedEventsParameterResolver
has not registered a listener for the JobRunr thread. A listener is only registered for the main thread in the test. I now assume that it works with the threads of@Async
(the test works there without any problems, although it is also a different thread) because they inherit from Main. However, this does not seem to be the case with JobRunr threads.From the comments on
ThreadBoundApplicationListenerAdapter
, I assume that this behavior is intended. However, I have not found a way to wait correctly for the events from the JobRunr threads. What is the concept behind this and what is the best way to implement the tests?The text was updated successfully, but these errors were encountered: