android-components icon indicating copy to clipboard operation
android-components copied to clipboard

Initialize sentry client lazily

Open pocmo opened this issue 7 years ago • 6 comments

Since we do not need sentry to catch exceptions or anything (just for the upload) we could just initialize it lazily.

  • Sentry does not need to get initialized on app startup
  • We do not need to run any Sentry code until we actually want to upload a crash

https://github.com/mozilla-mobile/android-components/blob/master/components/lib/crash/src/main/java/mozilla/components/lib/crash/service/SentryService.kt#L34-L49

┆Issue is synchronized with this Jira Task

pocmo avatar Jun 14 '19 15:06 pocmo

I would love to do this!

waahm7 avatar Jun 15 '19 03:06 waahm7

Fenix currently accesses Sentry.getContext() directly - we should avoid that (See also #3448): https://github.com/mozilla-mobile/fenix/pull/3484

pocmo avatar Jun 26 '19 13:06 pocmo

Hi @pocmo @Amejia481, I'm making theSentrylib initialization lazy inside theSentryService class, however, after moving it to use a kotlin lazy init, I realized that the tests expect the client to be initialized right away for them to execute the validations successfully.

When analyzing the SentryServiceTest class tests that are failing, they are validating that the client is initialized correctly with the constructor params and that they can be retrieved later.

I'm looking for your opinion on how to proceed on this matter because that kind of testing relies on exposing the wrapped client functionality/variables further beyond what is expected from the CrashReporterService interface.

Second, I think some of these functions' tests should be part of the Sentry codebase rather than the android-components one.

Third, after moving the client to a lazy init, we would need a testing only function to be exposed so that the client is internally initialized and can be verified if we choose to keep the test as how they are now. Unless you think there should be an init function inside the service and is also part of the CrashReporterService interface.

Please let me know what you think would be the best way to proceed.

nicolashleon avatar Jul 03 '19 20:07 nicolashleon

I think some of these functions' tests should be part of the Sentry codebase rather than the android-components one.

Which ones do you mean? Since we only mock SentryClient and only verify that we call the right things on the instance, it seems like those tests belong into our repo?

Third, after moving the client to a lazy init, we would need a testing only function to be exposed so that the client is internally initialized and can be verified if we choose to keep the test as how they are now.

Yeah, that's one option and that sounds okay. I wonder if a better approach would be to just call report() in those tests and assume that at this point SentryClient is initialized the way we expect it to be. The reason for that is that the lazy initialization is kind of an internal implementation detail and if possible we should avoid leaking that into the test.

pocmo avatar Jul 04 '19 10:07 pocmo

I have made the sentry client initialisation lazy Do I need to add a test case for this modification . Or can I push it for pull request

KKS161994 avatar Aug 27 '19 15:08 KKS161994

Sir I have made it lazy and in few test cases I had to invoke .report() of SentryService to make it initialised basically to correct test cases . I have also checked that their are no issues . Please can you have a look at it sir. https://github.com/mozilla-mobile/android-components/pull/4232

KKS161994 avatar Sep 03 '19 09:09 KKS161994