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

Alter how threshold works for disabling the SDK #1689

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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 @@ -10,6 +10,10 @@ class BehaviorThresholdCheck(
private val deviceIdProvider: Provider<String>,
) {

private companion object {
private const val DIGITS = 6
}

/**
* An implementation of [isBehaviorEnabled] that returns null if the pctEnabled parameter
* is null.
Expand Down Expand Up @@ -40,26 +44,16 @@ class BehaviorThresholdCheck(
if (pctEnabled <= 0 || pctEnabled > 100) {
return false
}
val deviceId = getNormalizedLargeDeviceId()
val deviceId = getNormalizedDeviceId()
return pctEnabled >= deviceId
}

fun getNormalizedLargeDeviceId(): Float = getNormalizedDeviceId(6)

/**
* Use [.isBehaviorEnabled] instead as it allows rollouts to be controlled
* at greater granularity.
*/
@Deprecated("")
fun getNormalizedDeviceId(): Float = getNormalizedDeviceId(2)

private fun getNormalizedDeviceId(digits: Int): Float {
fun getNormalizedDeviceId(): Float {
val deviceId = deviceIdProvider()
val finalChars = deviceId.substring(deviceId.length - digits)

val finalChars = deviceId.substring(deviceId.length - DIGITS)
// Normalize the device ID to a value between 0.0 - 100.0
val radix = 16
val space = (radix.toDouble().pow(digits.toDouble()) - 1).toInt()
val space = (radix.toDouble().pow(DIGITS.toDouble()) - 1).toInt()
val value = Integer.valueOf(finalChars, radix)
return value.toFloat() / space * 100
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class SdkModeBehaviorImpl(
private fun getOffset(): Int = remote?.offset ?: DEFAULT_OFFSET

override fun isSdkDisabled(): Boolean {
@Suppress("DEPRECATION")
val result = thresholdCheck.getNormalizedDeviceId()
// Check if this is lower than the threshold, to determine whether
// we should enable/disable the SDK.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,19 @@ internal class ConfigServiceImplTest {
clearAllMocks()
}

@Suppress("DEPRECATION")
@Test
fun `test legacy normalized DeviceId`() {
fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC0D0700"
assertEquals(0.0, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC0D07FF"
assertEquals(100.0, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC0D0739"
assertEquals(22.35, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC0D07D9"
assertEquals(85.09, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)
}

@Test
fun `test new normalized DeviceId`() {
fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC000000"
assertEquals(0.0, thresholdCheck.getNormalizedLargeDeviceId().toDouble(), 0.01)
assertEquals(0.0, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFCFFFFFF"
assertEquals(100.0, thresholdCheck.getNormalizedLargeDeviceId().toDouble(), 0.01)
assertEquals(100.0, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFC0D0739"
assertEquals(5.08, thresholdCheck.getNormalizedLargeDeviceId().toDouble(), 0.01)
assertEquals(5.08, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)

fakePreferenceService.deviceIdentifier = "07D85B44E4E245F4A30E559BFCED0739"
assertEquals(92.58, thresholdCheck.getNormalizedLargeDeviceId().toDouble(), 0.01)
assertEquals(92.58, thresholdCheck.getNormalizedDeviceId().toDouble(), 0.01)
}

@Test
Expand Down