From 09de98ddcd86ca60eb0c2d3ed891c90a69b04b57 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Wed, 1 Dec 2021 14:26:59 +0000
Subject: [PATCH] Wait for service to stop at the end of each test.

---
 .../java/org/torproject/jni/TorServiceTest.java           | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tor-android-binary/src/androidTest/java/org/torproject/jni/TorServiceTest.java b/tor-android-binary/src/androidTest/java/org/torproject/jni/TorServiceTest.java
index 68b6ff5a..5bd2a35e 100644
--- a/tor-android-binary/src/androidTest/java/org/torproject/jni/TorServiceTest.java
+++ b/tor-android-binary/src/androidTest/java/org/torproject/jni/TorServiceTest.java
@@ -174,6 +174,7 @@ public class TorServiceTest {
         FileUtils.write(torrc, dnsPort + " " + testValue + "\n");
 
         final CountDownLatch startedLatch = new CountDownLatch(1);
+        final CountDownLatch stoppedLatch = new CountDownLatch(1);
         BroadcastReceiver receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
@@ -185,6 +186,8 @@ public class TorServiceTest {
                 Log.i(TAG, "receiver.onReceive: " + status + " " + intent);
                 if (TorService.STATUS_ON.equals(status)) {
                     startedLatch.countDown();
+                } else if (TorService.STATUS_OFF.equals(status)) {
+                    stoppedLatch.countDown();
                 }
             }
         };
@@ -203,12 +206,14 @@ public class TorServiceTest {
         assertEquals(testValue, getConf(torService.getTorControlConnection(), dnsPort));
 
         serviceRule.unbindService();
+        stoppedLatch.await();
     }
 
     @Test
     public void testDownloadingLargeFile() throws TimeoutException, InterruptedException, IOException {
         Assume.assumeTrue("Only works on Android 7.1.2 or higher", Build.VERSION.SDK_INT >= 24);
         final CountDownLatch startedLatch = new CountDownLatch(1);
+        final CountDownLatch stoppedLatch = new CountDownLatch(1);
         BroadcastReceiver receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
@@ -220,6 +225,8 @@ public class TorServiceTest {
                 Log.i(TAG, "receiver.onReceive: " + status + " " + intent);
                 if (TorService.STATUS_ON.equals(status)) {
                     startedLatch.countDown();
+                } else if (TorService.STATUS_OFF.equals(status)) {
+                    stoppedLatch.countDown();
                 }
             }
         };
@@ -252,6 +259,7 @@ public class TorServiceTest {
         IOUtils.copy(connection.getInputStream(), new FileWriter(new File("/dev/null")));
 
         serviceRule.unbindService();
+        stoppedLatch.await();
     }
 
     private static boolean canConnectToSocket(String host, int port) {
-- 
GitLab