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

Refactor singleton service initializers #172

Open
wants to merge 25 commits into
base: rel/3.0.0
Choose a base branch
from

Conversation

evan-masseau
Copy link
Contributor

@evan-masseau evan-masseau commented May 24, 2024

Description

Update: Some of the work originally in this PR wound up in the 2.4 release instead. I've now merged master into rel/3.0 and then merged that into this branch. At this point, this PR is some lower priority changes to some service initializers and a lot of test fixture naming convention corrections.

We had some assumptions about the order in which dependencies are initialized, and encountered unrecoverable exceptions when those assumptions were incorrect. Most importantly, accessing KlaviyoApiClient before applicationContext was available would throw an exception in the initializer. An initialization error cannot be recovered, even if applicationContext were later provided, because init will only run once.

Check List

  • Are you changing anything with the public API?
  • Are your changes backwards compatible with previous SDK Versions?
  • Have you tested this change on real device?
  • Have you added unit test coverage for your changes?
  • Have you verified that your changes are compatible with all Android versions the SDK currently supports?

Changelog / Code Overview

As a safety precaution, I've removed all the object initializers that had an implicit dependency, though the API client was the only one I've observed an issue with from testing with our test apps.

Test Plan

Updated all the unit tests already
Functional changes are smaller now that some of this branch's original work was cherry picked and released in 2.4 after testing.

Related Issues/Tickets

CHNL-8569 -- this does not address all the AC of that ticket.

Evan Masseau added 8 commits May 23, 2024 10:46
…t rid of objects, just have to be more particular about using object initializers.
…KlaviyoApiClient being a singleton object that can hit an unrecoverable exception in its initializer...
…a class was complicated by the inner class, and besides it works well as a singleton. The initializer was the real problem, so I extract the listeners from the initializer and created a startService method. This is safe to run multiple times if the SDK is re-initialized.
… body except the methods that are themselves protected anyway
@evan-masseau evan-masseau requested a review from a team as a code owner May 24, 2024 18:40
@evan-masseau evan-masseau changed the title Ecm/services refactor Refactor singleton service initializers May 24, 2024
@evan-masseau evan-masseau changed the base branch from master to rel/3.0.0 May 24, 2024 18:40
@@ -61,34 +61,35 @@ internal class KlaviyoTest : BaseTest() {
}

private val capturedProfile = slot<Profile>()
private val apiClientMock: ApiClient = mockk<ApiClient>().apply {
private val mockApiClient: ApiClient = mockk<ApiClient>().apply {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Naming convention fix, sometimes using mock as a prefix, sometimes as a suffix. Suggesting we consolidate to prefix.

@@ -6,7 +6,26 @@ import com.klaviyo.core.config.Log
import com.klaviyo.core.config.Log.Level
import java.util.regex.Pattern

open class KLog : Log {
object KLog : Log {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

KLog doesn't need to be instantiated, it is essentially just a set of static methods anyway.

@@ -14,10 +14,6 @@ internal object KlaviyoLifecycleMonitor : LifecycleMonitor, Application.Activity

private var activityObservers = mutableListOf<ActivityObserver>()

init {
onActivityEvent { Registry.log.verbose(it.type) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one wasn't causing any exceptions, but I thought it best to get rid of any unnecessary object inits to head off the same issues.

@evan-masseau evan-masseau added the major-release For PRs that contain breaking changes and belong in a major release label Jun 4, 2024
@evan-masseau evan-masseau marked this pull request as draft June 4, 2024 15:57
@evan-masseau
Copy link
Contributor Author

Reverted to draft, will need to update once 2.4.0 is release. I think these changes were still valuable.

Evan Masseau added 3 commits June 11, 2024 10:56
# Conflicts:
#	README.md
#	docs/index.html
#	sdk/analytics/src/main/java/com/klaviyo/analytics/Klaviyo.kt
#	sdk/analytics/src/main/java/com/klaviyo/analytics/networking/KlaviyoApiClient.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoPreInitializeTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoUninitializedTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/KlaviyoApiClientTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/requests/KlaviyoApiRequestTest.kt
#	versions.properties
# Conflicts:
#	sdk/analytics/src/main/java/com/klaviyo/analytics/Klaviyo.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/KlaviyoUninitializedTest.kt
#	sdk/analytics/src/test/java/com/klaviyo/analytics/networking/requests/KlaviyoApiRequestTest.kt
Copy link
Contributor Author

@evan-masseau evan-masseau left a comment

Choose a reason for hiding this comment

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

Merged master into 3.0 and that back into this branch, after the 2.4 release.
At this point, this PR is some lower priority changes to some service initializers and a lot of test fixture naming convention corrections.

@evan-masseau evan-masseau marked this pull request as ready for review June 11, 2024 15:52
Copy link

This PR has not seen any updates in the last 16 days. Without further action this PR will be closed in 14 days. To disable further staleness checks add the evergreen label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
evergreen major-release For PRs that contain breaking changes and belong in a major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants