Skip to content

Solve update kotlintest problems #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8abd029
fix bad merge
AdrianRaFo Jan 17, 2019
2e24093
fix laws not running
AdrianRaFo Jan 17, 2019
e33e6cf
fix deprecations
AdrianRaFo Jan 17, 2019
1956c6a
fix some laws
AdrianRaFo Jan 17, 2019
77e0d12
Merge branch 'master' into arf-solve-update-kotlintest-problems
raulraja Jan 17, 2019
82bd9ea
revert Coroutines dispatcher
AdrianRaFo Jan 17, 2019
3f7d8e5
add also regular test
AdrianRaFo Jan 17, 2019
c3b56cf
Merge remote-tracking branch 'origin/arf-solve-update-kotlintest-prob…
AdrianRaFo Jan 17, 2019
658114c
minor fix
AdrianRaFo Jan 17, 2019
97ad2b0
Merge branch 'master' into arf-solve-update-kotlintest-problems
nomisRev Jan 20, 2019
0165140
fix local IO test problems
AdrianRaFo Jan 20, 2019
5e18367
Fix ReactiveX integration Bracket implementations.
nomisRev Jan 21, 2019
52e34b1
Fix Reactor integration Bracket implementations.
nomisRev Jan 22, 2019
641817e
Merge branch 'master' into arf-solve-update-kotlintest-problems
nomisRev Jan 22, 2019
4ccd3fc
Merge branch 'master' into arf-solve-update-kotlintest-problems
nomisRev Jan 22, 2019
4fc771b
Merge branch 'master' into arf-solve-update-kotlintest-problems
raulraja Jan 23, 2019
7ab9e5d
Add comment about influence parallelism on tests
nomisRev Jan 23, 2019
f8a4778
Remove DeferredK integration
nomisRev Jan 23, 2019
3c3ff6b
set as expression body
AdrianRaFo Jan 23, 2019
27dcec9
Merge remote-tracking branch 'origin/arf-solve-update-kotlintest-prob…
AdrianRaFo Jan 23, 2019
f7e7c67
Merge branch 'master' into arf-solve-update-kotlintest-problems
raulraja Jan 23, 2019
f77b683
delete bad dependencies and fix some samples
AdrianRaFo Jan 24, 2019
fb29687
Rewrote Fiber docs, added coroutines-core to docs module
nomisRev Jan 24, 2019
b68492b
delete Deferred from docs
AdrianRaFo Jan 24, 2019
0ac2ee2
Merge remote-tracking branch 'origin/arf-solve-update-kotlintest-prob…
AdrianRaFo Jan 24, 2019
6fd968f
delete unused deps
AdrianRaFo Jan 24, 2019
1db676c
delete more unused deps
AdrianRaFo Jan 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ org.gradle.jvmargs=-Xmx4g
# Kotlin configuration
kotlin.incremental=true
# Kotlin Test configuration
kotlintest.parallelism=2
#Parallelism needs to be set to 1 since the concurrent tests in arrow-effects become flaky otherwise
kotlintest.parallelism=1
# Pomfile definitions
POM_DESCRIPTION=Functional companion to Kotlin's Standard Library
POM_URL=https://github.com/arrow-kt/arrow/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import arrow.typeclasses.Conested
import arrow.typeclasses.Eq
import arrow.typeclasses.conest
import arrow.typeclasses.counnest
import io.kotlintest.matchers.shouldBe
import io.kotlintest.properties.Gen
import io.kotlintest.properties.forAll
import io.kotlintest.runner.junit4.KotlinTestRunner
import io.kotlintest.shouldBe
import org.junit.runner.RunWith

@RunWith(KotlinTestRunner::class)
Expand Down
34 changes: 11 additions & 23 deletions modules/core/arrow-test/src/main/kotlin/arrow/test/UnitSpec.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package arrow.test

import arrow.test.laws.Law
import arrow.typeclasses.Eq
import io.kotlintest.TestCase
import io.kotlintest.TestType
import io.kotlintest.specs.AbstractStringSpec
Expand All @@ -11,28 +10,17 @@ import io.kotlintest.specs.AbstractStringSpec
*/
abstract class UnitSpec : AbstractStringSpec() {

fun testLaws(vararg laws: List<Law>): List<TestCase> {
laws
.flatMap { list: List<Law> -> list.asIterable() }
.distinctBy { law: Law -> law.name }
.map { law: Law ->
createTestCase(law.name, law.test, defaultTestCaseConfig, TestType.Test)
}
return testCases()
}
private val lawTestCases = mutableListOf<TestCase>()


fun <F> Eq<F>.logged(): Eq<F> = Eq { a, b ->
try {
val result = a.eqv(b)
if (!result) {
println("$a <---> $b")
}
result
} catch (t: Throwable) {
println("EXCEPTION: ${t.message}")
println("$a <---> $b")
false
fun testLaws(vararg laws: List<Law>): List<TestCase> = laws
.flatMap { list: List<Law> -> list.asIterable() }
.distinctBy { law: Law -> law.name }
.map { law: Law ->
val lawTestCase = createTestCase(law.name, law.test, defaultTestCaseConfig, TestType.Test)
lawTestCases.add(lawTestCase)
lawTestCase
}
}

override fun testCases(): List<TestCase> = super.testCases() + lawTestCases

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion modules/docs/arrow-docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dependencies {
compile project(':arrow-effects-extensions')
compile project(':arrow-effects-rx2-extensions')
compile project(':arrow-effects-reactor-extensions')
compile project(':arrow-effects-kotlinx-coroutines-extensions')
compile project(':arrow-optics')
compile project(':arrow-validation')
compile project(':arrow-recursion-extensions')
Expand All @@ -30,6 +29,7 @@ dependencies {
compile project(":arrow-reflect")
kapt project(':arrow-meta')
kaptTest project(':arrow-meta')
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
compile "org.jetbrains.kotlinx:kotlinx-collections-immutable:$kotlinxCollectionsImmutableVersion"
testCompile("junit:junit:$jUnitVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ Note that the result is still an `IO.Async` operation, which means it's still de

We've mentioned that `Bracket` is agnostic of whether the `use` lambda is computed synchronously or asynchronously.
That's because it's able to run over any data type `F` that can support synchronous and asynchronous
computations, like [`IO`]({{ '/docs/effects/io' | relative_url }}), [`Observable`]({{ '/docs/integrations/rx2' | relative_url }})
or [`Deferred`]({{ '/docs/integrations/kotlinxcoroutines' | relative_url }}).
computations, like [`IO`]({{ '/docs/effects/io' | relative_url }}) or [`Observable`]({{ '/docs/integrations/rx2' | relative_url }}).

It basically targets what in Functional Programming is known as a "Higher Kind".

Expand Down Expand Up @@ -210,46 +209,7 @@ println(safeComputation)
}
```

And finally over `DeferredK`:

{: data-executable='true'}

```kotlin:ank
import arrow.effects.coroutines.DeferredK
import arrow.effects.coroutines.extensions.deferredk.bracket.bracket
import arrow.Kind
import arrow.effects.typeclasses.Bracket

class File(url: String) {
fun open(): File = this
fun close(): Unit {}
override fun toString(): String = "This file contains some interesting content!"
}

class Program<F>(BF: Bracket<F, Throwable>) : Bracket<F, Throwable> by BF {

fun openFile(uri: String): Kind<F, File> = just(File(uri).open())

fun closeFile(file: File): Kind<F, Unit> = just(file.close())

fun fileToString(file: File): Kind<F, String> = just(file.toString())
}

fun main(args: Array<String>) {
//sampleStart
val deferredProgram = Program(DeferredK.bracket())

val safeComputation= with (deferredProgram) {
openFile("data.json").bracket(
release = { file -> closeFile(file) },
use = { file -> fileToString(file) })
}
//sampleEnd
println(safeComputation)
}
```

Note that we're running the exact same program passing in three different data types. All of them can provide an
Note that we're running the exact same program passing in two different data types. All of them can provide an
instance of `Bracket`, which means that they can support asynchronous and synchronous computations.

This is the style you'd usually use in a Functional Program.
Expand Down
40 changes: 30 additions & 10 deletions modules/docs/arrow-docs/docs/docs/effects/fiber/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,57 @@ A `Fiber` is a concurrency primitive for describing parallel operations or multi
Concurrently started tasks can either be joined or canceled and this are the only two operators available on `Fiber`.

Using `Fiber` we can verily easily describe parallel operations such as `parallelMap`.
**Note** the operation written below does not support proper cancellation,
when the resulting `IO` is canceled it does not propagate this cancellation back to the underlying `IO`.

```kotlin:ank
import arrow.effects.*
import arrow.effects.deferredk.monad.monad
import kotlinx.coroutines.Dispatchers.Default
import arrow.effects.io.monad.monad
import arrow.effects.typeclasses.Fiber

fun <A, B, C> parallelMap(first: DeferredK<A>,
second: DeferredK<B>,
f: (A, B) -> C): DeferredK<C> =
DeferredK.monad().binding {
val fiberOne: Fiber<ForDeferredK, A> = first.startF().bind()
val fiberTwo: Fiber<ForDeferredK, B> = second.startF().bind()
fun <A, B, C> parallelMap(first: IO<A>,
second: IO<B>,
f: (A, B) -> C): IO<C> =
IO.monad().binding {
val fiberOne: Fiber<ForIO, A> = first.startF(Default).bind()
val fiberTwo: Fiber<ForIO, B> = second.startF(Default).bind()
f(fiberOne.join.bind(), fiberTwo.join.bind())
}.fix()

val first = DeferredK.sleep(5000).map {
val first = IO.sleep(5000).map {
println("Hi, I am first")
1
}

val second = DeferredK.sleep(3000).map {
val second = IO.sleep(3000).map {
println("Hi, I am second")
2
}
```

```kotlin
parMap(first, second, Int::plus).await()
parallelMap(first, second, Int::plus).await()

//Hi, I am second
//Hi, I am first
//3
```

We could fix this snippet to support proper cancellation by using `bracket` instead of `flatMap`,
which allows us to register an operation to run on cancelation, error or completion.

```kotlin:ank
fun <A, B, C> parallelMap2(first: IO<A>,
second: IO<B>,
f: (A, B) -> C): IO<C> =
first.startF(Default).bracket(use = { (joinA, _) ->
second.startF(Default).bracket(use = { (joinB, _) ->
joinA.flatMap { a ->
joinB.map { b -> f(a, b) }
}
}, release = { (_, cancelB) -> cancelB })
}, release = { (_, cancelA) -> cancelA })
```


13 changes: 6 additions & 7 deletions modules/docs/arrow-docs/docs/docs/generic/product/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ hListOf(1000, 900).toAccount()

`@product` allows us map independent values in the context of any `Applicative` capable data type straight to the data class inside the data type context

In the examples below we can observe how 2 different `Int` properties are returned inside a type constructor such as `Option`, `Try`, `Deferred` etc... and the automatically mapped to the shape of our `Account` data class removing all boilerplate from extracting the values from their context and returning an `Account` value in the same context.
In the examples below we can observe how 2 different `Int` properties are returned inside a type constructor such as `Option`, `Try`, `IO` etc... and the automatically mapped to the shape of our `Account` data class removing all boilerplate from extracting the values from their context and returning an `Account` value in the same context.

```kotlin:ank
import arrow.core.extensions.option.applicative.applicative
Expand Down Expand Up @@ -136,14 +136,13 @@ Try.applicative().run {
```

```kotlin:ank
import arrow.effects.coroutines.*
import arrow.effects.coroutines.extensions.deferredk.applicative.applicative
import kotlinx.coroutines.async
import arrow.effects.*
import arrow.effects.extensions.io.applicative.applicative

val asyncBalance: DeferredK<Int> = DeferredK { 1000 }
val asyncAvailable: DeferredK<Int> = DeferredK { 900 }
val asyncBalance: IO<Int> = IO { 1000 }
val asyncAvailable: IO<Int> = IO { 900 }

DeferredK.applicative().run {
IO.applicative().run {
mapToAccount(asyncBalance, asyncAvailable)
}
```
Expand Down
Loading