Bug 1553515 - Use environment.defaultTimeoutMillis in tests. r=snorp
☠☠ backed out by b6e6ade7d550 ☠ ☠
authorAgi Sferro <agi@sferro.dev>
Thu, 27 Jun 2019 19:00:23 +0000
changeset 543249 3d1a0d412dfd234a4a33e4c2ec4876fc88970c20
parent 543248 2b1f78e255ce690feed0d2df6e3538dfd84377df
child 543250 9e9dc0e7afb39be4eaa7919782e50600388eb0dd
push id2131
push userffxbld-merge
push dateMon, 26 Aug 2019 18:30:20 +0000
treeherdermozilla-release@b19ffb3ca153 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1553515
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1553515 - Use environment.defaultTimeoutMillis in tests. r=snorp Differential Revision: https://phabricator.services.mozilla.com/D32575
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.java
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExecutorTest.kt
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/crash/CrashTest.kt
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.java
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.java
@@ -1,13 +1,14 @@
 package org.mozilla.geckoview.test;
 
 import org.mozilla.geckoview.GeckoResult;
 import org.mozilla.geckoview.GeckoResult.OnExceptionListener;
 import org.mozilla.geckoview.GeckoResult.OnValueListener;
+import org.mozilla.geckoview.test.util.Environment;
 import org.mozilla.geckoview.test.util.UiThreadUtils;
 
 import android.os.Handler;
 import android.os.Looper;
 import android.support.test.annotation.UiThreadTest;
 import android.support.test.filters.MediumTest;
 import android.support.test.runner.AndroidJUnit4;
 
@@ -19,28 +20,28 @@ import java.util.concurrent.SynchronousQ
 import java.util.concurrent.TimeoutException;
 
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.assertThat;
 
 @RunWith(AndroidJUnit4.class)
 @MediumTest
 public class GeckoResultTest {
-    private static final long DEFAULT_TIMEOUT = 5000;
-
     private static class MockException extends RuntimeException {
     }
 
     private boolean mDone;
 
+    private final Environment mEnv = new Environment();
+
     private void waitUntilDone() {
         assertThat("We should not be done", mDone, equalTo(false));
 
         while (!mDone) {
-            UiThreadUtils.loopUntilIdle(DEFAULT_TIMEOUT);
+            UiThreadUtils.loopUntilIdle(mEnv.getDefaultTimeoutMillis());
         }
     }
 
     private void done() {
         UiThreadUtils.HANDLER.post(new Runnable() {
             @Override
             public void run() {
                 mDone = true;
@@ -390,35 +391,16 @@ public class GeckoResultTest {
                 GeckoResult.fromValue(42).poll(0), equalTo(42));
     }
 
     @Test(expected = MockException.class)
     public void pollCompleteWithError() throws Throwable {
         GeckoResult.fromException(new MockException()).poll(0);
     }
 
-    @Test
-    public void pollIncompleteWithValue() throws Throwable {
-        final GeckoResult<Integer> result = new GeckoResult<>();
-
-        final Thread thread = new Thread(() -> result.complete(42));
-
-        thread.start();
-        assertThat("Value should match", result.poll(), equalTo(42));
-    }
-
-    @Test(expected = MockException.class)
-    public void pollIncompleteWithError() throws Throwable {
-        final GeckoResult<Void> result = new GeckoResult<>();
-
-        final Thread thread = new Thread(() -> result.completeExceptionally(new MockException()));
-
-        thread.start();
-        result.poll();
-    }
 
     @Test(expected = TimeoutException.class)
     public void pollTimeout() throws Throwable {
         new GeckoResult<Void>().poll(1);
     }
 
     @UiThreadTest
     @Test(expected = TimeoutException.class)
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/GeckoResultTest.kt
@@ -0,0 +1,37 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+package org.mozilla.geckoview.test
+
+import org.junit.Test
+import org.mozilla.geckoview.GeckoResult
+import org.mozilla.geckoview.test.util.Environment
+
+import org.hamcrest.Matchers.*
+import org.junit.Assert.assertThat
+
+val env = Environment()
+
+fun <T> GeckoResult<T>.pollDefault(): T? =
+        this.poll(env.defaultTimeoutMillis)
+
+class GeckoResultTestKotlin {
+    class MockException : RuntimeException()
+
+    @Test fun pollIncompleteWithValue() {
+        val result = GeckoResult<Int>()
+        val thread = Thread { result.complete(42) }
+
+        thread.start()
+        assertThat("Value should match", result.pollDefault(), equalTo(42))
+    }
+
+    @Test(expected = MockException::class) fun pollIncompleteWithError() {
+        val result = GeckoResult<Void>()
+
+        val thread = Thread { result.completeExceptionally(MockException()) }
+        thread.start()
+
+        result.pollDefault()
+    }
+}
\ No newline at end of file
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExecutorTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/WebExecutorTest.kt
@@ -29,16 +29,17 @@ import java.util.concurrent.CountDownLat
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.*
 
 import org.json.JSONObject
 import org.junit.*
 
 import org.junit.rules.ExpectedException
 import org.junit.runner.RunWith
+import org.mozilla.geckoview.GeckoResult
 
 import org.mozilla.geckoview.GeckoWebExecutor
 import org.mozilla.geckoview.WebRequest
 import org.mozilla.geckoview.WebRequestError
 import org.mozilla.geckoview.WebResponse
 
 import org.mozilla.geckoview.test.util.Environment
 import org.mozilla.geckoview.test.util.HttpBin
@@ -50,17 +51,16 @@ import java.util.*
 @RunWith(AndroidJUnit4::class)
 class WebExecutorTest {
     companion object {
         val TEST_ENDPOINT: String = "http://localhost:4242"
     }
 
     lateinit var executor: GeckoWebExecutor
     lateinit var server: HttpBin
-    val env = Environment()
 
     @get:Rule val thrown = ExpectedException.none()
 
     @Before
     fun setup() {
         // Using @UiThreadTest here does not seem to block
         // the tests which are not using @UiThreadTest, so we do that
         // ourselves here as GeckoRuntime needs to be initialized
@@ -81,17 +81,17 @@ class WebExecutorTest {
         server.stop()
     }
 
     private fun fetch(request: WebRequest): WebResponse {
         return fetch(request, GeckoWebExecutor.FETCH_FLAGS_NONE)
     }
 
     private fun fetch(request: WebRequest, flags: Int): WebResponse {
-        return executor.fetch(request, flags).poll(env.defaultTimeoutMillis)!!
+        return executor.fetch(request, flags).pollDefault()!!
     }
 
     fun String.toDirectByteBuffer(): ByteBuffer {
         val chars = CharBuffer.wrap(this)
         val buffer = ByteBuffer.allocateDirect(this.length)
         Charset.forName("UTF-8").newEncoder().encode(chars, buffer, true)
 
         return buffer
@@ -249,52 +249,52 @@ class WebExecutorTest {
         executor.speculativeConnect("http://localhost")
 
         // This is just a fence to ensure the above actually ran.
         fetch(WebRequest("$TEST_ENDPOINT/cookies"))
     }
 
     @Test
     fun testResolveV4() {
-        val addresses = executor.resolve("localhost").poll()!!
+        val addresses = executor.resolve("localhost").pollDefault()!!
         assertThat("Addresses should not be null",
                 addresses, notNullValue())
         assertThat("First address should be loopback",
                 addresses.first().isLoopbackAddress, equalTo(true))
         assertThat("First address size should be 4",
                 addresses.first().address.size, equalTo(4))
     }
 
     @Test
     @SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
     fun testResolveV6() {
-        val addresses = executor.resolve("ip6-localhost").poll()!!
+        val addresses = executor.resolve("ip6-localhost").pollDefault()!!
         assertThat("Addresses should not be null",
                 addresses, notNullValue())
         assertThat("First address should be loopback",
                 addresses.first().isLoopbackAddress, equalTo(true))
         assertThat("First address size should be 16",
                 addresses.first().address.size, equalTo(16))
     }
 
     @Test
     fun testFetchUnknownHost() {
         thrown.expect(equalTo(WebRequestError(WebRequestError.ERROR_UNKNOWN_HOST, WebRequestError.ERROR_CATEGORY_URI)))
         fetch(WebRequest("https://this.should.not.resolve"))
     }
 
     @Test(expected = UnknownHostException::class)
     fun testResolveError() {
-        executor.resolve("this.should.not.resolve").poll()
+        executor.resolve("this.should.not.resolve").pollDefault()
     }
 
     @Test
     fun testFetchStream() {
         val expectedCount = 1 * 1024 * 1024 // 1MB
-        val response = executor.fetch(WebRequest("$TEST_ENDPOINT/bytes/$expectedCount")).poll(env.defaultTimeoutMillis)!!
+        val response = executor.fetch(WebRequest("$TEST_ENDPOINT/bytes/$expectedCount")).pollDefault()!!
 
         assertThat("Status code should match", response.statusCode, equalTo(200))
         assertThat("Content-Length should match", response.headers["Content-Length"]!!.toInt(), equalTo(expectedCount))
 
         val stream = response.body!!
         val bytes = stream.readBytes(expectedCount)
         stream.close()
 
@@ -303,17 +303,17 @@ class WebExecutorTest {
         val digest = MessageDigest.getInstance("SHA-256").digest(bytes)
         assertThat("Hashes should match", response.headers["X-SHA-256"],
                 equalTo(String.format("%064x", BigInteger(1, digest))))
     }
 
     @Test
     fun testFetchStreamCancel() {
         val expectedCount = 1 * 1024 * 1024 // 1MB
-        val response = executor.fetch(WebRequest("$TEST_ENDPOINT/bytes/$expectedCount")).poll(env.defaultTimeoutMillis)!!
+        val response = executor.fetch(WebRequest("$TEST_ENDPOINT/bytes/$expectedCount")).pollDefault()!!
 
         assertThat("Status code should match", response.statusCode, equalTo(200))
         assertThat("Content-Length should match", response.headers["Content-Length"]!!.toInt(), equalTo(expectedCount))
 
         val stream = response.body!!;
 
         assertThat("Stream should have 0 bytes available", stream.available(), equalTo(0))
 
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/crash/CrashTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/crash/CrashTest.kt
@@ -5,60 +5,40 @@ import android.os.Message
 import android.os.Messenger
 import android.support.test.InstrumentationRegistry
 import android.support.test.filters.MediumTest
 import android.support.test.rule.ServiceTestRule
 import android.support.test.runner.AndroidJUnit4
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.notNullValue
 import org.junit.Assert.assertThat
+import org.junit.Assume
 import org.junit.Before
 import org.junit.Rule
 import org.junit.Test
-import org.junit.rules.Timeout
 import org.junit.runner.RunWith
+import org.mozilla.geckoview.BuildConfig
 import org.mozilla.geckoview.GeckoRuntime
 import org.mozilla.geckoview.test.util.Environment
 import java.io.File
+import java.util.concurrent.TimeUnit
 
 @RunWith(AndroidJUnit4::class)
 @MediumTest
 class CrashTest {
-    companion object {
-        val DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS = 90000L
-        val DEFAULT_X86_DEVICE_TIMEOUT_MILLIS = 60000L
-        val DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS = 180000L
-        val DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS = 60000L
-    }
-
     lateinit var messenger: Messenger
+    val env = Environment()
 
     @get:Rule val rule = ServiceTestRule()
 
-    @get:Rule val timeoutRule = Timeout.millis(getTimeoutMillis())
-
-    fun getTimeoutMillis(): Long {
-        val env = Environment()
-        if (env.isX86) {
-            return if (env.isEmulator)
-                CrashTest.DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS
-            else
-                CrashTest.DEFAULT_X86_DEVICE_TIMEOUT_MILLIS
-        }
-        return if (env.isEmulator)
-            CrashTest.DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS
-        else
-            CrashTest.DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS
-    }
-
     @Before
     fun setup() {
         CrashTestHandler.queue.clear()
 
-        val context = InstrumentationRegistry.getTargetContext();
+        val context = InstrumentationRegistry.getTargetContext()
         val binder = rule.bindService(Intent(context, RemoteGeckoService::class.java))
         messenger = Messenger(binder)
         assertThat("messenger should not be null", binder, notNullValue())
     }
 
     fun assertCrashIntent(intent: Intent, fatal: Boolean) {
         assertThat("Action should match",
                 intent.action, equalTo(GeckoRuntime.ACTION_CRASHED))
@@ -79,12 +59,16 @@ class CrashTest {
     @Test
     fun crashParent() {
         messenger.send(Message.obtain(null, RemoteGeckoService.CMD_CRASH_PARENT_NATIVE))
         assertCrashIntent(CrashTestHandler.queue.take(), true)
     }
 
     @Test
     fun crashContent() {
+        // We need the crash reporter for this test
+        Assume.assumeTrue(BuildConfig.MOZ_CRASHREPORTER)
+
         messenger.send(Message.obtain(null, RemoteGeckoService.CMD_CRASH_CONTENT_NATIVE))
-        assertCrashIntent(CrashTestHandler.queue.take(), false)
+        assertCrashIntent(CrashTestHandler.queue.poll(
+                env.defaultTimeoutMillis, TimeUnit.MILLISECONDS), false)
     }
 }