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

EMAIL-204: Disable eager attachment loading on MimeMessageParser #135

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

HiuKwok
Copy link

@HiuKwok HiuKwok commented Feb 6, 2023

As discussed on the Jira ticket, this is the MR to update MimeMessageParser.createDataSource() to disable eager loading for email attachments, but instead keep the inputstream as a reference and only load the attachment binary into memory when .getInputStream() is called.

@garydgregory
Copy link
Member

garydgregory commented Feb 6, 2023

Would you please write a unit test that fails without the change to main?

@codecov-commenter
Copy link

codecov-commenter commented Feb 6, 2023

Codecov Report

Merging #135 (094a2b3) into master (347d5d9) will decrease coverage by 0.07%.
The diff coverage is 92.30%.

@@             Coverage Diff              @@
##             master     #135      +/-   ##
============================================
- Coverage     65.90%   65.84%   -0.07%     
- Complexity      305      308       +3     
============================================
  Files            18       19       +1     
  Lines          1053     1054       +1     
  Branches        138      137       -1     
============================================
  Hits            694      694              
- Misses          280      281       +1     
  Partials         79       79              
Impacted Files Coverage Δ
...g/apache/commons/mail/LazyByteArrayDataSource.java 91.66% <91.66%> (ø)
...rg/apache/commons/mail/util/MimeMessageParser.java 84.14% <100.00%> (-1.88%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@HiuKwok
Copy link
Author

HiuKwok commented Feb 6, 2023

@garydgregory Can you also advise on how unit can be written in this case?
As this is a performance improvement rather than a bug fix, hence this is no change of behavior from an external perspective I can assert.
I'm thinking to create a huge .eml file with an attachment in order to trigger an OOM exception as the original approach will load the whole file into the memory, which the revised method only loads a portion of the file into the input stream.
But I don't think that makes sense to push a huge .eml under the test resources folder either.

@HiuKwok
Copy link
Author

HiuKwok commented Feb 6, 2023

Actually, I can probably create a test to assert the object size of mimeMessageParser, the memory consumption should reduce after the revised code change.

@garydgregory
Copy link
Member

Would mocking help here?

@HiuKwok
Copy link
Author

HiuKwok commented Feb 8, 2023

I reckon so, to have a test check on .getInputStream() metho invoke count and make sure it is only called when the user is accessing the attachment content.
I will update this MR once again with the test.

@HiuKwok
Copy link
Author

HiuKwok commented Feb 27, 2023

This is not yet ready, I'm working on the test-cases

@HiuKwok
Copy link
Author

HiuKwok commented Mar 2, 2023

@garydgregory I have updated the code change once again with two new test cases to represent the scenarios of lazy loading of Email attachments with appropriate mocking.
Would that be possible for you to review it again?
Appreciate that.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

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

The class comment for the new lazy class should state why can't we reuse org.apache.commons.mail.ByteArrayDataSource or javax.mail.util.ByteArrayDataSource

@HiuKwok
Copy link
Author

HiuKwok commented Mar 4, 2023

I believe all comments are addressed, let me know if I missed any, thanks!

@HiuKwok HiuKwok requested a review from garydgregory March 4, 2023 10:54
@garydgregory
Copy link
Member

@HiuKwok
The build fails. Run 'mvn' on the command line before you push. Or, did you not see failures on your set up? OS? Java version?

@HiuKwok
Copy link
Author

HiuKwok commented Mar 4, 2023

Yes, my local build is green and I tried the exact commands used for the failed builds, which all build successfully (I couldn't reproduce this on my local).
By checking on the error message it seems related to the Easymock, let me investigate, and will update you once again on this thread.

BTW my setup:
OS: MAC OS 12.15.1
Java: Open JDK 17.0.2
MVN: 3.8.5

@garydgregory
Copy link
Member

I would try locally with Java 8.

@HiuKwok
Copy link
Author

HiuKwok commented Mar 4, 2023

Since it failed on Java 8 test, I will install Java8 locally and rerun the test.

@HiuKwok
Copy link
Author

HiuKwok commented Mar 4, 2023

@garydgregory I can now reproduce the issue after running the unit test locally with jdk8.
I will check and update the test case accordingly if need.
Thx for the help!

@HiuKwok
Copy link
Author

HiuKwok commented Mar 4, 2023

It turns out the easymock is having issues mocking the class DataHandler,
for now, I have replaced the mock of the class with a constructor instead.

final DataHandler dataHandler = new DataHandler(dataSource);

I have re-run the test suite locally against JDK8, 11 and 17 and all tests are passing for all three versions :)

@HiuKwok
Copy link
Author

HiuKwok commented Mar 18, 2023

@garydgregory all comments are addressed and I have fixed the test cases.
Would that be possible to have another round of review?

Thanks,

@garydgregory
Copy link
Member

garydgregory commented Dec 16, 2023

Hello @HiuKwok,

I am sorry about not getting back to you sooner.
I belive this functionality is currently implemented (see InputStreamDataSource), albeit differently, in git master. Please verify your use case and close this or update this PR as appropriate.

Thank you for your patience! 😄

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

Successfully merging this pull request may close these issues.

3 participants