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