<lambda>null1package kotlinx.coroutines.javafx 2 3 import kotlinx.coroutines.testing.* 4 import javafx.beans.property.SimpleIntegerProperty 5 import kotlinx.coroutines.testing.TestBase 6 import kotlinx.coroutines.* 7 import kotlinx.coroutines.flow.* 8 import org.junit.Before 9 import org.junit.Test 10 import kotlin.test.* 11 12 13 class JavaFxObservableAsFlowTest : TestBase() { 14 15 @Before 16 fun setup() { 17 ignoreLostThreads("JavaFX Application Thread", "Thread-", "QuantumRenderer-", "InvokeLaterDispatcher") 18 } 19 20 @Test 21 fun testFlowOrder() = runTest { 22 if (!initPlatform()) { 23 println("Skipping JavaFxTest in headless environment") 24 return@runTest // ignore test in headless environments 25 } 26 27 val integerProperty = SimpleIntegerProperty(0) 28 val n = 1000 29 val flow = integerProperty.asFlow().takeWhile { j -> j != n } 30 newSingleThreadContext("setter").use { pool -> 31 launch(pool) { 32 for (i in 1..n) { 33 launch(Dispatchers.JavaFx) { 34 integerProperty.set(i) 35 } 36 } 37 } 38 var i = -1 39 flow.collect { j -> 40 assertTrue(i < (j as Int), "Elements are neither repeated nor shuffled") 41 i = j 42 } 43 } 44 } 45 46 @Test 47 fun testConflation() = runTest { 48 if (!initPlatform()) { 49 println("Skipping JavaFxTest in headless environment") 50 return@runTest // ignore test in headless environments 51 } 52 53 withContext(Dispatchers.JavaFx) { 54 val END_MARKER = -1 55 val integerProperty = SimpleIntegerProperty(0) 56 val flow = integerProperty.asFlow().takeWhile { j -> j != END_MARKER } 57 launch { 58 yield() // to subscribe to [integerProperty] 59 yield() // send 0 60 integerProperty.set(1) 61 expect(3) 62 yield() // send 1 63 expect(5) 64 integerProperty.set(2) 65 for (i in (-100..-2)) { 66 integerProperty.set(i) // should be skipped due to conflation 67 } 68 integerProperty.set(3) 69 expect(6) 70 yield() // send 2 and 3 71 integerProperty.set(-1) 72 } 73 expect(1) 74 flow.collect { i -> 75 when (i) { 76 0 -> expect(2) 77 1 -> expect(4) 78 2 -> expect(7) 79 3 -> expect(8) 80 else -> fail("i is $i") 81 } 82 } 83 finish(9) 84 } 85 } 86 87 @Test 88 fun testIntermediateCrash() = runTest { 89 if (!initPlatform()) { 90 println("Skipping JavaFxTest in headless environment") 91 return@runTest // ignore test in headless environments 92 } 93 94 val property = SimpleIntegerProperty(0) 95 96 assertFailsWith<TestException> { 97 property.asFlow().onEach { 98 yield() 99 throw TestException() 100 }.collect() 101 } 102 } 103 } 104