Skip to content

Commit

Permalink
Fix flaky test (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed May 21, 2024
1 parent cefefd8 commit ea5db97
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import kotlin.jvm.JvmSynthetic
* @see [io.matthewnelson.kmp.tor.runtime.RuntimeEvent.ERROR]
* */
public class UncaughtException private constructor(
override val message: String,
override val cause: Throwable,
public override val message: String,
public override val cause: Throwable,
): Exception(message, cause) {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ class ProcessorUnitTest {
var threw: Throwable? = null
var invocationSuccess = 0
try {
val ctrl = try {
factory.connectAsync(address)
} catch (_: Throwable) {
withContext(Dispatchers.Default) { delay(350.milliseconds) }
factory.connectAsync(address)
}
val ctrl = factory.connectAsync(address)

val onFailure = OnFailure { threw = it }
val onSuccess = OnSuccess<Reply.Success.OK> { synchronized(lock) { invocationSuccess++ } }
Expand All @@ -81,6 +76,9 @@ class ProcessorUnitTest {
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)
ctrl.enqueue(TorCmd.Signal.Heartbeat, onFailure, onSuccess)

// Suspends test until non-suspending complete
ctrl.executeAsync(TorCmd.Signal.Dump)
Expand All @@ -100,22 +98,19 @@ class ProcessorUnitTest {
threw?.let { throw it }

// All commands for our test executed successfully
assertEquals(10, invocationSuccess)
assertEquals(6, invocationIntercept)
assertEquals(13, invocationSuccess)
assertEquals(9, invocationIntercept)

// Ensure that given our flurry of commands, a single processor
// coroutine was started to handle them all.
synchronized(lock) {
val processorStarts = debugLogs.mapNotNull {
// println(it)
if (it.contains("Processor Started")) it else null
}
val processorStarts = debugLogs.count { it.contains("Processor Started") }

// Simply need to know if the processor handled multiple
// commands when they were available, and other startProcessor
// calls were ignored (b/c was already looping). Cannot utilize
// a hard number because test will be flaky.
assertTrue(processorStarts.size < invocationSuccess)
assertTrue(processorStarts < invocationSuccess)
}
}
}

0 comments on commit ea5db97

Please sign in to comment.