-
Notifications
You must be signed in to change notification settings - Fork 112
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
Fix NPE in TestRunSession#toString and use AtomicReference #1213
base: master
Are you sure you want to change the base?
Conversation
20a536b
to
eef9d7c
Compare
Test Results1 154 files - 577 1 154 suites - 577 51m 54s ⏱️ - 31m 22s Results for commit b04304b. ± Comparison against base commit a2ad0b3. This pull request skips 1 test.
♻️ This comment has been updated with latest results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for AtomicReference
"This class is immutable and thread-safe."
https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
the rest looks good.
The class is threadsafe, but not the access to its instance. |
eef9d7c
to
4fb77cc
Compare
That was done by the volatile keyword. |
Even though I explained it already multiple times,
then If one needs this (and that's the case here) you need an atomic compare-and-set operation, what is offered by the |
There is no need to prevent two threads from writing into it in this case. Even if both multiple threads write a value it would be the same timestamp +-1. Here is no condition that relies on a atomic write. |
It is this code badly fails in some cases when test are running to fast or from different threads, so why should I want a weaker guarantee if I can have a stronger one that can be used with more features. volatile is about memory effects, AtomicReference is about concurrency and I want to solve concurrency problems here. |
That is not reflected by the commit message/PR title. If you list a problem that is solved by this change it would be OK for me. But it seems to be totally over engineered to fix the NPE during debug. |
currently if one calls TestRunSession#toString (e.g. in a debugger) a NPE can occur if the session is not yet started and the fStartTime is null. Also it is possible that fStartTime is concurrently set. This checks for this case adjust the message accordingly and also replace the volatile field with an AtomicReference to ensure the start time is only ever set by the first thread, alongside with that an unnecessary synchronized is removed from the method addTestSessionListener as ListenerList is already synchronized.
4fb77cc
to
b04304b
Compare
I adjusted the commit message now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one comment on a coding practice, the rest looks good.
Thank you, @laeubi , for contributing this!
if (startTime == null) { | ||
return fTestRunName + " (not started)"; //$NON-NLS-1$ | ||
} else { | ||
return fTestRunName + " " + DateFormat.getDateTimeInstance().format(new Date(startTime.toEpochMilli())); //$NON-NLS-1$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move outside else
Currently if one calls TestRunSession#toString (e.g. in a debugger) a
NPE can occur if the session is not yet started and the fStartTime is
null. Also it is possible that fStartTime is concurrently set.
This checks for this case adjust the message accordingly and also
replace the volatile field with an AtomicReference to ensure the start
time is only ever set by the first thread, alongside with that an
unnecessary synchronized is removed from the method
addTestSessionListener as ListenerList is already synchronized.