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

[SDK-416] Fix missing disqualification event after global opt-out #4606

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -490,18 +490,25 @@ open class Nimbus(
internal fun setGlobalUserParticipationOnThisThread(active: Boolean) = withCatchAll {
val enrolmentChanges = nimbusClient.setGlobalUserParticipation(active)
if (enrolmentChanges.isNotEmpty()) {
recordExperimentTelemetryEvents(enrolmentChanges)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

postEnrolmentCalculation()
}
}

override fun optOut(experimentId: String) {
dbScope.launch {
withCatchAll {
nimbusClient.optOut(experimentId).also(::recordExperimentTelemetryEvents)
optOutOnThisThread(experimentId)
}
}
}

@WorkerThread
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun optOutOnThisThread(experimentId: String) {
nimbusClient.optOut(experimentId).also(::recordExperimentTelemetryEvents)
}

override fun resetTelemetryIdentifiers() {
// The "dummy" field here is required for obscure reasons when generating code on desktop,
// so we just automatically set it to a dummy value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,56 @@ class NimbusTest {
assertNotNull("Experiment enrollment-id must not be null", enrollmentEventExtrasTryTwo["enrollment_id"])
}

@Test
fun `opting out generates the correct Glean event`() {
// Load the experiment in nimbus so and optIn so that it will be active. This is necessary
// because recordExposure checks for active experiments before recording.
nimbus.setUpTestExperiments(packageName, appInfo)

// Assert that there are no events to start with
assertFalse(
"There must not be any pre-existing events",
NimbusEvents.disqualification.testHasValue()
)

// Opt out of the specific experiment
nimbus.optOutOnThisThread("test-experiment")

// Use the Glean test API to check that the valid event is present
assertTrue("Event must have a value", NimbusEvents.disqualification.testHasValue())
val disqualificationEvents = NimbusEvents.disqualification.testGetValue()
assertEquals("Event count must match", disqualificationEvents.count(), 1)
val enrollmentEventExtras = disqualificationEvents.first().extra!!
assertEquals("Experiment slug must match", "test-experiment", enrollmentEventExtras["experiment"])
assertEquals("Experiment branch must match", "test-branch", enrollmentEventExtras["branch"])
assertNotNull("Experiment enrollment-id must not be null", enrollmentEventExtras["enrollment_id"])
}

@Test
fun `toggling the global opt out generates the correct Glean event`() {
// Load the experiment in nimbus so and optIn so that it will be active. This is necessary
// because recordExposure checks for active experiments before recording.
nimbus.setUpTestExperiments(packageName, appInfo)

// Assert that there are no events to start with
assertFalse(
"There must not be any pre-existing events",
NimbusEvents.disqualification.testHasValue()
)

// Opt out of all experiments
nimbus.setGlobalUserParticipationOnThisThread(false)

// Use the Glean test API to check that the valid event is present
assertTrue("Event must have a value", NimbusEvents.disqualification.testHasValue())
val disqualificationEvents = NimbusEvents.disqualification.testGetValue()
assertEquals("Event count must match", disqualificationEvents.count(), 1)
val enrollmentEventExtras = disqualificationEvents.first().extra!!
assertEquals("Experiment slug must match", "test-experiment", enrollmentEventExtras["experiment"])
assertEquals("Experiment branch must match", "test-branch", enrollmentEventExtras["branch"])
assertNotNull("Experiment enrollment-id must not be null", enrollmentEventExtras["enrollment_id"])
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

private fun Nimbus.setUpTestExperiments(appId: String, appInfo: NimbusAppInfo) {
this.setExperimentsLocallyOnThisThread("""
{"data": [{
Expand Down