Skip to content

Commit

Permalink
[ide starter] IJ-CR-118555 Correct waiting till Maven is able to dest…
Browse files Browse the repository at this point in the history
…roy daemon

GitOrigin-RevId: 5f2b98239620446d1c000fbd6307add34ec424d2
  • Loading branch information
Nikita-Kudrin authored and intellij-monorepo-bot committed Nov 4, 2023
1 parent e9acc7b commit 5da252c
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class EventsFilteringTest {

@RepeatedTest(value = 200)
fun `filtering events by type is working`() {
StarterBus.subscribe<Signal> {
StarterBus.subscribe<Signal, EventsFilteringTest>(this) {
isEventProcessed.set(true)
}

Expand All @@ -55,7 +55,7 @@ class EventsFilteringTest {

@RepeatedTest(value = 100)
fun `single event is published`() {
StarterBus.subscribe<Signal> {
StarterBus.subscribe<Signal, EventsFilteringTest>(this) {
isEventProcessed.set(true)
}

Expand All @@ -69,8 +69,8 @@ class EventsFilteringTest {
val secondSubscriberInvocationsData = mutableSetOf<Any>()

StarterBus
.subscribe<Signal> { firstSubscriberInvocationsData.add(it) }
.subscribe<Signal> { secondSubscriberInvocationsData.add(it) }
.subscribe<Signal, EventsFilteringTest>(this) { firstSubscriberInvocationsData.add(it) }
.subscribe<Signal, EventsFilteringTest>(this) { secondSubscriberInvocationsData.add(it) }

val firstSignal = Signal(EventState.BEFORE)
StarterBus.postAsync(firstSignal)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.intellij.ide.starter.junit5

import com.intellij.ide.starter.bus.Signal
import com.intellij.ide.starter.bus.StarterBus
import io.kotest.assertions.withClue
import io.kotest.matchers.booleans.shouldBeTrue
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.Duration.Companion.seconds

class FiringNestedEventsTest {
@AfterEach
fun afterEach() {
StarterBus.LISTENER.unsubscribe()
}

class FirstEvent : Signal() {
init {
StarterBus.postAndWaitProcessing(SecondEvent(), timeout = 2.seconds).shouldBeTrue()
}
}

class SecondEvent : Signal()

@Test
fun `firing nested events should work`() {
val firstSubscriberProcessedEvent = AtomicBoolean(false)
val secondSubscriberProcessedEvent = AtomicBoolean(false)

StarterBus
.subscribe(this) { _: FirstEvent ->
firstSubscriberProcessedEvent.set(true)
}
.subscribe(this) { _: SecondEvent ->
secondSubscriberProcessedEvent.set(true)
}

StarterBus.postAndWaitProcessing(FirstEvent(), timeout = 2.seconds).shouldBeTrue()

withClue("Processing nested events should not lead to deadlock") {
firstSubscriberProcessedEvent.get().shouldBeTrue()
secondSubscriberProcessedEvent.get().shouldBeTrue()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IdeLaunchEventTest {
@RepeatedTest(value = 5)
fun `events for ide launch should be fired`(testInfo: TestInfo) {
val firedEvents = mutableListOf<IdeLaunchEvent>()
StarterBus.subscribe { event: IdeLaunchEvent -> firedEvents.add(event) }
StarterBus.subscribe(this) { event: IdeLaunchEvent -> firedEvents.add(event) }

val context = Starter.newContext(testInfo.hyphenateWithClass(), TestCases.IU.withProject(NoProject).useRelease())

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.intellij.ide.starter.junit5

import com.intellij.ide.starter.bus.Signal
import com.intellij.ide.starter.bus.StarterBus
import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicInteger


class SubscribingOnlyOnceTest {
@AfterEach
fun afterEach() {
StarterBus.LISTENER.unsubscribe()
}

@Test
fun `multiple subscription should not work if subscribed only once`() {
val obj = Any()

val eventProcessedTimes = AtomicInteger()
val secondProcessedTimes = AtomicInteger()

StarterBus
.subscribeOnlyOnce(this) { _: Signal ->
eventProcessedTimes.incrementAndGet()
}
.subscribeOnlyOnce(this) { _: Signal ->
eventProcessedTimes.incrementAndGet()
}
.subscribeOnlyOnce(obj) { _: Signal ->
secondProcessedTimes.incrementAndGet()
}
.subscribeOnlyOnce(obj) { _: Signal ->
secondProcessedTimes.incrementAndGet()
}

StarterBus.postAsync(Signal())

withClue("Multiple subscription should not work if subscribed only once") {
eventProcessedTimes.get().shouldBe(1)
secondProcessedTimes.get().shouldBe(1)
}

eventProcessedTimes.set(0)
secondProcessedTimes.set(0)

StarterBus.postAndWaitProcessing(Signal())

withClue("Multiple subscription should not work if subscribed only once") {
eventProcessedTimes.get().shouldBe(1)
secondProcessedTimes.get().shouldBe(1)
}
}

@Test
fun `multiple subscription should work by default`() {
val obj = Any()

val eventProcessedTimes = AtomicInteger()
val secondProcessedTimes = AtomicInteger()

StarterBus
.subscribe(this) { event: Signal ->
eventProcessedTimes.incrementAndGet()
}
.subscribe(this) { event: Signal ->
eventProcessedTimes.incrementAndGet()
}
.subscribe(this) { event: Signal ->
eventProcessedTimes.incrementAndGet()
}
.subscribe(obj) { _: Signal ->
secondProcessedTimes.incrementAndGet()
}
.subscribe(obj) { _: Signal ->
secondProcessedTimes.incrementAndGet()
}

StarterBus.postAsync(Signal())

withClue("Multiple subscription should work by default") {
eventProcessedTimes.get().shouldBe(3)
secondProcessedTimes.get().shouldBe(2)
}

eventProcessedTimes.set(0)
secondProcessedTimes.set(0)

StarterBus.postAndWaitProcessing(Signal())

withClue("Multiple subscription should work by default") {
eventProcessedTimes.get().shouldBe(3)
secondProcessedTimes.get().shouldBe(2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestContextInitializationEventsTest {
@RepeatedTest(value = 200)
fun `events for test runner init should be fired`(testInfo: TestInfo) {
val firedEvents = mutableListOf<TestContextInitializedEvent>()
StarterBus.subscribe { event: TestContextInitializedEvent -> firedEvents.add(event) }
StarterBus.subscribe(this) { event: TestContextInitializedEvent -> firedEvents.add(event) }

val testName = testInfo.displayName.hyphenateTestName()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.kotest.matchers.comparables.shouldBeLessThan
import kotlinx.coroutines.delay
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
Expand All @@ -21,7 +22,8 @@ class WaitingForSubscribersTest {
StarterBus.LISTENER.unsubscribe()
}

@RepeatedTest(value = 5)
//@RepeatedTest(value = 5)
@Test
fun `waiting till subscribers finish their work`() {
val firstSubscriberProcessedEvent = AtomicBoolean(false)
val secondSubscriberProcessedEvent = AtomicBoolean(false)
Expand All @@ -30,11 +32,11 @@ class WaitingForSubscribersTest {
val secondSubscriberDelay = 4.seconds

StarterBus
.subscribe<Signal> {
.subscribe(this) { event: Signal ->
delay(firstSubscriberDelay)
firstSubscriberProcessedEvent.set(true)
}
.subscribe<Signal> {
.subscribe(this) { event: Signal ->
delay(secondSubscriberDelay)
secondSubscriberProcessedEvent.set(true)
}
Expand Down Expand Up @@ -70,11 +72,11 @@ class WaitingForSubscribersTest {
val secondSubscriberDelay = 6.seconds

StarterBus
.subscribe<Signal> {
.subscribe(this) { _: Signal ->
delay(firstSubscriberDelay)
firstSubscriberProcessedEvent.set(true)
}
.subscribe<Signal> {
.subscribe(this) { _: Signal ->
delay(secondSubscriberDelay)
secondSubscriberProcessedEvent.set(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ open class GradleBuildTool(testContext: IDETestContext) : BuildTool(BuildToolTyp
val mavenDaemonName = "gradleDaemon"
destroyProcessIfExists(mavenDaemonName)
}
}

init {
StarterBus.subscribe { event: IdeLaunchEvent ->
if (event.state == EventState.AFTER) {
destroyGradleDaemonProcessIfExists()
}
init {
StarterBus.subscribeOnlyOnce(GradleBuildTool::javaClass) { event: IdeLaunchEvent ->
if (event.data.runContext.testContext === testContext && event.state == EventState.AFTER) {
destroyGradleDaemonProcessIfExists()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.intellij.ide.starter.buildTool

import com.intellij.ide.starter.bus.EventState
import com.intellij.ide.starter.bus.StarterBus
import com.intellij.ide.starter.ide.IDETestContext
import com.intellij.ide.starter.process.destroyProcessIfExists
import com.intellij.ide.starter.runner.IdeLaunchEvent
import com.intellij.ide.starter.runner.ValidateVMOptionsWereSetEvent
import com.intellij.openapi.diagnostic.LogLevel
import com.intellij.tools.ide.util.common.logOutput
import java.nio.file.Path
Expand All @@ -28,13 +27,11 @@ open class MavenBuildTool(testContext: IDETestContext) : BuildTool(BuildToolType
val mavenDaemonName = "MavenServerIndexerMain"
destroyProcessIfExists(mavenDaemonName)
}
}

init {
StarterBus.subscribe { event: IdeLaunchEvent ->
if (event.state == EventState.AFTER) {
destroyMavenIndexerProcessIfExists()
}
}
init {
StarterBus.subscribeOnlyOnce(MavenBuildTool::javaClass) { event: ValidateVMOptionsWereSetEvent ->
if (event.data.testContext === testContext) destroyMavenIndexerProcessIfExists()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.intellij.ide.starter.bus

/**
* Event, that holds data. If you don't need to pass data with event, take a look at [com.intellij.ide.starter.bus.Signal]
*/
open class Event<T>(
state: EventState = EventState.UNDEFINED,
val data: T
Expand Down
Loading

0 comments on commit 5da252c

Please sign in to comment.