-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Providing options for generating synchronous methods for unary RPC calls: ```kotlin val response = testServiceConnectClient.unaryCallBlocking(message) assertThat(response.code).isEqualTo(Code.UNKNOWN) response.failure { errorResponse -> assertThat(errorResponse.error).isNotNull() assertThat(errorResponse.code).isEqualTo(Code.UNKNOWN) assertThat(errorResponse.error.message).isEqualTo("test status message") } response.success { fail<Unit>("unexpected success") } ```
- Loading branch information
1 parent
a16ff78
commit 2e45257
Showing
8 changed files
with
284 additions
and
6 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
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
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
70 changes: 70 additions & 0 deletions
70
library/src/main/kotlin/build/buf/connect/UnaryBlockingCall.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,70 @@ | ||
// Copyright 2022-2023 Buf Technologies, Inc. | ||
// | ||
// 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 build.buf.connect | ||
|
||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* A [UnaryBlockingCall] contains the way to make a blocking RPC call and cancelling the RPC. | ||
*/ | ||
class UnaryBlockingCall<Output> { | ||
private var executable: ((ResponseMessage<Output>) -> Unit) -> Unit = { } | ||
private var cancel: () -> Unit = { } | ||
|
||
/** | ||
* Execute the underlying request. | ||
* Subsequent calls will create a new request. | ||
*/ | ||
fun execute(): ResponseMessage<Output> { | ||
val countDownLatch = CountDownLatch(1) | ||
val reference = AtomicReference<ResponseMessage<Output>>() | ||
executable { responseMessage -> | ||
reference.set(responseMessage) | ||
countDownLatch.countDown() | ||
} | ||
countDownLatch.await() | ||
return reference.get() | ||
} | ||
|
||
/** | ||
* Cancel the underlying request. | ||
*/ | ||
fun cancel() { | ||
cancel() | ||
} | ||
|
||
/** | ||
* Gives the blocking call a cancellation function to cancel the | ||
* underlying request. | ||
* | ||
* @param cancel The function to call in order to cancel the | ||
* underlying request. | ||
*/ | ||
internal fun setCancel(cancel: () -> Unit) { | ||
this.cancel = cancel | ||
} | ||
|
||
/** | ||
* Gives the blocking call the execution function to initiate | ||
* the underlying request. | ||
* | ||
* @param executable The function to call in order to initiate | ||
* a request. | ||
*/ | ||
internal fun setExecute(executable: ((ResponseMessage<Output>) -> Unit) -> Unit) { | ||
this.executable = executable | ||
} | ||
} |
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
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