Skip to content

Commit

Permalink
Use thread join before verifying the invalidation in BufferedObservin…
Browse files Browse the repository at this point in the history
…gPreconditionTest (Draegerwerk#205)

Use thread join before verifying the invalidation in
BufferedObservingPreconditionTest.
This ensures we wait for the call instead of randomly failing.

Closes Draegerwerk#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
  • Loading branch information
ldeichmann authored Aug 28, 2024
1 parent 7c9bf04 commit 0e8a98d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MdibChange>()
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -116,7 +116,7 @@ internal class BufferedObservingPreconditionTest {
internal fun `test processing thread dying invalidates test`() {
val testRunObserver = mock<TestRunObserver>()
val mockInjector = mock<Injector>()
doReturn(testRunObserver).`when`(mockInjector).getInstance(TestRunObserver::class.java)
whenever(mockInjector.getInstance(TestRunObserver::class.java)).thenReturn(testRunObserver)

val exampleObserving = object : BufferedObservingPrecondition(
injector = mockInjector,
Expand All @@ -130,9 +130,21 @@ internal class BufferedObservingPreconditionTest {

exampleObserving.observeChange(mock<MdibChange.Metric>())

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<MdibChange.Metric>())
}

companion object {
private const val TIME_TO_WAIT_FOR_CHANGE_MILLIS = 60_000L
}
}

0 comments on commit 0e8a98d

Please sign in to comment.