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

feat: Identity API response caching #513

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions android-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ android {
jvmArgs += ['--add-opens', 'java.base/java.text=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.math=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.util.concurrent=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.util.concurrent.atomic=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.lang.ref=ALL-UNNAMED']
}
if (useOrchestrator()) {
Expand Down Expand Up @@ -158,6 +159,7 @@ dependencies {
testImplementation project(':testutils')
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"

androidTestImplementation project(':testutils')
if (useOrchestrator()) {
Expand All @@ -166,6 +168,7 @@ dependencies {
}
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class SessionMessagesTest : BaseCleanStartedEachTest() {
fun testSessionStartMessage() {
val sessionStartReceived = BooleanArray(1)
sessionStartReceived[0] = false
Assert.assertFalse(mAppStateManager.session.isActive)
Assert.assertFalse(mAppStateManager.fetchSession().isActive)
val sessionId = AndroidUtils.Mutable<String?>(null)
mAppStateManager.ensureActiveSession()
sessionId.value = mAppStateManager.session.mSessionID
sessionId.value = mAppStateManager.fetchSession().mSessionID
AccessUtils.awaitMessageHandler()
MParticle.getInstance()?.upload()
mServer.waitForVerify(
Expand All @@ -45,14 +45,14 @@ class SessionMessagesTest : BaseCleanStartedEachTest() {
if (eventObject.getString("dt") == Constants.MessageType.SESSION_START) {
Assert.assertEquals(
eventObject.getLong("ct").toFloat(),
mAppStateManager.session.mSessionStartTime.toFloat(),
mAppStateManager.fetchSession().mSessionStartTime.toFloat(),
1000f
)
Assert.assertEquals(
"""started sessionID = ${sessionId.value}
current sessionId = ${mAppStateManager.session.mSessionID}
current sessionId = ${mAppStateManager.fetchSession().mSessionID}
sent sessionId = ${eventObject.getString("id")}""",
mAppStateManager.session.mSessionID,
mAppStateManager.fetchSession().mSessionID,
eventObject.getString("id")
)
sessionStartReceived[0] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Looper
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.internal.ConfigManager
import com.mparticle.internal.Logger
import com.mparticle.networking.Matcher
import com.mparticle.testutils.AndroidUtils
import com.mparticle.testutils.BaseCleanStartedEachTest
Expand Down Expand Up @@ -96,8 +97,9 @@ class IdentityApiTest : BaseCleanStartedEachTest() {
val user2Called = AndroidUtils.Mutable(false)
val user3Called = AndroidUtils.Mutable(false)
val latch: CountDownLatch = MPLatch(3)

MParticle.getInstance()!!.Identity().addIdentityStateListener { user, previousUser ->
if (user != null && user.id == mpid1) {
if (user != null && user.id == mStartingMpid) {
user1Called.value = true
latch.countDown()
}
Expand All @@ -111,26 +113,23 @@ class IdentityApiTest : BaseCleanStartedEachTest() {

// test that change actually took place
result.addSuccessListener { identityApiResult ->
Assert.assertEquals(identityApiResult.user.id, mpid1)
Assert.assertEquals(identityApiResult.previousUser!!.id, mStartingMpid.toLong())
Assert.assertEquals(identityApiResult.user.id, mStartingMpid)
}
com.mparticle.internal.AccessUtils.awaitUploadHandler()
request = IdentityApiRequest.withEmptyUser().build()
result = MParticle.getInstance()!!.Identity().identify(request)
result.addSuccessListener { identityApiResult ->
Assert.assertEquals(identityApiResult.user.id, mpid2)
Assert.assertEquals(identityApiResult.user.id, mStartingMpid)
Assert.assertEquals(
identityApiResult.user.id,
MParticle.getInstance()!!
.Identity().currentUser!!.id
)
Assert.assertEquals(identityApiResult.previousUser!!.id, mpid1)
latch.countDown()
user3Called.value = true
}
latch.await()
Assert.assertTrue(user1Called.value)
Assert.assertTrue(user2Called.value)
Assert.assertTrue(user3Called.value)
}

@Test
Expand Down
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
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.mparticle.internal.database.services.AccessUtils
import com.mparticle.internal.database.services.MParticleDBManager
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.json.JSONException
import org.junit.Assert
import org.junit.Before
Expand All @@ -33,7 +35,7 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
}
mAppStateManager?.ensureActiveSession()
for (mpid in mpids) {
mAppStateManager?.session?.addMpid(mpid)
mAppStateManager?.fetchSession()?.addMpid(mpid)
}
val checked = BooleanArray(1)
val latch: CountDownLatch = MPLatch(1)
Expand Down Expand Up @@ -72,7 +74,7 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
mpids.add(Constants.TEMPORARY_MPID)
mAppStateManager?.ensureActiveSession()
for (mpid in mpids) {
mAppStateManager?.session?.addMpid(mpid)
mAppStateManager?.fetchSession()?.addMpid(mpid)
}
val latch: CountDownLatch = MPLatch(1)
val checked = MutableBoolean(false)
Expand Down Expand Up @@ -104,7 +106,7 @@ class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {

@Test
@Throws(InterruptedException::class)
fun testOnApplicationForeground() {
fun testOnApplicationForeground() = runTest(StandardTestDispatcher()) {
val latch: CountDownLatch = MPLatch(2)
val kitManagerTester = KitManagerTester(mContext, latch)
com.mparticle.AccessUtils.setKitManager(kitManagerTester)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class BatchSessionInfoTest : BaseCleanStartedEachTest() {

AccessUtils.awaitMessageHandler()
MParticle.getInstance()?.Internal()?.apply {
val sessionId = appStateManager.session.mSessionID
val sessionId = appStateManager.fetchSession().mSessionID
appStateManager.endSession()
appStateManager.ensureActiveSession()
InstallReferrerHelper.setInstallReferrer(mContext, "222")
assertNotEquals(sessionId, appStateManager.session.mSessionID)
assertNotEquals(sessionId, appStateManager.fetchSession().mSessionID)
}

var messageCount = 0
Expand Down
Loading
Loading