From 0e8a98d38c20939c41c2853dd6b55094aceb1594 Mon Sep 17 00:00:00 2001 From: Lukas Deichmann <8513777+ldeichmann@users.noreply.github.com> Date: Wed, 28 Aug 2024 12:56:17 +0200 Subject: [PATCH] Use thread join before verifying the invalidation in BufferedObservingPreconditionTest (#205) Use thread join before verifying the invalidation in BufferedObservingPreconditionTest. This ensures we wait for the call instead of randomly failing. Closes #202. # Checklist The following aspects have been respected by the author of this pull request, confirmed by both pull request assignee **and** reviewer: * Adherence to coding conventions * [X] Pull Request Assignee * [x] Reviewer * Adherence to javadoc conventions * [X] Pull Request Assignee * [x] Reviewer * Changelog update (necessity checked and entry added or not added respectively) * [X] Pull Request Assignee * [x] Reviewer * README update (necessity checked and entry added or not added respectively) * [X] Pull Request Assignee * [x] Reviewer * config update (necessity checked and entry added or not added respectively) * [X] Pull Request Assignee * [x] Reviewer * SDCcc executable ran against a test device (if necessary) * [X] Pull Request Assignee * [x] Reviewer --- .../BufferedObservingPrecondition.kt | 8 ++++++- .../BufferedObservingPreconditionTest.kt | 22 ++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPrecondition.kt b/sdccc/src/main/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPrecondition.kt index 707a6c4a..32e9cee3 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPrecondition.kt +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPrecondition.kt @@ -27,10 +27,16 @@ abstract class BufferedObservingPrecondition( // this is going to be a problem when the manipulation is very long-running, // as the queue will fill up (quite quickly with waveforms) private val updateQueue = LinkedBlockingQueue() - private val processingThread: Thread + private val processDied = AtomicBoolean(false) private val testRunObserver = injector.getInstance(TestRunObserver::class.java) + /** + * The thread that processes the incoming changes and provides them to + * the concrete observing precondition. + */ + val processingThread: Thread + init { processingThread = thread(start = true, isDaemon = true) { while (true) { diff --git a/sdccc/src/test/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPreconditionTest.kt b/sdccc/src/test/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPreconditionTest.kt index effdd040..305539bc 100644 --- a/sdccc/src/test/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPreconditionTest.kt +++ b/sdccc/src/test/java/com/draeger/medical/sdccc/manipulation/precondition/BufferedObservingPreconditionTest.kt @@ -14,9 +14,9 @@ import org.junit.jupiter.api.Test import org.mockito.Mockito.any import org.mockito.Mockito.anyString import org.mockito.Mockito.mock +import org.mockito.Mockito.times import org.mockito.Mockito.verify -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.times +import org.mockito.kotlin.whenever import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit import kotlin.test.assertEquals @@ -116,7 +116,7 @@ internal class BufferedObservingPreconditionTest { internal fun `test processing thread dying invalidates test`() { val testRunObserver = mock() val mockInjector = mock() - doReturn(testRunObserver).`when`(mockInjector).getInstance(TestRunObserver::class.java) + whenever(mockInjector.getInstance(TestRunObserver::class.java)).thenReturn(testRunObserver) val exampleObserving = object : BufferedObservingPrecondition( injector = mockInjector, @@ -130,9 +130,21 @@ internal class BufferedObservingPreconditionTest { exampleObserving.observeChange(mock()) - verify(testRunObserver, times(1)).invalidateTestRun(anyString(), any()) + exampleObserving + .processingThread + .join(TIME_TO_WAIT_FOR_CHANGE_MILLIS) - // passing more changes is still possible + verify( + testRunObserver, + times(1) + ).invalidateTestRun(anyString(), any()) + + // passing more changes is still possible and does not block, + // but will trigger no processing exampleObserving.observeChange(mock()) } + + companion object { + private const val TIME_TO_WAIT_FOR_CHANGE_MILLIS = 60_000L + } }