Merge dev into master #59

Merged
altavir merged 37 commits from dev into master 2019-05-31 12:35:58 +03:00
2 changed files with 33 additions and 36 deletions
Showing only changes of commit 6f4f658030 - Show all commits

View File

@ -1,12 +1,8 @@
package scientifik.kmath package scientifik.kmath
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
val Dispatchers.Math: CoroutineDispatcher get() = Dispatchers.Default val Dispatchers.Math: CoroutineDispatcher get() = Dispatchers.Default
@ -91,31 +87,16 @@ suspend fun <T> AsyncFlow<T>.collect(concurrency: Int, action: suspend (value: T
}) })
} }
//suspend fun <T> Flow<T>.collect(concurrency: Int, dispatcher: CoroutineDispatcher, collector: FlowCollector<T>){ @FlowPreview
// require(concurrency >= 1) { "Buffer size should be more than 1, but was $concurrency" } fun <T, R> Flow<T>.map(
// coroutineScope { concurrencyLevel: Int,
// //Starting up to N deferred coroutines ahead of time dispatcher: CoroutineDispatcher = Dispatchers.Default,
// val channel = produce(capacity = concurrency-1) { bufferSize: Int = concurrencyLevel,
// this@collect. transform: suspend (T) -> R
// deferredFlow.collect { value -> ): Flow<R> {
// value.start(this@coroutineScope) return flatMapMerge(concurrencyLevel, bufferSize) { value ->
// send(value) flow { emit(transform(value)) }
// } }.flowOn(dispatcher)
// } }
//
// (channel as Job).invokeOnCompletion {
// if (it is CancellationException && it.cause == null) cancel()
// }
//
// for (element in channel) {
// collector.emit(element.await())
// }
//
// val producer = channel as Job
// if (producer.isCancelled) {
// producer.join()
// //throw producer.getCancellationException()
// }
// }
//}

View File

@ -1,20 +1,36 @@
package scientifik.kmath.streaming package scientifik.kmath.streaming
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.*
import org.junit.Test import org.junit.Test
import scientifik.kmath.async import scientifik.kmath.async
import scientifik.kmath.collect import scientifik.kmath.collect
import scientifik.kmath.map
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
@InternalCoroutinesApi @InternalCoroutinesApi
@FlowPreview @FlowPreview
class BufferFlowTest { class BufferFlowTest {
@Test(timeout = 2000)
fun concurrentMap() {
runBlocking {
(1..20).asFlow().map(4) {
println("Started $it")
@Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(200)
it
}.collect {
println("Completed $it")
}
}
}
@Test(timeout = 2000) @Test(timeout = 2000)
fun mapParallel() { fun mapParallel() {
runBlocking { runBlocking {
(1..20).asFlow().async(Dispatchers.Default) { (1..20).asFlow().async {
println("Started $it") println("Started $it")
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
Thread.sleep(200) Thread.sleep(200)