-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mobile grpc: expose cancel method on a stream (#24780)
Commit Message: Expose `cancel` method on a gRPC stream. Additional Description: Risk Level: Low, new API Testing: Unit/integration tests Docs Changes: Release Notes: Platform Specific Features: [Optional Runtime guard:] [Optional Fixes #Issue] [Optional Fixes commit #PR or SHA] [Optional Deprecated:] [Optional [API Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]
- Loading branch information
1 parent
bfa849b
commit 2cdaac4
Showing
9 changed files
with
354 additions
and
2 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
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,149 @@ | ||
package test.kotlin.integration | ||
|
||
import io.envoyproxy.envoymobile.Custom | ||
import io.envoyproxy.envoymobile.EngineBuilder | ||
import io.envoyproxy.envoymobile.EnvoyError | ||
import io.envoyproxy.envoymobile.FilterDataStatus | ||
import io.envoyproxy.envoymobile.FilterHeadersStatus | ||
import io.envoyproxy.envoymobile.FilterTrailersStatus | ||
import io.envoyproxy.envoymobile.FinalStreamIntel | ||
import io.envoyproxy.envoymobile.GRPCClient | ||
import io.envoyproxy.envoymobile.GRPCRequestHeadersBuilder | ||
import io.envoyproxy.envoymobile.RequestMethod | ||
import io.envoyproxy.envoymobile.ResponseFilter | ||
import io.envoyproxy.envoymobile.ResponseHeaders | ||
import io.envoyproxy.envoymobile.ResponseTrailers | ||
import io.envoyproxy.envoymobile.StreamIntel | ||
import io.envoyproxy.envoymobile.engine.JniLibrary | ||
import java.nio.ByteBuffer | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
|
||
private val filterName = "cancel_validation_filter" | ||
private val config = | ||
""" | ||
static_resources: | ||
listeners: | ||
- name: base_api_listener | ||
address: | ||
socket_address: { protocol: TCP, address: 0.0.0.0, port_value: 10000 } | ||
api_listener: | ||
api_listener: | ||
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.EnvoyMobileHttpConnectionManager | ||
config: | ||
stat_prefix: api_hcm | ||
route_config: | ||
name: api_router | ||
virtual_hosts: | ||
- name: api | ||
domains: ["*"] | ||
routes: | ||
- match: { prefix: "/" } | ||
route: { cluster: fake_remote } | ||
http_filters: | ||
- name: envoy.filters.http.local_error | ||
typed_config: | ||
"@type": type.googleapis.com/envoymobile.extensions.filters.http.local_error.LocalError | ||
- name: envoy.filters.http.platform_bridge | ||
typed_config: | ||
"@type": type.googleapis.com/envoymobile.extensions.filters.http.platform_bridge.PlatformBridge | ||
platform_filter_name: $filterName | ||
- name: envoy.router | ||
typed_config: | ||
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router | ||
clusters: | ||
- name: fake_remote | ||
connect_timeout: 0.25s | ||
type: STATIC | ||
lb_policy: ROUND_ROBIN | ||
load_assignment: | ||
cluster_name: fake_remote | ||
endpoints: | ||
- lb_endpoints: | ||
- endpoint: | ||
address: | ||
socket_address: { address: 127.0.0.1, port_value: ${(10001..11000).random()} } | ||
""" | ||
|
||
class CancelGRPCStreamTest { | ||
|
||
init { | ||
JniLibrary.loadTestLibrary() | ||
} | ||
|
||
private val filterExpectation = CountDownLatch(1) | ||
private val onCancelCallbackExpectation = CountDownLatch(1) | ||
|
||
class CancelValidationFilter( | ||
private val latch: CountDownLatch | ||
) : ResponseFilter { | ||
override fun onResponseHeaders( | ||
headers: ResponseHeaders, | ||
endStream: Boolean, | ||
streamIntel: StreamIntel | ||
): FilterHeadersStatus<ResponseHeaders> { | ||
return FilterHeadersStatus.Continue(headers) | ||
} | ||
|
||
override fun onResponseData( | ||
body: ByteBuffer, | ||
endStream: Boolean, | ||
streamIntel: StreamIntel | ||
): FilterDataStatus<ResponseHeaders> { | ||
return FilterDataStatus.Continue(body) | ||
} | ||
|
||
override fun onResponseTrailers( | ||
trailers: ResponseTrailers, | ||
streamIntel: StreamIntel | ||
): FilterTrailersStatus<ResponseHeaders, ResponseTrailers> { | ||
return FilterTrailersStatus.Continue(trailers) | ||
} | ||
|
||
override fun onError(error: EnvoyError, finalStreamIntel: FinalStreamIntel) {} | ||
override fun onComplete(finalStreamIntel: FinalStreamIntel) {} | ||
|
||
override fun onCancel(finalStreamIntel: FinalStreamIntel) { | ||
latch.countDown() | ||
} | ||
} | ||
|
||
@Test | ||
fun `cancel grpc stream calls onCancel callback`() { | ||
val engine = EngineBuilder(Custom(config)) | ||
.addPlatformFilter( | ||
name = filterName, | ||
factory = { CancelValidationFilter(filterExpectation) } | ||
) | ||
.setOnEngineRunning {} | ||
.build() | ||
|
||
val client = GRPCClient(engine.streamClient()) | ||
|
||
val requestHeaders = GRPCRequestHeadersBuilder( | ||
scheme = "https", | ||
authority = "example.com", | ||
path = "/test" | ||
) | ||
.build() | ||
|
||
client.newGRPCStreamPrototype() | ||
.setOnCancel { _ -> | ||
onCancelCallbackExpectation.countDown() | ||
} | ||
.start(Executors.newSingleThreadExecutor()) | ||
.sendHeaders(requestHeaders, false) | ||
.cancel() | ||
|
||
filterExpectation.await(10, TimeUnit.SECONDS) | ||
onCancelCallbackExpectation.await(10, TimeUnit.SECONDS) | ||
|
||
engine.terminate() | ||
|
||
assertThat(filterExpectation.count).isEqualTo(0) | ||
assertThat(onCancelCallbackExpectation.count).isEqualTo(0) | ||
} | ||
} |
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
Oops, something went wrong.