-
Notifications
You must be signed in to change notification settings - Fork 4
[E2E] Prove ready preload cache hits on both native samples #627
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
Open
kyle-schellen
wants to merge
1
commit into
main
Choose a base branch
from
ks-e2e-preload-cache-hit-signal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
...idDemo/app/src/main/java/com/shopify/checkoutkit/androiddemo/e2e/PreloadCacheHitMarker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package com.shopify.checkoutkit.androiddemo.e2e | ||
|
|
||
| import android.os.Process | ||
| import com.shopify.checkoutkit.androiddemo.accessibility.AccessibilityIdentifiers | ||
| import java.io.BufferedReader | ||
| import java.io.InputStreamReader | ||
| import java.util.UUID | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import timber.log.Timber | ||
|
|
||
| /** Maps whether the SDK reported a ready preload cache hit to an identifier. */ | ||
| object PreloadCacheHitMarker { | ||
| fun testId(observed: Boolean): String = | ||
| "${AccessibilityIdentifiers.PRELOAD_CACHE_HIT_PREFIX}${text(observed)}" | ||
|
|
||
| fun text(observed: Boolean): String = if (observed) "observed" else "none" | ||
| } | ||
|
|
||
| /** A Logcat stream plus the resources that must be closed to stop reading it. */ | ||
| class LogStream( | ||
| val lines: Sequence<String>, | ||
| private val closeAction: () -> Unit = {}, | ||
| ) { | ||
| private val closed = AtomicBoolean(false) | ||
|
|
||
| fun close() { | ||
| if (closed.compareAndSet(false, true)) closeAction() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Watches this app's PID-scoped Logcat for a ready SDK cache hit. | ||
| * | ||
| * The SDK log sink is internal, so the sample reads its own logs instead of installing a logger. | ||
| * A unique boundary keeps buffered lines from an earlier app process outside this observation. | ||
| */ | ||
| class PreloadCacheHitLog( | ||
| private val observationBoundary: String = "$OBSERVATION_BOUNDARY_PREFIX${UUID.randomUUID()}", | ||
| private val openLines: () -> LogStream = ::followOwnLogcat, | ||
| private val writeBoundary: (String) -> Unit = { Timber.tag(OBSERVATION_TAG).d(it) }, | ||
| private val isPreloadReady: () -> Boolean = { false }, | ||
| private val reportError: (Throwable) -> Unit = { | ||
| Timber.e(it, "Failed to observe the preload cache-hit diagnostic") | ||
| }, | ||
| ) { | ||
| companion object { | ||
| /** Must stay in step with PreloadCache.kt, which logs this on a cache hit. */ | ||
| const val DIAGNOSTIC = "Returning cached preloaded WebView." | ||
|
|
||
| private const val OBSERVATION_TAG = "PreloadObservability" | ||
| private const val OBSERVATION_BOUNDARY_PREFIX = "Observation started: " | ||
|
|
||
| /** Follows this process's SDK diagnostics plus the boundary that arms this observation. */ | ||
| private fun followOwnLogcat(): LogStream { | ||
| val process = ProcessBuilder( | ||
| "logcat", | ||
| "-T", | ||
| "1", | ||
| "--pid=${Process.myPid()}", | ||
| "$OBSERVATION_TAG:D", | ||
| "PreloadCache:D", | ||
| "*:S", | ||
| ).redirectErrorStream(true).start() | ||
| val reader = BufferedReader(InputStreamReader(process.inputStream)) | ||
|
|
||
| return LogStream(reader.lineSequence()) { | ||
| process.destroy() | ||
| runCatching { reader.close() } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private val _observed = MutableStateFlow(false) | ||
| val observed: StateFlow<Boolean> = _observed.asStateFlow() | ||
|
|
||
| private val resourceLock = Any() | ||
| private var stream: LogStream? = null | ||
| private var job: Job? = null | ||
| private var closed = false | ||
| private var observationStarted = false | ||
|
|
||
| fun start(scope: CoroutineScope, dispatcher: CoroutineDispatcher = Dispatchers.IO): Job { | ||
| check(job == null) { "Preload cache-hit observation already started" } | ||
|
|
||
| return scope.launch(dispatcher) { | ||
| try { | ||
| val opened = openLines() | ||
| val shouldRead = synchronized(resourceLock) { | ||
| if (closed) { | ||
| false | ||
| } else { | ||
| stream = opened | ||
| true | ||
| } | ||
| } | ||
|
|
||
| if (!shouldRead) { | ||
| opened.close() | ||
| return@launch | ||
| } | ||
|
|
||
| writeBoundary(observationBoundary) | ||
| opened.lines.forEach(::record) | ||
| } catch (error: Exception) { | ||
| val shouldReport = synchronized(resourceLock) { !closed } && error !is CancellationException | ||
| if (shouldReport) reportError(error) | ||
| } finally { | ||
| closeStream() | ||
| } | ||
| }.also { job = it } | ||
| } | ||
|
|
||
| fun close() { | ||
| val opened = synchronized(resourceLock) { | ||
| if (closed) return | ||
| closed = true | ||
| stream.also { stream = null } | ||
| } | ||
|
|
||
| opened?.close() | ||
| job?.cancel() | ||
| } | ||
|
|
||
| fun record(line: String) { | ||
| if (!observationStarted) { | ||
| if (line.contains(observationBoundary)) observationStarted = true | ||
| return | ||
| } | ||
|
|
||
| if (line.contains(DIAGNOSTIC) && isPreloadReady()) { | ||
| _observed.value = true | ||
| } | ||
| } | ||
|
|
||
| private fun closeStream() { | ||
| val opened = synchronized(resourceLock) { | ||
| stream.also { stream = null } | ||
| } | ||
| opened?.close() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you been able to do much testing against this to ensure it doesn't false positive?
I'm wondering if at app launch we can add a marker UUID that designates the start of logs to read from to avoid possible conflicts reading a stale cache hit marker