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

fix: remove a data race when auto-generated device id is used instead of custom device id #156

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ class AndroidContextPlugin : Plugin {
if (!configuration.newDeviceIdPerInstall && configuration.useAdvertisingIdForDeviceId && !contextProvider.isLimitAdTrackingEnabled()) {
val advertisingId = contextProvider.advertisingId
if (advertisingId != null && validDeviceId(advertisingId)) {
amplitude.setDeviceId(advertisingId)
setDeviceId(advertisingId)
return
}
}
if (configuration.useAppSetIdForDeviceId) {
val appSetId = contextProvider.appSetId
if (appSetId != null && validDeviceId(appSetId)) {
amplitude.setDeviceId("${appSetId}S")
setDeviceId("${appSetId}S")
return
}
}
val randomId = AndroidContextProvider.generateUUID() + "R"
amplitude.setDeviceId(randomId)
setDeviceId(randomId)
}

private fun applyContextData(event: BaseEvent) {
Expand Down Expand Up @@ -144,6 +144,10 @@ class AndroidContextPlugin : Plugin {
}
}

private fun setDeviceId(deviceId: String) {
amplitude.idContainer.identityManager.editIdentity().setDeviceId(deviceId).commit()
}

companion object {
const val PLATFORM = "Android"
const val SDK_LIBRARY = "amplitude-analytics-android"
Expand Down
41 changes: 41 additions & 0 deletions android/src/test/java/com/amplitude/android/AmplitudeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ class AmplitudeTest {
}
}

@Test
fun test_setDeviceId_generatedDeviceIdShouldBeUsed() = runTest {
setDispatcher(testScheduler)
val n = 100
val clients = arrayOfNulls<Amplitude>(n)
for (i in 0 until n) {
val configuration = createConfiguration()
configuration.instanceName = "generated-instance-$i"
val amplitude = Amplitude(configuration)
clients[i] = amplitude
}

advanceUntilIdle()
Thread.sleep(1000)

for (i in 0 until n) {
Assertions.assertTrue(clients[i]?.getDeviceId()?.endsWith('R') ?: false)
}
}

@Test
fun test_setDeviceId_customDeviceIdShouldBeUsed() = runTest {
setDispatcher(testScheduler)
val n = 100
val clients = mutableListOf<Amplitude>()
for (i in 0 until n) {
val configuration = createConfiguration()
configuration.instanceName = "custom-instance-$i"
val amplitude = Amplitude(configuration)
amplitude.setDeviceId("custom-device-$i")
clients.add(amplitude)
}

advanceUntilIdle()
Thread.sleep(1000)

for (i in 0 until n) {
Assertions.assertEquals("custom-device-$i", clients[i].getDeviceId())
}
}

companion object {
const val instanceName = "testInstance"
}
Expand Down