Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use latch in test #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

package io.opentelemetry.instrumentation.library.httpurlconnection

import android.util.Log
import io.opentelemetry.android.instrumentation.AndroidInstrumentationLoader.Companion.getInstrumentation
import io.opentelemetry.android.test.common.OpenTelemetryRumRule
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.executeGet
import io.opentelemetry.instrumentation.library.httpurlconnection.HttpUrlConnectionTestUtil.post
import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -48,19 +48,19 @@ class InstrumentationTest {
@Test
fun testHttpUrlConnectionGetRequest_WhenFourConcurrentRequestsAreMade_AllShouldBeTraced() {
val executor = Executors.newFixedThreadPool(4)
val latch = CountDownLatch(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.submit { executeGet("http://httpbin.org/get") { latch.countDown() } }
executor.submit { executeGet("http://google.com") { latch.countDown() } }
executor.submit { executeGet("http://android.com") { latch.countDown() } }
executor.submit { executeGet("http://httpbin.org/headers") { latch.countDown() } }

executor.shutdown()

if (executor.awaitTermination(2, TimeUnit.SECONDS)) {
assertThat(openTelemetryRumRule.inMemorySpanExporter.finishedSpanItems.size).isEqualTo(4)
} else {
Log.e(TAG, "Test could not be completed as tasks did not complete within the 2s timeout period.")
}
// Timeout large enough to allow tests to pass consistently in CI, but happy path is fast.
// Only in failure will this be slow. As soon as all 4 executor jobs finish, this completes.
assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue()
assertThat(openTelemetryRumRule.inMemorySpanExporter.finishedSpanItems.size).isEqualTo(4)
} finally {
if (!executor.isShutdown) {
executor.shutdownNow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object HttpUrlConnectionTestUtil {
inputUrl: String,
getInputStream: Boolean = true,
disconnect: Boolean = true,
onComplete: Runnable = Runnable {},
) {
var connection: HttpURLConnection? = null
try {
Expand All @@ -33,6 +34,7 @@ object HttpUrlConnectionTestUtil {
Log.e(TAG, "Exception occurred while executing GET request", e)
} finally {
connection?.takeIf { disconnect }?.disconnect()
onComplete.run()
}
}

Expand Down