Skip to content

Commit

Permalink
mobile: Make FilterThrowingExceptionTest hermetic (envoyproxy#35872)
Browse files Browse the repository at this point in the history
This PR updates the test by using a local test server instead of making
an external network request, which can make the test flaky.

Risk Level: low (test only)
Testing: unit test
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw authored Aug 28, 2024
1 parent 45e0325 commit 6f618ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 1 addition & 3 deletions mobile/test/kotlin/integration/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ envoy_mobile_android_test(
srcs = [
"FilterThrowingExceptionTest.kt",
],
exec_properties = {
"dockerNetwork": "standard",
},
native_deps = [
"//test/jni:libenvoy_jni_with_test_extensions.so",
] + select({
Expand All @@ -369,5 +366,6 @@ envoy_mobile_android_test(
deps = [
"//library/kotlin/io/envoyproxy/envoymobile:envoy_interfaces_lib",
"//library/kotlin/io/envoyproxy/envoymobile:envoy_lib",
"//test/java/io/envoyproxy/envoymobile/engine/testing:http_test_server_factory_lib",
],
)
22 changes: 20 additions & 2 deletions mobile/test/kotlin/integration/FilterThrowingExceptionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ 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.EnvoyConfiguration
import io.envoyproxy.envoymobile.engine.JniLibrary
import io.envoyproxy.envoymobile.engine.testing.HttpTestServerFactory
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand Down Expand Up @@ -88,6 +92,18 @@ class FilterThrowingExceptionTest {
JniLibrary.loadTestLibrary()
}

private lateinit var httpTestServer: HttpTestServerFactory.HttpTestServer

@Before
fun setUp() {
httpTestServer = HttpTestServerFactory.start(HttpTestServerFactory.Type.HTTP2_WITH_TLS)
}

@After
fun tearDown() {
httpTestServer.shutdown()
}

@Test
fun `registers a filter that throws an exception and performs an HTTP request`() {
val onEngineRunningLatch = CountDownLatch(1)
Expand All @@ -106,6 +122,7 @@ class FilterThrowingExceptionTest {
builder
.setLogLevel(LogLevel.DEBUG)
.setLogger { _, msg -> print(msg) }
.setTrustChainVerification(EnvoyConfiguration.TrustChainVerification.ACCEPT_UNTRUSTED)
.setEventTracker { event ->
if (
event["name"] == "event_log" && event["log_name"] == "jni_cleared_pending_exception"
Expand All @@ -122,15 +139,16 @@ class FilterThrowingExceptionTest {
RequestHeadersBuilder(
method = RequestMethod.GET,
scheme = "https",
authority = "api.lyft.com",
path = "/ping"
authority = httpTestServer.address,
path = "/simple.txt"
)
.build()

engine
.streamClient()
.newStreamPrototype()
.setOnResponseHeaders { responseHeaders, _, _ ->
println("here!!")
val status = responseHeaders.httpStatus ?: 0L
assertThat(status).isEqualTo(200)
onResponseHeadersLatch.countDown()
Expand Down

0 comments on commit 6f618ea

Please sign in to comment.