From 4a9f59283b178b1f86c9b3ccff0373b859e8728f Mon Sep 17 00:00:00 2001 From: samedson Date: Thu, 6 Feb 2025 15:24:18 -0500 Subject: [PATCH] Attempt to reproduce issues/6625 --- .../network/InstrURLConnectionBaseTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java b/firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java index 9bf193efc95..d0268d7a001 100644 --- a/firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java +++ b/firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java @@ -28,10 +28,21 @@ import com.google.firebase.perf.util.Timer; import com.google.firebase.perf.v1.ApplicationProcessState; import com.google.firebase.perf.v1.NetworkRequestMetric; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicReference; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.util.logging.Logger; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -59,6 +70,56 @@ public void setUp() { networkMetricBuilder = NetworkRequestMetricBuilder.builder(transportManager); } + @Test + public void testDisconnectConcurrently() throws IOException, InterruptedException, ExecutionException { + for (int i = 0; i < 100; i++) { + System.out.println("attempt: " + i); + int threadCount = 100; + final CountDownLatch latch = new CountDownLatch(threadCount); + + ExecutorService executor = Executors.newFixedThreadPool(100); + List> futures = new ArrayList<>(); + List> exceptions = new ArrayList<>(); + + HttpURLConnection urlConnection = mockHttpUrlConnection(); + InstrURLConnectionBase instr = new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder); + + for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { + final AtomicReference backgroundException = new AtomicReference<>(); + exceptions.add(backgroundException); + + int finalThreadIndex = threadIndex; + futures.add(executor.submit(() -> { + try { + System.out.println(" - finalThreadIndex: " + finalThreadIndex); + instr.disconnect(); + + } catch (Throwable t) { + backgroundException.set(t); + + } finally { + latch.countDown(); + } + })); + } + + for (Future future : futures) { + future.get(); + } + + executor.shutdown(); + + for (int e = 0; e < exceptions.size(); e++) { + Throwable exception = exceptions.get(e).get(); + + if (exception != null) { + System.out.println(" - Exception in thread pair " + e + ": " + exception); + } + } + } + assertThat(true).isEqualTo(true); + } + @Test public void testConnectThrowsIOException() throws IOException { HttpURLConnection urlConnection = mockHttpUrlConnection();