-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add androidTests for httpUrlConnection module and create test-common …
…module for common test utilities (#452) * Add androidTests for httpUrlConnection module and pull out common utils in test-common module * Ensure executor.shutdown also gets called finally. * Convertion to Kotlin * Make tests independent allowing parallel run
- Loading branch information
Showing
11 changed files
with
284 additions
and
38 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
10 changes: 10 additions & 0 deletions
10
instrumentation/httpurlconnection/testing/build.gradle.kts
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,10 @@ | ||
plugins { | ||
id("otel.android-app-conventions") | ||
id("net.bytebuddy.byte-buddy-gradle-plugin") | ||
} | ||
|
||
dependencies { | ||
byteBuddy(project(":instrumentation:httpurlconnection:agent")) | ||
implementation(project(":instrumentation:httpurlconnection:library")) | ||
implementation(project(":test-common")) | ||
} |
112 changes: 112 additions & 0 deletions
112
.../kotlin/io/opentelemetry/instrumentation/library/httpurlconnection/InstrumentationTest.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,112 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.library.httpurlconnection | ||
|
||
import io.opentelemetry.android.test.common.OpenTelemetryTestUtils | ||
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.executeGet | ||
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.post | ||
import io.opentelemetry.instrumentation.library.httpurlconnection.internal.HttpUrlConnectionSingletons | ||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
class InstrumentationTest { | ||
@Test | ||
fun testHttpUrlConnectionGetRequest_ShouldBeTraced() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
executeGet("http://httpbin.org/get") | ||
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size) | ||
inMemorySpanExporter.shutdown() | ||
} | ||
|
||
@Test | ||
fun testHttpUrlConnectionPostRequest_ShouldBeTraced() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
post("http://httpbin.org/post") | ||
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size) | ||
inMemorySpanExporter.shutdown() | ||
} | ||
|
||
@Test | ||
fun testHttpUrlConnectionGetRequest_WhenNoStreamFetchedAndNoDisconnectCalled_ShouldNotBeTraced() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
executeGet("http://httpbin.org/get", false, false) | ||
Assert.assertEquals(0, inMemorySpanExporter.finishedSpanItems.size) | ||
inMemorySpanExporter.shutdown() | ||
} | ||
|
||
@Test | ||
fun testHttpUrlConnectionGetRequest_WhenNoStreamFetchedButDisconnectCalled_ShouldBeTraced() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
executeGet("http://httpbin.org/get", false) | ||
Assert.assertEquals(1, inMemorySpanExporter.finishedSpanItems.size) | ||
inMemorySpanExporter.shutdown() | ||
} | ||
|
||
@Test | ||
fun testHttpUrlConnectionGetRequest_WhenFourConcurrentRequestsAreMade_AllShouldBeTraced() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
val executor = Executors.newFixedThreadPool(4) | ||
try { | ||
executor.submit { executeGet("http://httpbin.org/get") } | ||
executor.submit { executeGet("http://google.com") } | ||
executor.submit { executeGet("http://android.com") } | ||
executor.submit { executeGet("http://httpbin.org/headers") } | ||
|
||
executor.shutdown() | ||
// Wait for all tasks to finish execution or timeout | ||
if (executor.awaitTermination(2, TimeUnit.SECONDS)) { | ||
// if all tasks finish before timeout | ||
Assert.assertEquals(4, inMemorySpanExporter.finishedSpanItems.size) | ||
} else { | ||
// if all tasks don't finish before timeout | ||
Assert.fail( | ||
"Test could not be completed as tasks did not complete within the 2s timeout period.", | ||
) | ||
} | ||
} catch (e: InterruptedException) { | ||
// print stack trace to decipher lines that threw InterruptedException as it can be | ||
// possibly thrown by multiple calls above. | ||
e.printStackTrace() | ||
Assert.fail("Test could not be completed due to an interrupted exception.") | ||
} finally { | ||
if (!executor.isShutdown) { | ||
executor.shutdownNow() | ||
} | ||
inMemorySpanExporter.shutdown() | ||
} | ||
} | ||
|
||
@Test | ||
fun testHttpUrlConnectionRequest_ContextPropagationHappensAsExpected() { | ||
val inMemorySpanExporter = InMemorySpanExporter.create() | ||
HttpUrlConnectionSingletons.setInstrumenterForTesting(OpenTelemetryTestUtils.setUpSpanExporter(inMemorySpanExporter)) | ||
val parentSpan = OpenTelemetryTestUtils.getSpan() | ||
|
||
parentSpan.makeCurrent().use { | ||
executeGet("http://httpbin.org/get") | ||
val spanDataList = inMemorySpanExporter.finishedSpanItems | ||
if (spanDataList.isNotEmpty()) { | ||
val currentSpanData = spanDataList[0] | ||
Assert.assertEquals( | ||
parentSpan.spanContext.traceId, | ||
currentSpanData.traceId, | ||
) | ||
} | ||
} | ||
parentSpan.end() | ||
|
||
Assert.assertEquals(2, inMemorySpanExporter.finishedSpanItems.size) | ||
inMemorySpanExporter.shutdown() | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
instrumentation/httpurlconnection/testing/src/main/AndroidManifest.xml
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" > | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
58 changes: 58 additions & 0 deletions
58
...n/io/opentelemetry/instrumentation/library/httpurlconnection/HttpUrlConnectionTestUtil.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,58 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.library.httpurlconnection | ||
|
||
import android.util.Log | ||
import java.io.IOException | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import java.nio.charset.StandardCharsets | ||
|
||
object HttpUrlConnectionTestUtil { | ||
private const val TAG = "HttpUrlConnectionTest" | ||
|
||
fun executeGet( | ||
inputUrl: String, | ||
getInputStream: Boolean = true, | ||
disconnect: Boolean = true, | ||
) { | ||
var connection: HttpURLConnection? = null | ||
try { | ||
connection = URL(inputUrl).openConnection() as HttpURLConnection | ||
|
||
// always call one API that reads from the connection | ||
val responseCode = connection.responseCode | ||
|
||
val readInput = if (getInputStream) connection.inputStream.bufferedReader(StandardCharsets.UTF_8).use { it.readText() } else "" | ||
|
||
Log.d(TAG, "response code: $responseCode ,input Stream: $readInput") | ||
} catch (e: IOException) { | ||
Log.e(TAG, "Exception occurred while executing GET request", e) | ||
} finally { | ||
connection?.takeIf { disconnect }?.disconnect() | ||
} | ||
} | ||
|
||
fun post(inputUrl: String) { | ||
var connection: HttpURLConnection? = null | ||
try { | ||
connection = URL(inputUrl).openConnection() as HttpURLConnection | ||
connection.doOutput = true | ||
connection.requestMethod = "POST" | ||
|
||
connection.outputStream.bufferedWriter(StandardCharsets.UTF_8).use { out -> out.write("Writing content to output stream!") } | ||
|
||
// always call one API that reads from the connection | ||
val readInput = connection.inputStream.bufferedReader(StandardCharsets.UTF_8).use { it.readText() } | ||
|
||
Log.d(TAG, "InputStream: $readInput") | ||
} catch (e: IOException) { | ||
Log.e(TAG, "Exception occurred while executing post", e) | ||
} finally { | ||
connection?.disconnect() | ||
} | ||
} | ||
} |
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,16 @@ | ||
plugins { | ||
id("otel.android-library-conventions") | ||
} | ||
|
||
description = "OpenTelemetry Android common test utils" | ||
|
||
android { | ||
namespace = "io.opentelemetry.android.test.common" | ||
} | ||
|
||
dependencies { | ||
api(platform(libs.opentelemetry.platform)) | ||
api(libs.opentelemetry.sdk) | ||
api(libs.opentelemetry.api) | ||
implementation(libs.androidx.core) | ||
} |
Oops, something went wrong.