Skip to content

Commit

Permalink
refactor: alter how disabled threshold works
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Nov 18, 2024
1 parent a3d471a commit 87aec53
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 35 deletions.
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

0 comments on commit 87aec53

Please sign in to comment.