-
Notifications
You must be signed in to change notification settings - Fork 61
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
feat: Identity API response caching #513
Open
Mansi-mParticle
wants to merge
12
commits into
development
Choose a base branch
from
feat/SQDSDKS-6673_identity_caching_2
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b3b63f7
feat: Identity API response caching
Mansi-mParticle 529c725
Add Remaining unit test cases
Mansi-mParticle 983175f
Removed other testcases
Mansi-mParticle efad64c
refactor: Migrate Internal OS Helper class to kotlin (#515)
Mansi-mParticle a8efd13
refactor: Migrate Internal user storage class to kotlin (#516)
Mansi-mParticle b6ab438
refactor: Migrate Internal CoreCallback class to kotlin (#517)
Mansi-mParticle 268d850
refactor: Migrate Internal Application context class to kotlin (#511)
Mansi-mParticle 669ae88
fix: Testcase related to code refactor (#519)
Mansi-mParticle fa9b79b
fix: previous Google ad Id from being null on cold launch (#518)
Mansi-mParticle 4eb7566
Merge remote-tracking branch 'origin/development' into feat/SQDSDKS-6…
Mansi-mParticle 2e832ca
Address review comments
Mansi-mParticle 9d432bd
Address review comments and change algorithm logic
Mansi-mParticle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.mparticle.identity | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.MutableBoolean | ||
import com.mparticle.MParticle | ||
import com.mparticle.internal.ConfigManager | ||
|
@@ -16,6 +17,8 @@ import org.junit.Assert | |
import org.junit.Before | ||
import org.junit.Test | ||
import java.io.IOException | ||
import java.lang.reflect.Field | ||
import java.lang.reflect.Method | ||
import java.util.concurrent.CountDownLatch | ||
|
||
class MParticleIdentityClientImplTest : BaseCleanStartedEachTest() { | ||
|
@@ -57,6 +60,202 @@ class MParticleIdentityClientImplTest : BaseCleanStartedEachTest() { | |
Assert.assertTrue(called.value) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testLoginWithTwoDifferentUsers() { | ||
// clear existing catch | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(2) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
// Login with First User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
// Login With second User | ||
MParticle.getInstance()?.Identity()?.login(IdentityApiRequest.withEmptyUser().build())?.addSuccessListener { | ||
val currentLoginRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(2, currentLoginRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
|
||
latch.await() | ||
Assert.assertTrue(called.value) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testLoginWithTwoSameUsers() { | ||
// clear existing catch | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(2) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
// Login with First User | ||
Thread { | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
// Login With same User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
val currentLoginRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(1, currentLoginRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
latch.await() | ||
Assert.assertTrue(called.value) | ||
}.start() | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testLoginWithTwoSameUsers_withLogout() { | ||
// clear existing catch | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(3) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
// Login with First User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
MParticle.getInstance()?.Identity()?.logout()?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
// Login With same User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
val currentLoginRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(2, currentLoginRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
latch.await() | ||
Assert.assertTrue(called.value) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testLoginAndIdentitySameUser() { | ||
// clear existing catch | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(2) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
// Login with First User | ||
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
// Login With same User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
val currentLoginRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(1, currentLoginRequestCount) | ||
val currentIdentityRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(1, currentIdentityRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
latch.await() | ||
Assert.assertTrue(called.value) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testTwoIdentitySameUser_WithModify() { | ||
// clear existing catch | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(3) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
val identityRequestModify = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
|
||
// Identity with First User | ||
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
MParticle.getInstance()?.Identity()?.modify(identityRequestModify)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
// Identity With same User | ||
MParticle.getInstance()?.Identity()?.identify(identityRequest)?.addSuccessListener { | ||
val currentIdentityRequestCount = mServer.Requests().identify.size | ||
Assert.assertEquals(3, currentIdentityRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
latch.await() | ||
Assert.assertTrue(called.value) | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testLoginWithTwoSameUsers_WithTimeout() { | ||
// clear existing catch | ||
val mParticleIdentityClient = MParticleIdentityClientImpl( | ||
mContext, | ||
mConfigManager, | ||
MParticle.OperatingSystem.ANDROID | ||
) | ||
clearIdentityCache() | ||
val latch: CountDownLatch = MPLatch(2) | ||
val handler = Handler(Looper.getMainLooper()) | ||
handler.postDelayed({ Assert.fail("Process not complete") }, (10 * 1000).toLong()) | ||
val called = AndroidUtils.Mutable(false) | ||
val identityRequest = IdentityApiRequest.withEmptyUser() | ||
.email("[email protected]") | ||
.customerId("TestUser777777") | ||
.build() | ||
// Login with First User | ||
Thread { | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
latch.countDown() | ||
} | ||
|
||
val field: Field = | ||
MParticleIdentityClientImpl::class.java.getDeclaredField("identityCacheTime") | ||
field.isAccessible = true | ||
field.set(mParticleIdentityClient, 0L) | ||
// Login With same User | ||
MParticle.getInstance()?.Identity()?.login(identityRequest)?.addSuccessListener { | ||
val currentLoginRequestCount = mServer.Requests().login.size | ||
Assert.assertEquals(2, currentLoginRequestCount) | ||
called.value = true | ||
latch.countDown() | ||
} | ||
latch.await() | ||
Assert.assertTrue(called.value) | ||
}.start() | ||
} | ||
|
||
@Test | ||
@Throws(Exception::class) | ||
fun testIdentifyMessage() { | ||
|
@@ -309,6 +508,17 @@ class MParticleIdentityClientImplTest : BaseCleanStartedEachTest() { | |
} | ||
MParticle.getInstance()?.Identity()?.apiClient = mApiClient | ||
} | ||
private fun clearIdentityCache() { | ||
val mParticleIdentityClient = MParticleIdentityClientImpl( | ||
mContext, | ||
mConfigManager, | ||
MParticle.OperatingSystem.ANDROID | ||
) | ||
|
||
val method: Method = MParticleIdentityClientImpl::class.java.getDeclaredMethod("clearCatch") | ||
method.isAccessible = true | ||
method.invoke(mParticleIdentityClient) | ||
} | ||
|
||
@Throws(JSONException::class) | ||
private fun checkStaticsAndRemove(knowIdentites: JSONObject) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -21,6 +21,8 @@ | |||
import com.mparticle.internal.MessageManager; | ||||
import com.mparticle.internal.listeners.ApiClass; | ||||
|
||||
import java.security.MessageDigest; | ||||
import java.security.NoSuchAlgorithmException; | ||||
import java.util.ArrayList; | ||||
import java.util.Collections; | ||||
import java.util.Comparator; | ||||
|
@@ -348,6 +350,26 @@ private void reset() { | |||
} | ||||
} | ||||
|
||||
private String generateHash(String input) { | ||||
try { | ||||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||||
byte[] hashBytes = digest.digest(input.getBytes()); | ||||
|
||||
// Convert byte array to hex string | ||||
StringBuilder hexString = new StringBuilder(); | ||||
for (byte b : hashBytes) { | ||||
String hex = Integer.toHexString(0xff & b); | ||||
if (hex.length() == 1) hexString.append('0'); | ||||
hexString.append(hex); | ||||
} | ||||
return hexString.toString(); | ||||
} catch (NoSuchAlgorithmException e) { | ||||
//throw new RuntimeException("Hashing algorithm not found", e); | ||||
Mansi-mParticle marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
} | ||||
return null; | ||||
|
||||
} | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also if we removed this extra line change, the whole file will be removed from the PR as it's not actually being changed |
||||
private BaseIdentityTask makeIdentityRequest(IdentityApiRequest request, final IdentityNetworkRequestRunnable networkRequest) { | ||||
if (request == null) { | ||||
request = IdentityApiRequest.withEmptyUser().build(); | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looks like these imports aren't required