Skip to content

Commit

Permalink
[FX-1376] Fetch rosetta-traffic-percentage to determine vault provider (
Browse files Browse the repository at this point in the history
#258)

* add support for reading rosetta-traffic-percentage flag to LDManager

* update LDManager.getVaultProvider tests

* re-organize and combine lines

* remove `vault-primary-traffic-percentage` flag references entirely

* update LDManager tests

* add TODO comment to clean up vgsVaultWrapper

* clean up unused imports
  • Loading branch information
evanfreeze authored May 30, 2024
1 parent 45abc1f commit 963cbf1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.launchdarkly.sdk.android.LDClient
import com.launchdarkly.sdk.android.LDConfig

internal object LDFlags {
const val VAULT_PRIMARY_TRAFFIC_PERCENTAGE_FLAG = "vault-primary-traffic-percentage"
const val ISO_POLLING_WAIT_INTERVALS = "iso-polling-wait-intervals"
const val ROSETTA_TRAFFIC_PERCENTAGE = "rosetta-traffic-percentage"
}

internal object LDContexts {
Expand All @@ -22,12 +22,13 @@ internal object LDContextKind {
const val SERVICE = "service"
}

internal val ALWAYS_BT_PERCENT = 100.0
internal val ALWAYS_VGS_PERCENT = 0.0
// rosetta-traffic-percentage
internal val ALWAYS_ROSETTA_PERCENT = 100.0
internal val ALWAYS_THIRD_PARTY_PERCENT = 0.0

internal fun computeVaultType(trafficPrimaryPercentFlag: Double): VaultType {
internal fun computeVaultType(rosettaPercentage: Double): VaultType {
val randomNum = Math.random() * 100
return if (randomNum < trafficPrimaryPercentFlag) VaultType.BT_VAULT_TYPE else VaultType.VGS_VAULT_TYPE
return if (randomNum <= rosettaPercentage) VaultType.FORAGE_VAULT_TYPE else VaultType.BT_VAULT_TYPE
}

internal object LDManager {
Expand All @@ -45,14 +46,14 @@ internal object LDManager {
}

internal fun getVaultProvider(logger: Log = Log.getSilentInstance()): VaultType {
val vaultPercent = client?.doubleVariation(
LDFlags.VAULT_PRIMARY_TRAFFIC_PERCENTAGE_FLAG,
ALWAYS_VGS_PERCENT
) ?: ALWAYS_VGS_PERCENT
logger.i("[LaunchDarkly] Vault percent of $vaultPercent return from LD")

// convert the flag percent into an answer to which vault provider to use
val vaultType = computeVaultType(vaultPercent)
val rosettaPercent = client?.doubleVariation(
LDFlags.ROSETTA_TRAFFIC_PERCENTAGE,
ALWAYS_ROSETTA_PERCENT
) ?: ALWAYS_ROSETTA_PERCENT
logger.i("[LaunchDarkly] Rosetta percent of $rosettaPercent returned from LD")

// convert the rosetta flag percent into an answer to which vault provider to use
val vaultType = computeVaultType(rosettaPercent)
logger.i("[LaunchDarkly] Vault type set to $vaultType")

// return vault provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class ForagePINEditText @JvmOverloads constructor(
return if (vaultType == VaultType.BT_VAULT_TYPE) {
btVaultWrapper
} else {
// TODO: Update this to the ForageVaultWrapper once it's back in this codebase
// https://linear.app/joinforage/issue/FX-1368/re-introduce-the-foragevaultwrapper-into-the-coreui-module-in-the
vgsVaultWrapper
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.joinforage.forage.android.network.data
import android.app.Application
import androidx.test.platform.app.InstrumentationRegistry
import com.joinforage.forage.android.core.services.VaultType
import com.joinforage.forage.android.core.services.launchdarkly.ALWAYS_BT_PERCENT
import com.joinforage.forage.android.core.services.launchdarkly.ALWAYS_VGS_PERCENT
import com.joinforage.forage.android.core.services.launchdarkly.ALWAYS_ROSETTA_PERCENT
import com.joinforage.forage.android.core.services.launchdarkly.ALWAYS_THIRD_PARTY_PERCENT
import com.joinforage.forage.android.core.services.launchdarkly.LDFlags
import com.joinforage.forage.android.core.services.launchdarkly.LDManager
import com.joinforage.forage.android.core.services.launchdarkly.computeVaultType
Expand Down Expand Up @@ -35,38 +35,50 @@ class LaunchDarklyTest() {
}

@Test
fun `Test computeVaultType returns BT percent is 100f`() {
assertThat(computeVaultType(ALWAYS_BT_PERCENT)).isEqualTo(VaultType.BT_VAULT_TYPE)
fun `Test computeVaultType returns Forage Vault if percent is 100f`() {
assertThat(computeVaultType(ALWAYS_ROSETTA_PERCENT)).isEqualTo(VaultType.FORAGE_VAULT_TYPE)
}

@Test
fun `Test computeVaultType returns VGS percent is 0f`() {
assertThat(computeVaultType(ALWAYS_VGS_PERCENT)).isEqualTo(VaultType.VGS_VAULT_TYPE)
fun `Test computeVaultType returns BT percent is 0f`() {
assertThat(computeVaultType(ALWAYS_THIRD_PARTY_PERCENT)).isEqualTo(VaultType.BT_VAULT_TYPE)
}

@Test
fun `It should default to using VGS and honor flag updates`() = runTest {
// set up LDManager, importantly, we're not giving it any value for
// primaryTrafficPercent since we want to test it defaults to VGS
fun `It should default to using Rosetta and honor flag updates`() = runTest {
// set up LDManager, importantly, we're not giving it any value for rosettaTrafficPercentage or
// primaryTrafficPercent since we want to test it defaults to Rosetta
val app = InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as Application
LDManager.initialize(app, ldConfig)

// it should default to using VGS as the vault provider
// it should default to using Rosetta as the vault provider when no flags are set
val original = LDManager.getVaultProvider()
assertThat(original).isEqualTo(VaultType.VGS_VAULT_TYPE)
assertThat(original).isEqualTo(VaultType.FORAGE_VAULT_TYPE)

// Set the test data to send all traffic to BT
// Set the test data to send all traffic to rosetta
td.update(
td.flag(LDFlags.VAULT_PRIMARY_TRAFFIC_PERCENTAGE_FLAG).variations(
td.flag(LDFlags.ROSETTA_TRAFFIC_PERCENTAGE).variations(
LDValue.of(
ALWAYS_BT_PERCENT
ALWAYS_ROSETTA_PERCENT
)
)
)

// it should consume the flag and choose BT
val postUpdate = LDManager.getVaultProvider()
assertThat(postUpdate).isEqualTo(VaultType.BT_VAULT_TYPE)
val postRosettaUpdate = LDManager.getVaultProvider()
assertThat(postRosettaUpdate).isEqualTo(VaultType.FORAGE_VAULT_TYPE)

// Set the test data to send all traffic to third-party
td.update(
td.flag(LDFlags.ROSETTA_TRAFFIC_PERCENTAGE).variations(
LDValue.of(
ALWAYS_THIRD_PARTY_PERCENT
)
)
)

// it should use BT
val post3PUpdate = LDManager.getVaultProvider()
assertThat(post3PUpdate).isEqualTo(VaultType.BT_VAULT_TYPE)
}

@Test
Expand Down

0 comments on commit 963cbf1

Please sign in to comment.