Skip to content

Maintain: fix flaky tests in operationRepoTests#2318

Merged
jinliu9508 merged 1 commit intoidentity_verification_betafrom
fix-failed-test-operationrepotests
Jun 17, 2025
Merged

Maintain: fix flaky tests in operationRepoTests#2318
jinliu9508 merged 1 commit intoidentity_verification_betafrom
fix-failed-test-operationrepotests

Conversation

@jinliu9508
Copy link
Copy Markdown
Contributor

@jinliu9508 jinliu9508 commented Jun 17, 2025

Description

One Line Summary

Fixed some flaky test units in operationRepoTests due to previous commits

Details

Motivation

Fix the flaky test units that have been blocking the runner to complete successfully

Other

This PR also added a waitForIdle() method for testing in OSPrimaryCoroutineScope to allow thread to wait for the completion of operation enqueue.

Testing

Unit testing

The test units are fixed and passed.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

@jinliu9508 jinliu9508 changed the base branch from main to identity_verification_beta June 17, 2025 19:10
@jinliu9508 jinliu9508 force-pushed the fix-failed-test-operationrepotests branch 2 times, most recently from e3c5504 to 56e9082 Compare June 17, 2025 19:16
@jinliu9508 jinliu9508 requested a review from jkasten2 June 17, 2025 19:27
@jinliu9508 jinliu9508 changed the title Fix failed tests in operationRepoTests Fix flaky tests in operationRepoTests Jun 17, 2025
@jinliu9508 jinliu9508 changed the title Fix flaky tests in operationRepoTests Maintain: fix flaky tests in operationRepoTests Jun 17, 2025
Comment on lines +7 to +49
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

object OSPrimaryCoroutineScope {
// CoroutineScope tied to the main thread
private val mainScope = CoroutineScope(newSingleThreadContext(name = "OSPrimaryCoroutineScope"))
private val activeJobs = mutableSetOf<Job>()

/**
* Executes the given [block] on the OS primary coroutine scope.
*/
fun execute(block: suspend () -> Unit) {
mainScope.launch {
block()
val job =
mainScope.launch {
block()
}
activeJobs.add(job)
job.invokeOnCompletion {
activeJobs.remove(job)
}
}

/**
* Suspends until there are no active tasks running in the mainScope.
* This is useful for testing or ensuring all background work is complete.
*/
suspend fun waitForIdle() =
suspendCancellableCoroutine { continuation ->
if (activeJobs.isEmpty()) {
continuation.resume(Unit)
return@suspendCancellableCoroutine
}

val completionJob =
mainScope.launch {
activeJobs.forEach { it.join() }
continuation.resume(Unit)
}

continuation.invokeOnCancellation {
completionJob.cancel()
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we need to track a list of active jobs. Instead in waitForIdle(), why not just add a job and wait for it to be completed?

I think something like this would work:

suspend fun waitForIdle() = mainScope.launch { }.join()

The blank job will only finish once all jobs before it are done, so we don't need to also track all the jobs with our own set/list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Was trying to resume the blocking until completion, but didn't realize that mainScope is all we need to look after. I made the change in the fixup commit.

@jinliu9508 jinliu9508 force-pushed the fix-failed-test-operationrepotests branch from b65847a to 4580941 Compare June 17, 2025 22:15
@jinliu9508
Copy link
Copy Markdown
Contributor Author

Squashed the fixup commit and merging...

@jinliu9508 jinliu9508 merged commit 7e163e8 into identity_verification_beta Jun 17, 2025
2 checks passed
@jinliu9508 jinliu9508 deleted the fix-failed-test-operationrepotests branch June 17, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants