IJPL-158075 fix SingleAlarm.isDisposed

GitOrigin-RevId: 76193033481084202d31b8bb79290c766d488071
This commit is contained in:
Vladimir Krivosheev
2024-07-17 14:28:16 +02:00
committed by intellij-monorepo-bot
parent 8d06affcc7
commit d2814ae288
2 changed files with 21 additions and 2 deletions

View File

@@ -46,6 +46,7 @@ class SingleAlarm @Internal constructor(
) : Disposable {
// it is a supervisor coroutine scope
private val taskCoroutineScope: CoroutineScope
private val taskContext: CoroutineContext
private val LOCK = Any()
@@ -241,11 +242,16 @@ class SingleAlarm @Internal constructor(
}
override fun dispose() {
isDisposed = true
cancel()
}
val isDisposed: Boolean
get() = !taskCoroutineScope.isActive
// SingleAlarm can be created without `parentDisposable`.
// So, we cannot create child scope. So, we cannot use `!taskCoroutineScope.isActive` to implement `isDisposed`.
@Volatile
var isDisposed: Boolean = false
get() = field || !taskCoroutineScope.isActive
private set
val isEmpty: Boolean
get() = synchronized(LOCK) { currentJob == null }

View File

@@ -9,6 +9,8 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.impl.LaterInvocator
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.use
import com.intellij.testFramework.LoggedErrorProcessor
import com.intellij.testFramework.assertions.Assertions.assertThat
import com.intellij.testFramework.junit5.TestApplication
@@ -29,6 +31,17 @@ import java.util.concurrent.atomic.AtomicInteger
@TestApplication
class AlarmTest {
@Test
fun `isDisposed is true if parentDisposable used`() {
val disposable = Disposer.newDisposable()
disposable.use {
@Suppress("DEPRECATION")
val alarm = SingleAlarm.pooledThreadSingleAlarm(delay = 100, parentDisposable = disposable) { }
Disposer.dispose(disposable)
assertThat(alarm.isDisposed).isTrue()
}
}
@Test
fun `cancel request by task`(@TestDisposable disposable: Disposable): Unit = runBlocking(Dispatchers.EDT) {
val alarm = Alarm(threadToUse = Alarm.ThreadToUse.SWING_THREAD, parentDisposable = disposable)