-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add coroutine scope stub builder ext (#43)
* remove coroutine scope interface from generated stubs * add coroutine scope stub builder ext and generic stub def interface * bug fix for additional context builder for abstract stub ext * add suspending stub builder to generated code
- Loading branch information
1 parent
202e1f4
commit 4f706e1
Showing
5 changed files
with
196 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
98 changes: 98 additions & 0 deletions
98
...lus-coroutines/src/main/kotlin/com/github/marcoferrer/krotoplus/coroutines/StubSupport.kt
This file contains 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,98 @@ | ||
/* | ||
* Copyright 2019 Kroto+ Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.marcoferrer.krotoplus.coroutines | ||
|
||
import io.grpc.Channel | ||
import io.grpc.stub.AbstractStub | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.newCoroutineContext | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
|
||
/** | ||
* Represents the metadata related to a specific grpc stub type. This interface is used to provide a generic form of | ||
* instantiating grpc stubs. This interface is used mainly in implementations of companion objects in the generated | ||
* coroutine stub clients. | ||
* | ||
* ``` | ||
* // GreeterCoroutineGrpc.GreeterCoroutineStub.Companion implements the [StubDefinition] interface | ||
* | ||
* println(GreeterCoroutineGrpc.GreeterCoroutineStub.serviceName) | ||
* | ||
* val stub = GreeterCoroutineGrpc.GreeterCoroutineStub.newStub(channel) | ||
* | ||
* ``` | ||
*/ | ||
public interface StubDefinition<T : AbstractStub<T>> { | ||
|
||
/** | ||
* The canonical name of the service this stub represents | ||
*/ | ||
public val serviceName: String | ||
|
||
/** | ||
* Create a new stub of type [T] that is bound to the supplied [channel] | ||
*/ | ||
public fun newStub(channel: Channel): T | ||
|
||
/** | ||
* Create a new stub of type [T] that is bound to the supplied [channel] and implicit coroutineContext | ||
* as a call option. | ||
*/ | ||
public suspend fun newStubWithContext(channel: Channel): T | ||
|
||
} | ||
|
||
/** | ||
* Creates a new grpc stub, inheriting the context of the receiving [CoroutineScope]. Additional context elements can | ||
* be specified with the [context] argument. | ||
* | ||
* This builder is meant to provide a mechanism for creating a new stub instance while explicitly defining what scope | ||
* the executed rpcs wil run in. This method makes it clear that the resulting stub will use the receiving scope to | ||
* create any child coroutines if necessary. | ||
* | ||
* One case of child jobs being created using this scope as a parent is during manual flow control management in | ||
* streaming variations of rpcs. | ||
* | ||
* ``` | ||
* | ||
* launch { | ||
* val stub = newGrpcStub(GreeterCoroutineStub, channel) | ||
* .... | ||
* } | ||
* | ||
* ``` | ||
* | ||
* @param context additional to [CoroutineScope.coroutineContext] context of the coroutine. | ||
* @param stubDefinition the definition of the stub to create. Usually implemented in the companion object of the | ||
* generated client stubs. | ||
* | ||
* @param channel the channel that this stub will use to do communications | ||
* | ||
*/ | ||
fun <T : AbstractStub<T>> CoroutineScope.newGrpcStub( | ||
stubDefinition: StubDefinition<T>, | ||
channel: Channel, | ||
context: CoroutineContext = EmptyCoroutineContext | ||
): T { | ||
val newContext = newCoroutineContext(context) | ||
|
||
return stubDefinition | ||
.newStub(channel) | ||
.withCoroutineContext(newContext) | ||
} | ||
|
56 changes: 56 additions & 0 deletions
56
...oroutines/src/test/kotlin/com/github/marcoferrer/krotoplus/coroutines/StubSupportTests.kt
This file contains 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,56 @@ | ||
/* | ||
* Copyright 2019 Kroto+ Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.github.marcoferrer.krotoplus.coroutines | ||
|
||
import io.grpc.Channel | ||
import io.grpc.examples.helloworld.GreeterCoroutineGrpc | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.CoroutineName | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class NewGrpcStubTests { | ||
|
||
@Test | ||
fun `Stub inherits context from coroutine scope`(){ | ||
|
||
val nameElement = CoroutineName("test_name") | ||
val channel = mockk<Channel>() | ||
val scope = CoroutineScope(nameElement) | ||
val stub = scope | ||
.newGrpcStub(GreeterCoroutineGrpc.GreeterCoroutineStub, channel) | ||
|
||
assertEquals(nameElement.name, stub.coroutineContext[CoroutineName]?.name) | ||
} | ||
|
||
|
||
@Test | ||
fun `Stub inherits context from coroutine scope and context argument`(){ | ||
|
||
val scopeJob = Job() | ||
val scopeNameElement = CoroutineName("test_name") | ||
val expectedNameElement = CoroutineName("expected_name") | ||
val channel = mockk<Channel>() | ||
val stub = CoroutineScope(scopeNameElement + scopeJob) | ||
.newGrpcStub(GreeterCoroutineGrpc.GreeterCoroutineStub, channel,expectedNameElement) | ||
|
||
assertEquals(expectedNameElement.name, stub.coroutineContext[CoroutineName]?.name) | ||
assertEquals(scopeJob, stub.coroutineContext[Job]) | ||
} | ||
} |
This file contains 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 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