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

Bug 1779638 - Kotlin: Add a hook to run tasks after init #2116

Merged
merged 1 commit into from
Jul 18, 2022
Merged
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 @@ -44,6 +44,12 @@ internal class OnGleanEventsImpl(val glean: GleanInternalAPI) : OnGleanEvents {
ProcessLifecycleOwner.get().lifecycle.addObserver(glean.gleanLifecycleObserver)
}
glean.initialized = true

if (glean.testingMode) {
glean.afterInitQueue.forEach { block ->
block()
}
}
}

override fun triggerUpload() {
Expand Down Expand Up @@ -102,6 +108,8 @@ open class GleanInternalAPI internal constructor() {
// such as the last time it was sent and the store name
internal var metricsPingScheduler: MetricsPingScheduler? = null

internal val afterInitQueue: MutableList<() -> Unit> = mutableListOf()

// This is used to cache the process state and is used by the function `isMainProcess()`
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal var isMainProcess: Boolean? = null
Expand Down Expand Up @@ -450,6 +458,23 @@ open class GleanInternalAPI internal constructor() {
Glean.initialize(context, uploadEnabled, config, buildInfo)
}

/**
* Run a task right after initialization.
*
* If initialization already happened the task runs immediately.
* Otherwise it is queued and run after initialization finishes.
*/
internal fun afterInitialize(block: () -> Unit) {
// Queueing tasks after initialize is only allowed in test mode.
assert(isInitialized())

if (isInitialized()) {
block()
} else {
this.afterInitQueue.add(block)
}
}

/**
* TEST ONLY FUNCTION.
* Sets the server endpoint to a local address for ingesting test pings.
Expand All @@ -465,12 +490,10 @@ open class GleanInternalAPI internal constructor() {

isSendingToTestEndpoint = true

// We can't set the configuration unless we're initialized.
assert(isInitialized())

val endpointUrl = "http://localhost:$port"

Glean.configuration = configuration.copy(serverEndpoint = endpointUrl)
Glean.afterInitialize {
val endpointUrl = "http://localhost:$port"
Glean.configuration = configuration.copy(serverEndpoint = endpointUrl)
}
}

/**
Expand Down