-
Notifications
You must be signed in to change notification settings - Fork 206
Solve 6.2.0 regression due to runblocking being unavailable #575
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6000740
Add new module for testing Mockito-kotlin in a context without corout…
m-koops 8ce28a6
Introduce fallback to the runBlocking function in case kotlinx-corout…
m-koops b2917af
Use jvmToolchain API to configure builds
m-koops 759437c
Use jvmToolchain API to configure builds
m-koops 2f6cdc2
Apply safeRunBlocking in some more occasions.
m-koops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ import org.mockito.internal.stubbing.answers.ThrowsExceptionForClassType | |
| import org.mockito.kotlin.internal.CoroutineAwareAnswer | ||
| import org.mockito.kotlin.internal.CoroutineAwareAnswer.Companion.wrapAsCoroutineAwareAnswer | ||
| import org.mockito.kotlin.internal.KAnswer | ||
| import org.mockito.kotlin.internal.safeRunBlocking | ||
| import org.mockito.stubbing.Answer | ||
| import org.mockito.stubbing.OngoingStubbing | ||
|
|
||
|
|
@@ -92,8 +93,8 @@ fun <T> whenever(methodCall: T): OngoingStubbing<T> { | |
| * @return OngoingStubbing object used to stub fluently. ***Do not*** create a reference to this | ||
| * returned object. | ||
| */ | ||
| fun <T> whenever(methodCall: suspend CoroutineScope.() -> T): OngoingStubbing<T> { | ||
| return runBlocking { `when`<T>(methodCall())!! } | ||
| fun <T> whenever(methodCall: suspend () -> T): OngoingStubbing<T> { | ||
| return safeRunBlocking { `when`<T>(methodCall()) } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -108,7 +109,7 @@ fun <T> whenever(methodCall: suspend CoroutineScope.() -> T): OngoingStubbing<T> | |
| */ | ||
| @Deprecated("Use whenever { mock.methodCall() } instead") | ||
| fun <T> wheneverBlocking(methodCall: suspend CoroutineScope.() -> T): OngoingStubbing<T> { | ||
| return whenever(methodCall) | ||
| return runBlocking { `when`(methodCall()) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why doesn't this use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see my other response. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/SafeRunBlocking.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2018 Niek Haarman | ||
| * Copyright (c) 2007 Mockito contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package org.mockito.kotlin.internal | ||
|
|
||
| import kotlin.coroutines.Continuation | ||
| import kotlin.coroutines.CoroutineContext | ||
| import kotlin.coroutines.startCoroutine | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.mockito.kotlin.mock | ||
|
|
||
| internal fun <T> safeRunBlocking(block: suspend () -> T): T { | ||
| return try { | ||
| Class.forName("kotlinx.coroutines.BuildersKt") | ||
| runBlocking { block() } | ||
| } catch (_: ClassNotFoundException) { | ||
| bareBasicsRunBlocking(block) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This function is a bare basics replacement for the proper function runBlocking from the | ||
| * kotlinx-coroutines-core library. | ||
| * | ||
| * Mockito-kotlin is compiled with kotlinx-coroutines-core library on the compileOnly classpath, so | ||
| * the library is not marked as a transitive dependency of Mockito-kotlin. | ||
| * | ||
| * Mockito-kotlin API, like [org.mockito.kotlin.whenever] accepts suspending lambda arguments to | ||
| * unify the handling of synchronous and suspending lambdas. Currently, Kotlin compiler does not yet | ||
| * allow to define overload functions, one for a synchronous lambda and one for a suspending lambda: | ||
| * it leads to resolution ambiguity issues. But once a synchronous lambda is passed into the | ||
| * Mockito-kotlin API and then flagged as potentially suspending, there is no easy way to unflag the | ||
| * suspending nature. | ||
| * | ||
| * If the project that applies Mockito-kotlin is not including the kotlinx-coroutines-core | ||
| * dependency, simply because the project does not include any suspending/coroutines functionality, | ||
| * Mockito-kotlin needs an alternative mean to execute the lambda parameter flagged as suspending. | ||
| * | ||
| * Therefor this function assumes that the lambda parameter [block], although marked suspending, | ||
| * should be considered a synchronous lambda in the absence of any coroutines infrastructure as | ||
| * delivered by the kotlinx-coroutines-core library. It takes a bare basics approach to invoke the | ||
| * lambda on the current thread without any safeguards for (potential) suspension points in the | ||
| * lambda. | ||
| * | ||
| * In future, when support for overloads just differing in suspending nature of the lambda argument | ||
| * is provided by the Kotlin compiler, this rather ugly fix should be dropped in favor of | ||
| * introducing proper overloads in the Mockito-kotlin API to cater for both synchronous and | ||
| * suspending lambdas. | ||
| */ | ||
| private fun <T> bareBasicsRunBlocking(block: suspend () -> T): T { | ||
| val completion = SimpleContinuation<T>() | ||
| block.startCoroutine(completion) | ||
| return completion.result!!.let { result -> | ||
| result.exceptionOrNull()?.let { throw it } | ||
| result.getOrNull()!! | ||
| } | ||
| } | ||
|
|
||
| private class SimpleContinuation<T> : Continuation<T> { | ||
| var result: Result<T>? = null | ||
|
|
||
| override val context: CoroutineContext | ||
| get() = mock() | ||
|
|
||
| override fun resumeWith(result: Result<T>) { | ||
| this.result = result | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
| import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
|
||
| plugins { | ||
| id "org.gradle.java" | ||
| id "org.jetbrains.kotlin.jvm" version "2.1.20" | ||
| } | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation "org.mockito.kotlin:mockito-kotlin" | ||
| implementation "org.jetbrains.kotlin:kotlin-stdlib" | ||
| testImplementation 'junit:junit:4.13.2' | ||
| } | ||
|
|
||
| tasks.withType(KotlinCompile).configureEach { | ||
| compilerOptions { | ||
| jvmTarget.set(JvmTarget.JVM_11) | ||
| targetCompatibility = "11" | ||
| } | ||
| } | ||
m-koops marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| includeBuild '..' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.stubbing | ||
|
|
||
| class RegressionTest { | ||
| private val a = mock<A>() | ||
|
|
||
| @Test | ||
| fun `should stub synchronous mock in absence of the 'kotlinx-coroutines-core' library`() { | ||
| stubbing(a) { | ||
| on { doSomething() } | ||
| .thenReturn("a") | ||
| } | ||
|
|
||
| assertEquals("a", a.doSomething()) | ||
| } | ||
| } | ||
|
|
||
| interface A { | ||
| fun doSomething(): String | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised ktfmt didn't remove the
kotlinx.coroutines.runBlockingimport. Let's remove it hereand in OngoingStubbing.ktThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runBlockingis still used directly in 2 expicit 'blocking' stub methods (e.g.givenBlockingandwheneverBlocking) that already exist for longer time. These methods have a parameter of typesuspend CoroutineScope.() -> T. The CoroutineScope receiver type is preventing the use ofsafeRunBlocking.I have no clear picture whether it would be feasable to remove the receiver and simplify the parameters to
suspend () -> T. I suppose that could break existing usages of these stub methods. And it should not be a problem to userunBlockingdirectly, because these expicit 'blocking' stub methods are typically applied in the context of projects with coroutines, so thekotlinx-coroutines-corelibrary should be available.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we could extend the test suite you added with calls to these methods? Then we know for sure. For the one where it already took a coroutine as argument prior to your changes, it would be safe to use
runBlocking. That said, for consistency and potential incorrect copy-pasting in the future, let's usesafeRunBlockingin as many cases as we can.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's already the current state since my last commit this morning.
Only 2 spots use
runBlockingdirectly as described above.Talking about the new test suite, I had another thought popping up to my mind: To split the 'tests' suite in two: 1 suite for all synchronous tests in a suite without dependency to
kotlinx-coroutines-corelibrary, and 1 suite for all coroutine related tests. That would safeguard the project best for regressions I guess.I would suggest to do that split/move in a new PR of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that sounds like a good idea! Ack about
runBlocking. Will publish this as 6.2.1.